forked from eliorsulem/SAMSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscene_sentence_extraction.py
58 lines (45 loc) · 1.51 KB
/
scene_sentence_extraction.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
from ucca import layer0, layer1, convert, core
from xml.etree.ElementTree import ElementTree, tostring, fromstring
import nltk
def get_scenes(P):
"""
P is a ucca passage. Return all the scenes in each passage
"""
scenes = [x for x in P.layer("1").all if x.tag == "FN" and x.is_scene()]
y = P.layer("0")
output = []
for sc in scenes:
p = []
d = sc.get_terminals(False,True)
for i in list(range(0,len(d))):
p.append(d[i].position)
output2 = []
for k in p:
if(len(output2)) == 0:
output2.append(str(y.by_position(k)))
elif str(y.by_position(k)) != output2[-1]:
output2.append(str(y.by_position(k)))
output.append(output2)
return(output)
def get_sentences(P):
"""
P is the output of the simplification system. Return all the sentences in each passage
"""
dirpath = '/Mypath/System_output'
folder = nltk.data.find(dirpath)
corpusReader = nltk.corpus.PlaintextCorpusReader(folder, P)
d = len(corpusReader.sents())
return (corpusReader.sents()[:d])
index = list(range(0,100))
for t in index:
f1 = open('UCCAannotated_source/%s.xml' %t)
xml_string1 = f1.read()
f1.close()
xml_object1 = fromstring(xml_string1)
P1 = convert.from_site(xml_object1)
L1 = get_scenes(P1)
L2 = get_sentences('%s.txt' %t)
s = open('s%s.txt' %t, 'w')
s.write('%s\n' %L1)
s.write('%s\n' %L2)
s.close()