-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentity_trees.py
More file actions
141 lines (113 loc) · 4.25 KB
/
entity_trees.py
File metadata and controls
141 lines (113 loc) · 4.25 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
from typing import NamedTuple
from textblob import TextBlob
class SimpleLinkedListNode:
def __init__(self, value):
self.value = value
self.next = None
self.previous = None
def replace_val(self, new_val):
self.value = new_val
class SimpleLinkedList:
def __init__(self):
self._head = None
self._tail = None
def append(self, val):
new_node = SimpleLinkedListNode(val)
if self._head is None:
self._head = new_node
self._tail = new_node
else:
new_node.previous = self._tail
self._tail.next = new_node
def walk(self):
current_node_record = [None]
def remove():
current_node = current_node_record[0]
if current_node.previous is None:
self._head = current_node.next
if self._head is None:
self._tail = None
current_node_record[0] = None
else:
current_node.next.previous = None
current_node_record[0] = current_node.next
else:
current_node_record[0] = current_node.next
if current_node.next is None:
self._tail = current_node.previous
current_node.previous.next = current_node.next
def replace(new_val):
current_node_record[0].replace_val(new_val)
def iterate():
current_node = current_node_record[0]
if current_node is None:
current_node = self._head
else:
current_node = current_node.next
if current_node is None:
return
current_node_record[0] = current_node
yield current_node.value
return remove, replace, iterate()
class EntityTreeNode:
def __init__(self):
self._children = {}
self.parent = None
self.contained_data = None
def back_trace(self):
if self.parent is None:
yield self
else:
yield from self.parent.back_trace()
def push(self, lemma_list, contained_data, lemma_list_index=0):
if len(lemma_list) == 0:
return
lemma_selected = lemma_list[lemma_list_index]
selected_node = None
if lemma_selected in self._children:
selected_node = self._children[lemma_selected]
else:
selected_node = EntityTreeNode()
self._children[lemma_selected] = selected_node
selected_node.parent = self
new_index = lemma_list_index + 1
if new_index == len(lemma_list):
selected_node.contained_data = contained_data
return
selected_node.push(lemma_list, contained_data, new_index)
def traverse(self, lemma):
if lemma in self._children:
return self._children[lemma]
else:
return None
class WalkableEntityTree:
def __init__(self):
self._root = None
self._potential_terms_queue = SimpleLinkedList()
def push(self, term, associated_data=True):
term_blob = TextBlob(term)
lemmas = [word.lemmatize().lower() for word in term_blob.words]
return self.push_lemmas(lemmas, associated_data)
def push_lemmas(self, lemmas, associated_data=True):
if self._root is None:
self._root = EntityTreeNode()
self._root.push(lemmas, associated_data)
return " ".join(lemmas)
def accept_lemma(self, lemma):
potential_branch = self._root.traverse(lemma)
new_branch_exists = potential_branch is not None
if new_branch_exists and potential_branch.contained_data is not None:
yield potential_branch
remover, replacer, iterator = self._potential_terms_queue.walk()
for branch in iterator:
new_branch = branch.traverse(lemma)
if new_branch is None:
remover()
else:
replacer(new_branch)
if new_branch.contained_data is not None:
yield new_branch
if new_branch_exists:
self._potential_terms_queue.append(potential_branch)
def reset(self):
self._potential_terms_queue = SimpleLinkedList()