-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuildSenten.py
196 lines (164 loc) · 5.11 KB
/
buildSenten.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# -*- coding: utf-8 -*-
"""
Created on Thu Jan 16 08:38:44 2020
@author: Lenovo T420s
"""
from lxml import etree
import spacy
import localisationAndParam as locp
segmentLoc = locp.__groundT__ / "segments"
wordsLoc = locp.__groundT__ / "words"
""" Extraction"""
def onePortionExtractor(part, speaker ):
"""
segmentName = Meeting + part + "." + speaker + ".segments.xml"
print(segmentName)
segmentTree = etree.parse(str(segmentLoc / segmentName))
"""
cpt = 0
sentenses = {}
wordsName = locp.__Meeting + part + "." + speaker + ".words.xml"
wordsTree = etree.parse(str(wordsLoc / wordsName))
begin = True
s = ""
for w in wordsTree.xpath("w"):
if begin:
begin = False
b = w.get("starttime")
s += w.text + " "
if w.get("punc") == "true" and (w.text in [".","!","?"]) :
sentenses[cpt] = [float(b),float(w.get("endtime")),s]
s = ""
begin = True
cpt += 1
return (sentenses)
def onePortionExtractorBis(part, speaker,meeting = locp.__Meeting ):
"""
segmentName = Meeting + part + "." + speaker + ".segments.xml"
print(segmentName)
segmentTree = etree.parse(str(segmentLoc / segmentName))
"""
cpt = 0
sentenses = {}
wordsName = meeting + part + "." + speaker + ".words.xml"
wordsTree = etree.parse(str(wordsLoc / wordsName))
s = ""
for w in wordsTree.xpath("w"):
b = w.get("starttime")
s = w.text
cpt += 1
sentenses[cpt] = [float(b),float(w.get("endtime")),s]
cpt += 1
return (sentenses)
def partExtractor(partName,speakers, meeting = locp.__Meeting):
part = {}
for spker in speakers:
part[spker] = onePortionExtractorBis(partName,spker,meeting)
return part
def sentensesAllSort(part):
def valueOfElem(e):
return(e[1])
totalSp = []
for speaker, speach in part.items():
for sent in speach.values():
totalSp.append( [speaker] + sent)
res = sorted(totalSp,key = valueOfElem)
return res
""" Part of speach detection"""
def groupSpeach(sortSents):
res = []
spker = sortSents[0][0]
save = sortSents[0][3]
b = sortSents[0][1]
f = sortSents[0][2]
for s in sortSents:
if s[0] != spker:
res.append((spker,b,f,save))
spker = s[0]
save = s[3]
b = s[1]
f = s[2]
else:
if not(s[3] in [",",".","!","?"]):
save += " "
save += s[3]
f = s[2]
res.append((spker,b,s[1],save))
return res
def detectMonologue(grSents,minTime):
monologues = []
for s in grSents:
if s[2]- s[1] > minTime:
monologues.append(s)
return monologues
"""
def interuption(ordSP, minTime):
return 1
def dialogue(ordSP,minTime):
monologues = []
spker = [sortSents[0][0]]
save = [sortSents[0][3]]
b = { }
for s in sortSents:
if s[0] != spker:
if (s[1] - b) > minTime:
monologues.append((spker,b,s[1],save))
spker = s[0]
save = [s[3]]
b = s[1]
else:
save.append(s[3])
if sortSents[-1][1] - b > minTime:
monologues.append((spker,b,s[1],save))
return monologues
"""
"""sementics"""
def querryIdent(grSents):
nlp = spacy.load("en_core_web_lg")
qw = [[],[]]
for sent in grSents:
docSent = nlp(sent[3])
for sent in docSent.sents:
doc = nlp(sent.text)
q = False
Subjfirst = False
VerbFirst = False
sub = False
verb = False
for token in doc:
q = token.text == "?" or q
if token.dep_ == "nsubj" and not sub :
Subjfirst = not VerbFirst
sub = True
elif token.dep_ == "ROOT" and not verb:
VerbFirst = not Subjfirst
verb = True
if (VerbFirst and (sub and verb)):
qw[0].append(sent.text)
elif (q and sub and verb) :
qw[1].append(sent.text)
return qw
def speachToSent(sp):
sents= []
s = ""
nlp = spacy.load("en_core_web_lg")
doc = nlp(sp)
for token in doc:
s += token.text + " "
if token.text in [".","!","?"]:
sents.append(s)
s = ""
if s != "":
sents.append(s)
return sents
def organisedSpeachTotalPipe(partOfSpeach = "a", meeting = locp.__Meeting):
return groupSpeach(sentensesAllSort(partExtractor(partOfSpeach,locp.__seapkers, meeting)))
"""
p = partExtractor("a",locp.__seapkers)
o = sentensesAllSort(p)
m = groupSpeach(o)
print(detectMonologue(m,100))
res = querryIdent(m)
print(res[0])
print(res[1])
"""