-
Notifications
You must be signed in to change notification settings - Fork 6
/
form_choice.py
67 lines (56 loc) · 2 KB
/
form_choice.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
__author__ = ''
"""
Author: ANONYMOUS
Date: 02/06/2017
Description:
Choice of referential form model
"""
import cPickle as p
import operator
from random import shuffle
DISTRIBUTIONS = p.load(open('pronoun_data/form_distributions.cPickle'))
# text-new -> name / text-old -> pronoun
def rule_form_choice(text_status):
if text_status == 'new':
return 'name'
else:
return 'pronoun'
def regular_bayes(references):
distributions = p.load(open('pronoun_data/form_distributions.cPickle'))
for reference in references:
X = (reference['syntax'], reference['text_status'], reference['sentence_status'])
form = sorted(distributions[X].items(), key=operator.itemgetter(1), reverse=True)[0][0]
reference['form'] = form
return references
def variation_bayes(references):
'''
Apply variation in the choice of referential with distributions from ACL 2016 model
:param references:
:return:
'''
distributions = p.load(open('pronoun_data/form_distributions.cPickle'))
def group():
g = {}
for reference in references:
X = (reference['syntax'], reference['text_status'], reference['sentence_status'])
if X not in g:
g[X] = {'distribution': distributions[X], 'references':[]}
g[X]['references'].append(reference)
return g
def choose_form(_references, distribution):
size = len(_references)
for form in distribution:
distribution[form] = size * distribution[form]
# print distribution
shuffle(_references)
for reference in _references:
form = sorted(distribution.items(), key=operator.itemgetter(1), reverse=True)[0][0]
reference['form'] = form
distribution[form] -= 1
return _references
groups = group()
references = []
for g in groups:
_references = choose_form(groups[g]['references'], groups[g]['distribution'])
references.extend(_references)
return references