-
Notifications
You must be signed in to change notification settings - Fork 174
/
Copy pathmain.jl
executable file
·82 lines (66 loc) · 1.55 KB
/
main.jl
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
using JSON
using _
io = open("dictionary.txt")
lines = readlines(io)
close(io)
lines = map(strip, lines[27:end])
isheader(l) = ismatch(r"^[A-Z\s-_]+$", l)
isdefn(l) = ismatch(r"^Defn:", l)
isword(l) = ismatch(r"^[A-z]+$", l)
getwords(d) = filter(isword, split(d, r"[^A-z]", false))
dictionary = Dict()
word = false
defining = false
defn = ""
words = {}
for line in lines
if isheader(line)
if !haskey(dictionary, line)
# start looking for definition
word = line
push!(words, word)
end
continue
end
if isdefn(line)
defn = ""
if word != false
# begin reading definition
defining = true
replaced = replace(line, "Defn: ", "")
defn = "$defn$replaced"
end
continue
end
if defining
if word != false
if line == ""
dictionary[word] = defn
defining = false
word = false
continue
end
defn = "$defn $line"
end
end
end
io = open("dictionary.json", "w+")
JSON.print(io, dictionary)
close(io)
nodes = unique(keys(dictionary))
graph = Dict()
# Build a graph representation
for node in nodes
definition = dictionary[node]
try
words = getwords(definition)
catch x
println("Error on word: $node. Defn: $definition")
continue
end
words = _.without(words, node)
graph[node] = map(uppercase, words)
end
io = open("graph.json", "w+")
JSON.print(io, graph)
close(io)