-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedListModel.py
More file actions
89 lines (71 loc) · 2.65 KB
/
LinkedListModel.py
File metadata and controls
89 lines (71 loc) · 2.65 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
from textblob import TextBlob
from textblob import Word as blob_word
from textblob import Sentence as blob_sentence
class LinkedText:
def __init__(self, text_blob: TextBlob):
self.sentences = [LinkedSentence(sentence, index) for index, sentence in enumerate(text_blob.sentences)]
for sentence_a, sentence_b in zip(self.sentences[:-1], self.sentences[1:]):
sentence_a.next = sentence_b
sentence_b.previous = sentence_a
index = 0
for sentence in self.sentences:
for word in sentence.words:
word.index = index
index += 1
def __iter__(self):
return iter(self.sentences)
def __len__(self):
return len(self.sentences)
def __getitem__(self, item):
return self.sentences[item]
class LinkedSentence:
def __init__(self, sentence: blob_sentence, parent_index):
self.words = [LinkedWord(word, self, i) for i, word in enumerate(sentence.words)]
self.text = sentence.string
self.parent_index = parent_index
for word_a, word_b in zip(self.words[:-1], self.words[1:]):
word_a.next = word_b
word_b.previous = word_a
self.next = None
self.previous = None
def __iter__(self):
return iter(self.words)
def __len__(self):
return len(self.words)
def __getitem__(self, item):
return self.words[item]
class LinkedWord:
def __init__(self, word: blob_word, parent: LinkedSentence, parent_index):
self.lemma = word.lemmatize().lower()
self.next = None
self.previous = None
self.index = -1
self.parent_index = parent_index
self.parent = parent
@property
def after(self):
if self.next is not None:
return self.next
parent_next = self.parent.next
if parent_next is None:
return None
while len(parent_next) == 0:
parent_next = parent_next.next
if parent_next is None:
return None
return parent_next[0]
def __str__(self):
return self.lemma
def __repr__(self):
return self.lemma
def midpoint(word_a: LinkedWord, word_b: LinkedWord):
desired_index = (word_a.index + word_b.index) // 2
selected_parent = word_a.parent
while len(selected_parent) == 0:
selected_parent = selected_parent.next
while selected_parent[-1].index < desired_index:
selected_parent = selected_parent.next
end_offset = selected_parent[-1].index - desired_index
if len(selected_parent) < end_offset:
return selected_parent[0]
return selected_parent[end_offset - 1]