-
Notifications
You must be signed in to change notification settings - Fork 0
/
wernicke.py
138 lines (111 loc) · 4.17 KB
/
wernicke.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
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
#wernicke.py
#non-native libraries
import sys
#import opennre
import hashlib
import time
import inflect
#adjust system path to include Language folder
sys.path.insert(0, './Language/STClassifier')
sys.path.insert(0, './Language')
#import native generator libraries
import STGenerator
import spacyGenerator
import AdjectiveCorpex
def construct_empty_sentence_frame():
sentence_frame = {
"sent_type_pred": None,
"plaintext": None,
"emotional_charge_vector": None,
"entities": None,
"tokens": None,
"chunks": None,
'speaker': None,
'tense': None
}
return(sentence_frame)
def resolveIndices(text, words):
a = text.index(words[0])
b = a + len(words[0])
c = text.index(words[1])
d = c + len(words[1])
return([a,b,c,d])
def detect_past_sentece(sentence):
sent = list(nlp(sentence).sents)[0]
return (
sent.root.tag_ == "VBD" or
any(w.dep_ == "aux" and w.tag_ == "VBD" for w in sent.root.children))
#concatenate text and semantic form, encode, and then md5 hash
def create_hash_id(wtext, wform):
q = hashlib.md5((wtext+wform).encode()).hexdigest()
return(q)
def runnable():
valid_corpexed = ['JJ', 'advmod', 'adj', 'WRB' 'JJ', 'JJR', 'JJS', 'advmod']
print("< ------- Wernicke Area Initializing ------ >")
#Initialize Sentence Type Generator
STGen = STGenerator.STClassGen()
#Wait until generator is alive
while(next(STGen)!=True):
time.sleep(0.1)
print("< ------- Sentence Type Gen Online ------ >")
#Initialize Spacy Generator
spGen = spacyGenerator.chunkGenerator()
#wait until generator is alive
while(next(spGen)!=True):
time.sleep(0.1)
print("< ------- Spacy Gen Online ------ >")
#Flush generator lines
currentSTPred = next(STGen)
currentspGen = next(spGen)
yield(True)
while(True):
#insert sentence
sentence = yield
#print(sentence)
print("< ------- Wernicke Looping ------ >")
sentence_frame = construct_empty_sentence_frame()
if(sentence is not None):
speaker = sentence[0]
#get next from each generator
next(STGen)
next(spGen)
f = sentence[1]
#ship input to generators
currentSTPred = STGen.send(f)
currentspGen = spGen.send(f)
#print(currentSTPred)
#[entities, tokens, chunks]
emotional_charge_vector = []
if(currentspGen!=None):
for x in range(0, len(currentspGen[1])):
if(currentspGen[1][x][1] == 'JJ' or currentspGen[1][x][1] == 'VBP' or currentspGen[1][x][1] == 'VBP'):
echarge = int(AdjectiveCorpex.binarySearch(currentspGen[1][x][0]))
emotional_charge_vector.append(echarge)
else:
emotional_charge_vector.append(0)
if(len(currentspGen[2])>=2):
q = resolveIndices(f, [str(currentspGen[2][0]), str(currentspGen[2][1])])
else:
pass
#construct sentence frame
#sentence_frame ["text"]
if(currentspGen!=None and currentSTPred!=None):
sentence_frame['sent_type_pred'] = currentSTPred[0]
sentence_frame['emotional_charge_vector'] = emotional_charge_vector
sentence_frame['plaintext'] = currentspGen[3]
sentence_frame['entities'] = currentspGen[0]
sentence_frame['tokens'] = currentspGen[1]
#print(sentence_frame['tokens'])
sentence_frame['chunks'] = currentspGen[2]
sentence_frame['speaker'] = speaker
sentence_frame['tense'] = currentspGen[4]
yield(sentence_frame)
print("<-- Wernicke Loop complete -->")
else:
pass
#for adj in chnked text, apply AdjectiveCorpex
#tense, charge
#time spoken: y
#words: x
#tense: y
#Initialize Spacy Generator