-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordnet.py
82 lines (71 loc) · 2.12 KB
/
wordnet.py
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
# -*- coding: utf-8 -*-
from pygraph.classes.digraph import digraph
from pygraph.algorithms.searching import depth_first_search, breadth_first_search
import codecs
import unicodedata
import os, inspect
from cookingsite.settings import RECIPES_COLLECTION_PREFIX
prefix = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
f = codecs.open(RECIPES_COLLECTION_PREFIX+"/words/wordnet.txt", 'r', "utf-8")
graph = digraph()
nodes = set()
inverseGraph = digraph()
allL = []
for l in f:
l = l.strip()
if l.startswith("s;"): #synonym line
continue
l = l.split(u"|")
l = [n for n in l]
for n in l:
if n not in nodes:
if not n:
continue
nodes.add(n)
allL.append(n)
graph.add_node(n)
inverseGraph.add_node(n)
for i,n in enumerate(l):
if i<len(l)-1:
try:
graph.add_edge((l[i], l[i+1]))
except:
print("could not add edge %s <-> %s" % (l[i], l[i+1]))
raise RuntimeError("fix it")
inverseGraph.add_edge((l[i+1], l[i]))
def knowsWord(x):
return x in nodes
def generalize(x):
if x not in nodes:
return []
return breadth_first_search(graph, root=x)[1]
def dfs(x):
if x not in nodes:
return None
return depth_first_search(graph, root=x)
def bfs(x):
if x not in nodes:
return None
return breadth_first_search(graph, root=x)
def bfs_depth(rootNode):
if rootNode not in graph:
return None
distances = {}
visited = { rootNode : 0}
queue = []
queue.append(rootNode)
while len(queue) > 0:
node = queue.pop(0)
depth = visited[node] + 1
distance = distances.get(depth, [])
for child in graph.neighbors(node):
if not child in visited:
visited[child] = depth
distance.append(child)
queue.append(child)
distances[depth] = distance
return visited
def descendants(x):
if x not in nodes:
return []
return breadth_first_search(inverseGraph, root=x)[1][1:]