-
Notifications
You must be signed in to change notification settings - Fork 0
/
IsAResolver.cs
223 lines (205 loc) · 8.08 KB
/
IsAResolver.cs
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
//Copyright (C) 2006 Richard J. Northedge
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//This file is based on the IsAResolver.java source file found in the
//original java implementation of OpenNLP. That source file contains the following header:
//Copyright (C) 2003 Thomas Morton
//
//This library is free software; you can redistribute it and/or
//modify it under the terms of the GNU Lesser General Public
//License as published by the Free Software Foundation; either
//version 2.1 of the License, or (at your option) any later version.
//
//This library is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU Lesser General Public License for more details.
//
//You should have received a copy of the GNU Lesser General Public
//License along with this program; if not, write to the Free Software
//Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
using System;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using DiscourseEntity = OpenNLP.Tools.Coreference.DiscourseEntity;
using MentionContext = OpenNLP.Tools.Coreference.Mention.MentionContext;
namespace OpenNLP.Tools.Coreference.Resolver
{
/// <summary> Resolves coreference between appositives. </summary>
public class IsAResolver:MaximumEntropyResolver
{
internal Regex predicativePattern;
public IsAResolver(string projectName, ResolverMode mode):base(projectName, "/imodel", mode, 20)
{
ShowExclusions = false;
//predicativePattern = Pattern.compile("^(,|am|are|is|was|were|--)$");
predicativePattern = new Regex("^(,|--)$", RegexOptions.Compiled);
}
public IsAResolver(string projectName, ResolverMode mode, INonReferentialResolver nonReferentialResolver) : base(projectName, "/imodel", mode, 20, nonReferentialResolver)
{
ShowExclusions = false;
//predicativePattern = Pattern.compile("^(,|am|are|is|was|were|--)$");
predicativePattern = new Regex("^(,|--)$", RegexOptions.Compiled);
}
public override bool CanResolve(MentionContext context)
{
if (PartsOfSpeech.IsNoun(context.HeadTokenTag))
{
return (context.PreviousToken != null && predicativePattern.IsMatch(context.PreviousToken.ToString()));
}
return false;
}
protected internal override bool IsExcluded(MentionContext context, DiscourseEntity discourseEntity)
{
var currentContext = discourseEntity.LastExtent;
if (context.SentenceNumber != currentContext.SentenceNumber)
{
return true;
}
//shallow parse appositives
if (currentContext.IndexSpan.End == context.IndexSpan.Start - 2)
{
return false;
}
//full parse w/o trailing comma
if (currentContext.IndexSpan.End == context.IndexSpan.End)
{
return false;
}
//full parse w/ trailing comma or period
if (currentContext.IndexSpan.End <= context.IndexSpan.End + 2 && (context.NextToken != null
&& (context.NextToken.ToString() == PartsOfSpeechStrings.Comma || context.NextToken.ToString() == PartsOfSpeechStrings.SentenceFinalPunctuation)))
{
return false;
}
return true;
}
protected internal override bool IsOutOfRange(MentionContext context, DiscourseEntity discourseEntity)
{
var currentContext = discourseEntity.LastExtent;
return (currentContext.SentenceNumber != context.SentenceNumber);
}
protected internal override bool defaultReferent(DiscourseEntity discourseEntity)
{
return true;
}
protected internal override List<string> GetFeatures(MentionContext mention, DiscourseEntity entity)
{
var features = base.GetFeatures(mention, entity);
if (entity != null)
{
var ant = entity.LastExtent;
var leftContexts = GetContextFeatures(ant);
for (int ci = 0, cn = leftContexts.Count; ci < cn; ci++)
{
features.Add("l" + leftContexts[ci]);
}
var rightContexts = GetContextFeatures(mention);
for (int ci = 0, cn = rightContexts.Count; ci < cn; ci++)
{
features.Add("r" + rightContexts[ci]);
}
features.Add("hts" + ant.HeadTokenTag + "," + mention.HeadTokenTag);
}
/*
if (entity != null) {
//previous word and tag
if (ant.prevToken != null) {
features.add("pw=" + ant.prevToken);
features.add("pt=" + ant.prevToken.getSyntacticType());
}
else {
features.add("pw=<none>");
features.add("pt=<none>");
}
//next word and tag
if (mention.nextToken != null) {
features.add("nw=" + mention.nextToken);
features.add("nt=" + mention.nextToken.getSyntacticType());
}
else {
features.add("nw=<none>");
features.add("nt=<none>");
}
//modifier word and tag for c1
int i = 0;
List c1toks = ant.tokens;
for (; i < ant.headTokenIndex; i++) {
features.add("mw=" + c1toks.get(i));
features.add("mt=" + ((Parse) c1toks.get(i)).getSyntacticType());
}
//head word and tag for c1
features.add("mh=" + c1toks.get(i));
features.add("mt=" + ((Parse) c1toks.get(i)).getSyntacticType());
//modifier word and tag for c2
i = 0;
List c2toks = mention.tokens;
for (; i < mention.headTokenIndex; i++) {
features.add("mw=" + c2toks.get(i));
features.add("mt=" + ((Parse) c2toks.get(i)).getSyntacticType());
}
//head word and tag for n2
features.add("mh=" + c2toks.get(i));
features.add("mt=" + ((Parse) c2toks.get(i)).getSyntacticType());
//word/tag pairs
for (i = 0; i < ant.headTokenIndex; i++) {
for (int j = 0; j < mention.headTokenIndex; j++) {
features.add("w=" + c1toks.get(i) + "|" + "w=" + c2toks.get(j));
features.add("w=" + c1toks.get(i) + "|" + "t=" + ((Parse) c2toks.get(j)).getSyntacticType());
features.add("t=" + ((Parse) c1toks.get(i)).getSyntacticType() + "|" + "w=" + c2toks.get(j));
features.add("t=" + ((Parse) c1toks.get(i)).getSyntacticType() + "|" + "t=" + ((Parse) c2toks.get(j)).getSyntacticType());
}
}
features.add("ht=" + ant.headTokenTag + "|" + "ht=" + mention.headTokenTag);
features.add("ht1=" + ant.headTokenTag);
features.add("ht2=" + mention.headTokenTag);
*/
//semantic categories
/*
if (ant.neType != null) {
if (re.neType != null) {
features.add("sc="+ant.neType+","+re.neType);
}
else if (!PartsOfSpeech.IsProperNoun(re.headTokenTag) && PartsOfSpeech.IsNoun(re.headTokenTag)) {
Set synsets = re.synsets;
for (Iterator si=synsets.iterator();si.hasNext();) {
features.add("sc="+ant.neType+","+si.next());
}
}
}
else if (!PartsOfSpeech.IsProperNoun(ant.headTokenTag) && PartsOfSpeech.IsNoun(ant.headTokenTag)) {
if (re.neType != null) {
Set synsets = ant.synsets;
for (Iterator si=synsets.iterator();si.hasNext();) {
features.add("sc="+re.neType+","+si.next());
}
}
else if (!PartsOfSpeech.IsProperNoun(re.headTokenTag) && PartsOfSpeech.IsNoun(re.headTokenTag)) {
Set synsets1 = ant.synsets;
Set synsets2 = re.synsets;
for (Iterator si=synsets1.iterator();si.hasNext();) {
Object synset = si.next();
if (synsets2.contains(synset)) {
features.add("sc="+synset);
}
}
}
}
}
*/
return features;
}
}
}