-
-
Notifications
You must be signed in to change notification settings - Fork 133
Expand file tree
/
Copy pathmisc.py
More file actions
45 lines (40 loc) · 1.52 KB
/
Copy pathmisc.py
File metadata and controls
45 lines (40 loc) · 1.52 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
# -*- coding: utf-8 -*-
"""
Created on Wed Jul 31 10:46:13 2019
@author: WT
"""
import os
import pickle
import re
from itertools import permutations
from pathlib import Path
def load_pickle(filename):
completeName = os.path.join("./data/",\
filename)
with open(completeName, 'rb') as pkl_file:
data = pickle.load(pkl_file)
return data
def save_as_pickle(filename, data):
completeName = os.path.join("./data/",\
filename)
Path.mkdir(Path(completeName).parent, exist_ok=True)
with open(completeName, 'wb') as output:
pickle.dump(data, output)
def get_subject_objects(sent_):
### get subject, object entities by dependency tree parsing
#sent_ = next(sents_doc.sents)
root = sent_.root
subject = None; objs = []; pairs = []
for child in root.children:
#print(child.dep_)
if child.dep_ in ["nsubj", "nsubjpass"]:
if len(re.findall("[a-z]+",child.text.lower())) > 0: # filter out all numbers/symbols
subject = child; #print('Subject: ', child)
elif child.dep_ in ["dobj", "attr", "prep", "ccomp"]:
objs.append(child); #print('Object ', child)
if (subject is not None) and (len(objs) > 0):
for a, b in permutations([subject] + [obj for obj in objs], 2):
a_ = [w for w in a.subtree]
b_ = [w for w in b.subtree]
pairs.append((a_[0] if (len(a_) == 1) else a_ , b_[0] if (len(b_) == 1) else b_))
return pairs