-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask2work.py
More file actions
129 lines (107 loc) · 3.99 KB
/
Copy pathtask2work.py
File metadata and controls
129 lines (107 loc) · 3.99 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
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
# WORK(Person, Organization, Position, Location)
def parse_work_template(doc):
temp_PVJO = False
temp_JOVP = False
for i,tok in enumerate(doc):
if tok.dep_=='nsubj':
if tok.ent_type_ == 'PERSON':
temp_PVJO =True
elif tok.ent_type_ == 'Job-Title' or tok.ent_type_ == 'ORG':
temp_JOVP =True
print(temp_PVJO)
compound_noun = ''
person= ''
prev_per=''
job_titles = ''
org = ''
place = ''
results = []
row = []
# iterate through all the tokens in the input sentence
for i,tok in enumerate(doc):
if temp_PVJO:
# extract subject
if (tok.dep_== "nsubj" or tok.ent_type_ =='PERSON') and tok.head.dep_ =='ROOT' :
if person =='':
person = compound_noun + ' ' + tok.text
elif job_titles!='' and org !='':
row.append(person)
row.append(job_titles)
row.append(org)
row.append(place)
results.append(row)
row=[]
prev_per=person
person = ''
org = ''
job_titles = ''
place=''
else:
person = compound_noun + ' ' + tok.text
if tok.pos_ == 'PUNCT' and tok.text == ';':
row.append(person)
row.append(job_titles)
row.append(org)
row.append(place)
results.append(row)
row=[]
prev_per=person
person = ''
org = ''
job_titles = ''
place=''
# extract Job-title
if tok.ent_type_ =='Job-Title':
if person == '':
person = prev_per
if job_titles == '':
job_titles = compound_noun + ' ' + tok.text
else:
job_titles = job_titles+',' + compound_noun + ' ' + tok.text
# extract organisation
if tok.ent_type_ == 'ORG':
if org=='':
org = compound_noun +' '+tok.text
elif compound_noun!='':
org = compound_noun + " " +tok.text
else:
org = org+ " " +tok.text
#extract place
if tok.ent_type_ == 'GPE' and (tok.head.ent_type_ == 'ORG' or tok.head.pos_ == 'ADP'):
place = tok.text
if tok.dep_ == 'compound':
compound_noun = compound_noun+ ' ' + tok.text
else:
compound_noun = ''
else:
# extract subject
if tok.dep_== "nsubj" and tok.ent_type_ =='Job-Title' :
if job_titles == '':
job_titles = compound_noun + ' ' + tok.text
else:
job_titles = job_titles+',' + compound_noun + ' ' + tok.text
# extract Person
if tok.ent_type_ =='PERSON' and tok.head.dep_ == 'ROOT':
if person =='':
person = compound_noun + ' ' + tok.text
else:
person = person+ ' '+tok.text
# extract organisation
if tok.ent_type_ == 'ORG':
if org=='':
org = compound_noun +' '+tok.text
else:
org = org +" " +tok.text
#extract place
if tok.ent_type_ == 'GPE' and (tok.head.ent_type_ == 'ORG' or tok.head.pos_ == 'ADP'):
place = tok.text
if tok.dep_ == 'compound':
compound_noun = compound_noun+ ' ' + tok.text
else:
compound_noun = ''
row.append(person)
row.append(job_titles)
row.append(org)
row.append(place)
results.append(row)
return results