-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cr
124 lines (110 loc) · 2.99 KB
/
main.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/ruby
########################################################################
# #
# Author: Brian Hood #
# Description: Anagrams Generator #
# Email: [email protected] #
# Version: v0.1 #
# #
########################################################################
# Crystal version using crystal-monetdb-libmapi
#
# You will need todo shards update or crystal deps before you compile
#
# To build
#
# crystal build main.cr
# ./main
require "./crystal-monetdb-libmapi/monetdb"
require "./crystal-monetdb-libmapi/monetdb_data"
class Anagrams
property? mid : MonetDBMAPI::Mapi
getter host, username, password, db, mid, conn
setter host, username, password, db, mid, conn, quiet
def initialize
@host = "172.17.0.7"
@port = 50000
@username = "monetdb"
@password = "monetdb"
@db = "anagrams"
@quiet = true
@conn = MonetDB::ClientJSON.new
@conn.host = @host
@conn.port = @port
@conn.username = @username
@conn.password = @password
@conn.db = @db
@mid = @conn.connect
@conn.setAutocommit(@mid, true)
end
def perms(word, maxgenperms)
unless @quiet == false; print "\e[1;34mWord:\e[0m\ \e[1;32m#{word}\e[0m\ \e[1;34mPermutations:\e[0m\ "; end
#print "Word: #{word}"
@sql = "INSERT INTO \"anagrams\".\"words\" (word, anagrams) VALUES ('#{word}', "
b = Hash(Int32, String).new
l = 0_i32
@sql = "#{@sql}' "
maxgenperms.times {|n|
a = "#{word}".split(//).shuffle
#puts a
b.merge!({l => a.join})
l += 1
}
b.each { |s, x|
@sql = "#{@sql}#{x} "
}
@sql = "#{@sql}');"
#puts "SQL: #{@sql}"
return b
end
def show(result)
i = 0
result.each {|n, x|
unless @quiet == false
if i <= 5
print "\e[1;33m#{x} "
i = i + 1
end
end
}
print " ...\e[0m\ "
unless @quiet == false; print "\n"; end
end
def dbclose
@conn.destroy(@mid)
end
def readfile(name)
begin
#dbconnect
File.open("#{name}") {|t|
t.each_line {|l|
w = l.strip
#puts w
unless Regex.new(w) =~ "'"
c = self.perms("#{w}", 150)
#puts c
#self.show(c)
begin
@conn.query(@mid, "#{@sql}")
unless @quiet == false; print "\e[1;30mSQL: #{@sql}\e[0m\ \n"; end
rescue
puts "DB Error"
exit
end
end
}
}
@conn.query(@mid, "COMMIT;")
#rescue Errno::EBADF
# puts "Stopping import"
# puts "Bye!"
end
self.dbclose
end
end
# Start the job
puts Time.now
t = Anagrams.new
t.quiet = false
t.readfile("wordsEn.txt")
puts Time.now