-
Notifications
You must be signed in to change notification settings - Fork 6
/
reg_train.py
161 lines (134 loc) · 6.61 KB
/
reg_train.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
__author__ = ''
"""
Author: ANONYMOUS
Date: 08/08/2017
Description:
Compute frequency of referring expressions for each reference condition
PYTHON VERSION :2.7
DEPENDENCIES:
cPickle
NLTK: http://www.nltk.org/
UPDATE CONSTANT PATHS:
TRAIN_REFEX_PATH: training referring expressions path
MODEL_PATH: path to save the trained model
"""
import cPickle as p
import json
import nltk
import sys
sys.path.append('../')
TRAIN_REFEX_PATH = '../data/train/data.json'
MODEL_PATH = 'reg.cPickle'
lemma = {
'he':'he', 'his':'he', 'him': 'he',
'she':'she', 'her':'she', 'hers':'she',
'it':'it', 'its':'it',
'we':'we', 'our':'we', 'ours':'we', 'us':'we',
'they':'they', 'their':'they', 'theirs':'they', 'them':'they'
}
pronouns, names, descriptions, demonstratives = {}, {}, {}, {}
# References extracted on preprocessing
references = json.load(open(TRAIN_REFEX_PATH))
# Retrieve all wiki entities and normalize their names
entities = set()
for reference in references:
entities = entities.union([reference['entity']])
print 'Number of entities: ', len(list(entities))
for entity in entities:
print 'Entity: ', entity
pronouns[entity] = []
# backoff properties
bnames, bdescriptions, bdemonstratives = [], [], []
for syntax in ['np-subj', 'np-obj', 'subj-det']:
for text_status in ['new', 'given']:
for sentence_status in ['new', 'given']:
reference = filter(lambda x: x['entity'] == entity and x['syntax'] == syntax and x['text_status'] == text_status and x['sentence_status'] == sentence_status, references)
if (syntax, text_status, sentence_status, entity) not in names:
names[(syntax, text_status, sentence_status, entity)] = []
if (syntax, text_status, sentence_status, entity) not in descriptions:
descriptions[(syntax, text_status, sentence_status, entity)] = []
if (syntax, text_status, sentence_status, entity) not in demonstratives:
demonstratives[(syntax, text_status, sentence_status, entity)] = []
if len(reference) > 0:
for refex in reference:
reftype = refex['reftype']
reg = refex['refex'].strip().lower()
if reftype == 'pronoun' and reg.replace('eos', '').strip() in lemma:
prn = reg.replace('eos', '').strip()
pronouns[entity].append(lemma[prn])
elif reftype == 'name':
names[(syntax, text_status, sentence_status, entity)].append(reg)
elif reftype == 'description':
descriptions[(syntax, text_status, sentence_status, entity)].append(reg)
elif reftype == 'demonstrative':
demonstratives[(syntax, text_status, sentence_status, entity)].append(reg)
if len(names[(syntax, text_status, sentence_status, entity)]) == 0:
bnames.append((syntax, text_status, sentence_status, entity))
if len(descriptions[(syntax, text_status, sentence_status, entity)]) == 0:
bdescriptions.append((syntax, text_status, sentence_status, entity))
if len(demonstratives[(syntax, text_status, sentence_status, entity)]) == 0:
bdemonstratives.append((syntax, text_status, sentence_status, entity))
# First backoff (sentence status off)
reference = filter(lambda x: x['entity'] == entity and x['syntax'] == syntax and x['text_status'] == text_status, references)
if len(reference) > 0:
for refex in reference:
reftype = refex['reftype']
reg = refex['refex'].strip().lower()
if reftype == 'name':
for key in bnames:
names[key].append(reg)
bnames = []
elif reftype == 'description':
for key in bdescriptions:
descriptions[key].append(reg)
bdescriptions = []
elif reftype == 'demonstrative':
for key in bdemonstratives:
demonstratives[key].append(reg)
bdemonstratives = []
# Second backoff (text status off)
reference = filter(lambda x: x['entity'] == entity and x['syntax'] == syntax, references)
if len(reference) > 0:
for refex in reference:
reftype = refex['reftype']
reg = refex['refex'].strip().lower()
if reftype == 'name':
for key in bnames:
names[key].append(reg)
bnames = []
elif reftype == 'description':
for key in bdescriptions:
descriptions[key].append(reg)
bdescriptions = []
elif reftype == 'demonstrative':
for key in bdemonstratives:
demonstratives[key].append(reg)
bdemonstratives = []
# Third backoff (syntax off)
reference = filter(lambda x: x['entity'] == entity, references)
if len(reference) > 0:
for refex in reference:
reftype = refex['reftype']
reg = refex['refex'].strip().lower()
if reftype == 'name':
for key in bnames:
names[key].append(reg)
bnames = []
elif reftype == 'description':
for key in bdescriptions:
descriptions[key].append(reg)
bdescriptions = []
elif reftype == 'demonstrative':
for key in bdemonstratives:
demonstratives[key].append(reg)
bdemonstratives = []
for entity in pronouns:
pronouns[entity] = sorted(nltk.FreqDist(pronouns[entity]).items(), key=lambda x:x[1], reverse=True)[:2]
for key in names:
names[key] = sorted(nltk.FreqDist(names[key]).items(), key=lambda x:x[1], reverse=True)[:2]
for key in descriptions:
descriptions[key] = sorted(nltk.FreqDist(descriptions[key]).items(), key=lambda x:x[1], reverse=True)[:2]
for key in demonstratives:
demonstratives[key] = sorted(nltk.FreqDist(demonstratives[key]).items(), key=lambda x:x[1], reverse=True)[:2]
references = {'pronouns':pronouns, 'names':names, 'descriptions':descriptions, 'demonstratives':demonstratives}
p.dump(references, open(MODEL_PATH, 'w'))