/
unknownmodel.py
189 lines (167 loc) · 5.16 KB
/
unknownmodel.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
"""
Code copied/adapted from Andreas van Cranenburgh's discodop.
"""
import re
UNK = '_UNK'
# === functions for unknown word signatures ============
HASDIGIT = re.compile(r"\d", re.UNICODE)
HASNONDIGIT = re.compile(r"\D", re.UNICODE)
# NB: includes '-', hyphen, non-breaking hyphen
# does NOT include: figure-dash, em-dash, en-dash (these are punctuation,
# not word-combining) u2012-u2015; nb: these are hex values.
HASDASH = re.compile("[-\u2010\u2011]")
# FIXME: exclude accented characters for model 6?
HASLOWER = re.compile('[a-z\xe7\xe9\xe0\xec\xf9\xe2\xea\xee\xf4\xfb\xeb'
'\xef\xfc\xff\u0153\xe6]')
HASUPPER = re.compile('[A-Z\xc7\xc9\xc0\xcc\xd9\xc2\xca\xce\xd4\xdb\xcb'
'\xcf\xdc\u0178\u0152\xc6]')
HASLETTER = re.compile('[A-Za-z\xe7\xe9\xe0\xec\xf9\xe2\xea\xee\xf4\xfb'
'\xeb\xef\xfc\xff\u0153\xe6\xc7\xc9\xc0\xcc\xd9\xc2\xca\xce\xd4'
'\xdb\xcb\xcf\xdc\u0178\u0152\xc6]')
# Cf. http://en.wikipedia.org/wiki/French_alphabet
LOWER = ('abcdefghijklmnopqrstuvwxyz\xe7\xe9\xe0\xec\xf9\xe2\xea\xee\xf4\xfb'
'\xeb\xef\xfc\xff\u0153\xe6')
UPPER = ('ABCDEFGHIJKLMNOPQRSTUVWXYZ\xc7\xc9\xc0\xcc\xd9\xc2\xca\xce\xd4\xdb'
'\xcb\xcf\xdc\u0178\u0152\xc6')
LOWERUPPER = LOWER + UPPER
def unknownword6(word, loc, lexicon):
"""
Model 6 of the Stanford parser (for WSJ treebank).
:param word: the actual word observed
:param loc: the 0-based position in the sentence
:param lexicon: the known terminals
:return: signature
>>> lexicon = set('This is a short sentence .'.split())
>>> unknownword6('this', 0, lexicon)
'_UNK-LC'
>>> unknownword6('this', 1, lexicon)
'_UNK-LC'
>>> unknownword6('Sentence', 0, lexicon)
'_UNK-INITC-KNOWNLC'
>>> unknownword6('Sentence', 1, lexicon)
'_UNK-CAP'
>>> unknownword6('annoying', 1, lexicon)
'_UNK-LC-ing'
"""
wlen = len(word)
numcaps = 0
sig = UNK
numcaps = len(HASUPPER.findall(word))
lowered = word.lower()
if numcaps > 1:
sig += "-CAPS"
elif numcaps > 0:
if loc == 0:
sig += "-INITC"
if lowered in lexicon:
sig += "-KNOWNLC"
else:
sig += "-CAP"
elif HASLOWER.search(word):
sig += "-LC"
if HASDIGIT.search(word):
sig += "-NUM"
if HASDASH.search(word):
sig += "-DASH"
if lowered.endswith('s') and wlen >= 3:
if lowered[-2] not in 'siu':
sig += '-s'
elif wlen >= 5 and not HASDASH.search(word) and not (HASDIGIT.search(word) and numcaps > 0):
suffixes = ('ed', 'ing', 'ion', 'er', 'est', 'ly', 'ity', 'y', 'al')
for a in suffixes:
if lowered.endswith(a):
sig += "-%s" % a
break
return sig
def unknownword4(word, loc, lexicon=set()):
"""
Model 4 of the Stanford parser. Relatively language agnostic.
:param word: the actual word observed
:param loc: the 0-based position in the sentence
:param lexicon: the known terminals (ignored in this version)
:return: signature
>>> unknownword4('this', 0)
'_UNK-L-is'
>>> unknownword4('this', 1)
'_UNK-L-is'
>>> unknownword4('Sentence', 0)
'_UNK-SC-ce'
>>> unknownword4('Sentence', 1)
'_UNK-C-ce'
>>> unknownword4('annoying', 1)
'_UNK-L-ng'
"""
sig = UNK
# letters
if word and word[0] in UPPER:
if not HASLOWER.search(word):
sig += "-AC"
elif loc == 0:
sig += "-SC"
else:
sig += "-C"
elif HASLOWER.search(word):
sig += "-L"
elif HASLETTER.search(word):
sig += "-U"
else:
sig += "-S" # no letter
# digits
if HASDIGIT.search(word):
if HASNONDIGIT.search(word):
sig += "-n"
else:
sig += "-N"
# punctuation
if "-" in word:
sig += "-H"
if "." in word:
sig += "-P"
if "," in word:
sig += "-C"
if len(word) > 3:
if word[-1] in LOWERUPPER:
sig += "-%s" % word[-2:].lower()
return sig
def unknownwordbase(word, loc=0, lexicon=set()):
"""
BaseUnknownWordModel of the Stanford parser.
Relatively language agnostic.
:param word: the actual word observed
:param loc: the 0-based position in the sentence (ignored in this version)
:param lexicon: the known terminals (ignored in this version)
:return: signature
>>> unknownwordbase('this')
'_UNK-c-is'
>>> unknownwordbase('this')
'_UNK-c-is'
>>> unknownwordbase('Sentence')
'_UNK-C-ce'
>>> unknownwordbase('Sentence')
'_UNK-C-ce'
>>> unknownwordbase('annoying')
'_UNK-c-ng'
"""
sig = UNK
# letters
if word[0] in UPPER:
sig += "-C"
else:
sig += "-c"
# digits
if HASDIGIT.search(word):
if HASNONDIGIT.search(word):
sig += "-n"
else:
sig += "-N"
# punctuation
if "-" in word:
sig += "-H"
if word == ".":
sig += "-P"
if word == ",":
sig += "-C"
if len(word) > 3:
if word[-1] in LOWERUPPER:
sig += "-%s" % word[-2:].lower()
return sig