forked from adaamko/newpotato
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lsoie_parser_marina.py
53 lines (43 loc) · 1.38 KB
/
lsoie_parser_marina.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
import pickle
from collections import defaultdict
import stanza
from tuw_nlp.text.utils import gen_tsv_sens
# stanza.download("en")
def main(input_stream, outfile):
nlp = stanza.Pipeline(
lang="en",
processors="tokenize,mwt,pos,lemma,depparse",
tokenize_pretokenized=True,
)
stream = open(input_stream, "r", encoding="utf-8")
mydict = {}
iter = 0
for sen_idx, sen in enumerate(gen_tsv_sens(stream)):
iter += 1
print(f"processing sentence {sen_idx}")
parsed_doc = nlp(" ".join(t[1] for t in sen))
parsed_sen = parsed_doc.sentences[0]
args = defaultdict(list)
sent = []
pred = []
for i, tok in enumerate(sen):
sent.append(tok[1])
label = tok[7].split("-")[0]
if label == "O":
continue
elif label == "P":
pred.append(i)
continue
args[label].append(i)
mydict[sen_idx] = {"sent": sent, "pred": pred, "args": args}
if iter == 20:
break
with open(outfile, "wb") as f:
pickle.dump(mydict, f)
print("dictionary saved successfully to file")
# with open(outfile, "rb") as f:
# loaded_dict = pickle.load(f)
if __name__ == "__main__":
input_stream = "lsoie_wiki_dev.conll"
outfile = "sample.pkl"
main(input_stream, outfile)