-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathFwXmlTraceManager.cs
More file actions
503 lines (433 loc) · 19.6 KB
/
Copy pathFwXmlTraceManager.cs
File metadata and controls
503 lines (433 loc) · 19.6 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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
// Copyright (c) 2014-2021 SIL International
// This software is licensed under the LGPL, version 2.1 or later
// (http://www.gnu.org/licenses/lgpl-2.1.html)
using System.Diagnostics;
using System.Linq;
using System.Xml.Linq;
using SIL.LCModel;
using SIL.Machine.Morphology.HermitCrab;
using SIL.Machine.Morphology.HermitCrab.MorphologicalRules;
using SIL.Machine.Morphology.HermitCrab.PhonologicalRules;
using SIL.Machine.FeatureModel;
using SIL.Machine.Annotations;
using System.Collections.Generic;
using System.Text;
namespace SIL.FieldWorks.WordWorks.Parser
{
internal class FwXmlTraceManager : ITraceManager
{
private readonly LcmCache m_cache;
public FwXmlTraceManager(LcmCache fdoCache)
{
m_cache = fdoCache;
}
public bool IsTracing { get; set; }
public object GenerateWords(Language lang)
{
throw new System.NotImplementedException();
}
public void AnalyzeWord(Language lang, Word input)
{
input.CurrentTrace = new XElement("WordAnalysisTrace", new XElement("InputWord", input.Shape.ToString(lang.SurfaceStratum.CharacterDefinitionTable, true)));
}
public void BeginUnapplyStratum(Stratum stratum, Word input)
{
}
public void EndUnapplyStratum(Stratum stratum, Word output)
{
}
public void PhonologicalRuleUnapplied(IPhonologicalRule rule, int subruleIndex, Word input, Word output)
{
((XElement) output.CurrentTrace).Add(new XElement("PhonologicalRuleAnalysisTrace",
CreateHCRuleElement("PhonologicalRule", rule),
CreateWordElement("Input", input, true),
CreateWordElement("Output", output, true)));
}
public void PhonologicalRuleNotUnapplied(IPhonologicalRule rule, int subruleIndex, Word input)
{
((XElement) input.CurrentTrace).Add(new XElement("PhonologicalRuleAnalysisTrace",
CreateHCRuleElement("PhonologicalRule", rule),
CreateWordElement("Input", input, true),
CreateWordElement("Output", input, true)));
}
public void BeginUnapplyTemplate(AffixTemplate template, Word input)
{
((XElement) input.CurrentTrace).Add(new XElement("TemplateAnalysisTraceIn",
CreateHCRuleElement("AffixTemplate", template),
CreateWordElement("Input", input, true)));
}
public void EndUnapplyTemplate(AffixTemplate template, Word output, bool unapplied)
{
((XElement) output.CurrentTrace).Add(new XElement("TemplateAnalysisTraceOut",
CreateHCRuleElement("AffixTemplate", template),
CreateWordElement("Output", unapplied ? output : null, true)));
}
public void MorphologicalRuleUnapplied(IMorphologicalRule rule, int subruleIndex, Word input, Word output)
{
var trace = new XElement("MorphologicalRuleAnalysisTrace",
CreateMorphologicalRuleElement(rule));
var aprule = rule as AffixProcessRule;
if (aprule != null)
trace.Add(CreateAllomorphElement(aprule.Allomorphs[subruleIndex]));
trace.Add(CreateWordElement("Output", output, true));
((XElement) output.CurrentTrace).Add(trace);
output.CurrentTrace = trace;
}
public void MorphologicalRuleNotUnapplied(IMorphologicalRule rule, int subruleIndex, Word input)
{
}
public void LexicalLookup(Stratum stratum, Word input)
{
var trace = new XElement("LexLookupTrace",
new XElement("Stratum", stratum.Name),
new XElement("Shape", input.Shape.ToRegexString(stratum.CharacterDefinitionTable, true)));
((XElement) input.CurrentTrace).Add(trace);
}
public void SynthesizeWord(Language lang, Word input)
{
var trace = new XElement("WordSynthesisTrace",
CreateAllomorphElement(input.RootAllomorph),
new XElement("MorphologicalRules", input.MorphologicalRules.Select(CreateMorphologicalRuleElement)));
var curTrace = (XElement) input.CurrentTrace;
var lookupTrace = (XElement) curTrace.LastNode;
lookupTrace.Add(trace);
input.CurrentTrace = trace;
}
public void BeginApplyStratum(Stratum stratum, Word input)
{
}
public void NonFinalTemplateAppliedLast(Stratum stratum, Word word)
{
((XElement) word.CurrentTrace).Add(new XElement("FailureReason", new XAttribute("type", "nonFinalTemplate")));
}
public void ApplicableTemplatesNotApplied(Stratum stratum, Word word)
{
((XElement) word.CurrentTrace).Add(new XElement("FailureReason", new XAttribute("type", "noTemplatesApplied")));
}
public void EndApplyStratum(Stratum stratum, Word output)
{
}
public void PhonologicalRuleApplied(IPhonologicalRule rule, int subruleIndex, Word input, Word output)
{
((XElement) output.CurrentTrace).Add(new XElement("PhonologicalRuleSynthesisTrace",
CreateHCRuleElement("PhonologicalRule", rule),
// Show bracketed to make debugging phonological rules easier (fixes LT-18682).
CreateWordElement("Input", input, false, true),
CreateWordElement("Output", output, false, true)));
}
public void PhonologicalRuleNotApplied(IPhonologicalRule rule, int subruleIndex, Word input, FailureReason reason, object failureObj)
{
var pruleTrace = new XElement("PhonologicalRuleSynthesisTrace",
CreateHCRuleElement("PhonologicalRule", rule),
// Show bracketed to make debugging phonological rules easier (fixes LT-18682).
CreateWordElement("Input", input, false, true),
CreateWordElement("Output", input, false, true));
var rewriteRule = rule as RewriteRule;
if (rewriteRule != null)
{
RewriteSubrule sr = rewriteRule.Subrules[subruleIndex];
switch (reason)
{
case FailureReason.RequiredSyntacticFeatureStruct:
pruleTrace.Add(new XElement("FailureReason", new XAttribute("type", "category"),
new XElement("Category", input.SyntacticFeatureStruct.PartsOfSpeech().FirstOrDefault()),
new XElement("RequiredCategories", sr.RequiredSyntacticFeatureStruct.PartsOfSpeech()
.Select(pos => new XElement("Category", pos)))));
break;
case FailureReason.RequiredMprFeatures:
pruleTrace.Add(CreateMprFeaturesFailureElement(true, (MprFeatureGroup) failureObj, sr.RequiredMprFeatures, input));
break;
case FailureReason.ExcludedMprFeatures:
pruleTrace.Add(CreateMprFeaturesFailureElement(false, (MprFeatureGroup) failureObj, sr.ExcludedMprFeatures, input));
break;
}
}
((XElement) input.CurrentTrace).Add(pruleTrace);
}
private static XElement CreateMprFeaturesFailureElement(bool required, MprFeatureGroup group, MprFeatureSet feats, Word input)
{
return new XElement("FailureReason", new XAttribute("type", "mprFeatures"),
new XElement("MatchType", required ? "required" : "excluded"),
new XElement("Group", group),
new XElement("MprFeatures", input.MprFeatures.Where(mf => mf.Group == group).Select(f => new XElement("MprFeature", f))),
new XElement("ConstrainingMprFeatrues", feats.Where(mf => mf.Group == group).Select(f => new XElement("MprFeature", f))));
}
public void BeginApplyTemplate(AffixTemplate template, Word input)
{
((XElement) input.CurrentTrace).Add(new XElement("TemplateSynthesisTraceIn",
CreateHCRuleElement("AffixTemplate", template),
CreateWordElement("Input", input, false)));
}
public void EndApplyTemplate(AffixTemplate template, Word output, bool applied)
{
((XElement) output.CurrentTrace).Add(new XElement("TemplateSynthesisTraceOut",
CreateHCRuleElement("AffixTemplate", template),
CreateWordElement("Output", applied ? output : null, false)));
}
public void MorphologicalRuleApplied(IMorphologicalRule rule, int subruleIndex, Word input, Word output)
{
var trace = new XElement("MorphologicalRuleSynthesisTrace",
CreateMorphologicalRuleElement(rule));
var aprule = rule as AffixProcessRule;
if (aprule != null)
trace.Add(CreateAllomorphElement(aprule.Allomorphs[subruleIndex]));
trace.Add(CreateWordElement("Output", output, false));
((XElement) output.CurrentTrace).Add(trace);
output.CurrentTrace = trace;
}
public void MorphologicalRuleNotApplied(IMorphologicalRule rule, int subruleIndex, Word input, FailureReason reason, object failureObj)
{
var trace = new XElement("MorphologicalRuleSynthesisTrace",
CreateMorphologicalRuleElement(rule));
var aprule = rule as AffixProcessRule;
if (aprule != null)
trace.Add(CreateAllomorphElement(subruleIndex == -1 ? aprule.Allomorphs.Last() : aprule.Allomorphs[subruleIndex]));
trace.Add(new XElement("Output", "*None*"));
switch (reason)
{
case FailureReason.RequiredSyntacticFeatureStruct:
Debug.Assert(aprule != null);
var requiredFS = (FeatureStruct) failureObj;
FeatureSymbol[] requiredPos = requiredFS.PartsOfSpeech().ToArray();
FeatureSymbol[] inputPos = input.SyntacticFeatureStruct.PartsOfSpeech().ToArray();
if (requiredPos.Intersect(inputPos).Any())
{
trace.Add(new XElement("FailureReason", new XAttribute("type", "inflFeats"),
CreateInflFeaturesElement("InflFeatures", input.SyntacticFeatureStruct),
CreateInflFeaturesElement("RequiredInflFeatures", requiredFS)));
}
else
{
trace.Add(new XElement("FailureReason", new XAttribute("type", "pos"),
new XElement("Pos", string.Join(", ", inputPos.Select(s => s.Description))),
new XElement("RequiredPos", string.Join(", ", requiredPos.Select(s => s.Description)))));
}
break;
case FailureReason.RequiredStemName:
trace.Add(new XElement("FailureReason", new XAttribute("type", "fromStemName"),
new XElement("StemName", failureObj)));
break;
case FailureReason.RequiredMprFeatures:
Debug.Assert(aprule != null);
var group = (MprFeatureGroup) failureObj;
trace.Add(group.Name == "lexEntryInflTypes" ? new XElement("FailureReason", new XAttribute("type", "requiredInflType"))
: CreateMprFeaturesFailureElement(true, group, aprule.Allomorphs[subruleIndex].RequiredMprFeatures, input));
break;
case FailureReason.ExcludedMprFeatures:
trace.Add(new XElement("FailureReason", new XAttribute("type", "excludedInflType")));
break;
case FailureReason.Pattern:
Debug.Assert(aprule != null);
var env = (string) aprule.Allomorphs[subruleIndex].Properties[HCParser.Env];
var prefixEnv = (string) aprule.Allomorphs[subruleIndex].Properties[HCParser.PrefixEnv];
var suffixEnv = (string) aprule.Allomorphs[subruleIndex].Properties[HCParser.SuffixEnv];
if (env != null || prefixEnv != null || suffixEnv != null)
{
var reasonElem = new XElement("FailureReason", new XAttribute("type", "environment"));
if (env != null)
reasonElem.Add(new XElement("Environment", env));
if (prefixEnv != null)
reasonElem.Add(new XElement("Environment", env));
if (suffixEnv != null)
reasonElem.Add(new XElement("Environment", env));
trace.Add(reasonElem);
}
else
{
trace.Add(new XElement("FailureReason", new XAttribute("type", "affixProcess")));
}
break;
case FailureReason.MaxApplicationCount:
trace.Add(new XElement("FailureReason", new XAttribute("type", "maxAppCount")));
break;
case FailureReason.NonPartialRuleProhibitedAfterFinalTemplate:
trace.Add(new XElement("FailureReason", new XAttribute("type", "nonPartialRuleAfterFinalTemplate")));
break;
case FailureReason.NonPartialRuleRequiredAfterNonFinalTemplate:
trace.Add(new XElement("FailureReason", new XAttribute("type", "partialRuleAfterNonFinalTemplate")));
break;
default:
return;
}
((XElement) input.CurrentTrace).Add(trace);
}
public void Blocked(IHCRule rule, Word output)
{
}
public void Successful(Language lang, Word word)
{
((XElement)word.CurrentTrace).Add(new XElement("ParseCompleteTrace", new XAttribute("success", true),
CreateWordElement("Result", word, false)));
}
public void Failed(Language lang, Word word, FailureReason reason, Allomorph allomorph, object failureObj)
{
XElement trace;
switch (reason)
{
case FailureReason.AllomorphCoOccurrenceRules:
var alloRule = (AllomorphCoOccurrenceRule) failureObj;
trace = CreateParseCompleteElement(word,
new XElement("FailureReason", new XAttribute("type", "adhocProhibitionRule"),
new XElement("RuleType", "Allomorph"),
CreateAllomorphElement(allomorph),
new XElement("Others", alloRule.Others.Select(CreateAllomorphElement)),
new XElement("Adjacency", alloRule.Adjacency)));
break;
case FailureReason.MorphemeCoOccurrenceRules:
var morphemeRule = (MorphemeCoOccurrenceRule) failureObj;
trace = CreateParseCompleteElement(word,
new XElement("FailureReason", new XAttribute("type", "adhocProhibitionRule"),
new XElement("RuleType", "Morpheme"),
CreateMorphemeElement(allomorph.Morpheme),
new XElement("Others", morphemeRule.Others.Select(CreateMorphemeElement)),
new XElement("Adjacency", morphemeRule.Adjacency)));
break;
case FailureReason.Environments:
trace = CreateParseCompleteElement(word,
new XElement("FailureReason", new XAttribute("type", "environment"),
CreateAllomorphElement(allomorph),
new XElement("Environment", failureObj)));
break;
case FailureReason.SurfaceFormMismatch:
trace = CreateParseCompleteElement(word, new XElement("FailureReason", new XAttribute("type", "formMismatch")));
break;
case FailureReason.RequiredSyntacticFeatureStruct:
trace = CreateParseCompleteElement(word,
new XElement("FailureReason", new XAttribute("type", "affixInflFeats"),
CreateAllomorphElement(allomorph),
CreateInflFeaturesElement("InflFeatures", word.SyntacticFeatureStruct),
CreateInflFeaturesElement("RequiredInflFeatures", (FeatureStruct) failureObj)));
break;
case FailureReason.RequiredStemName:
trace = CreateParseCompleteElement(word,
new XElement("FailureReason", new XAttribute("type", "requiredStemName"),
CreateAllomorphElement(allomorph),
new XElement("StemName", failureObj)));
break;
case FailureReason.ExcludedStemName:
trace = CreateParseCompleteElement(word,
new XElement("FailureReason", new XAttribute("type", "excludedStemName"),
CreateAllomorphElement(allomorph),
new XElement("StemName", failureObj)));
break;
case FailureReason.BoundRoot:
trace = CreateParseCompleteElement(word, new XElement("FailureReason", new XAttribute("type", "boundStem")));
break;
case FailureReason.DisjunctiveAllomorph:
trace = CreateParseCompleteElement(word,
new XElement("FailureReason", new XAttribute("type", "disjunctiveAllomorph"),
CreateAllomorphElement((Allomorph) failureObj)));
break;
case FailureReason.PartialParse:
trace = CreateParseCompleteElement(word, new XElement("FailureReason", new XAttribute("type", "partialParse")));
break;
default:
return;
}
((XElement) word.CurrentTrace).Add(trace);
}
private static XElement CreateParseCompleteElement(Word word, XElement reasonElem)
{
return new XElement("ParseCompleteTrace", new XAttribute("success", false),
CreateWordElement("Result", word, false),
reasonElem);
}
private static XElement CreateInflFeaturesElement(string name, FeatureStruct fs)
{
string feat = (fs.Head() == null) ? "" : fs.Head().ToString().Replace(",", "");
return new XElement(name, feat);
}
private static XElement CreateWordElement(string name, Word word, bool analysis, bool bracketed = false)
{
string wordStr;
if (word == null)
wordStr = "*None*";
else
wordStr = analysis
? word.Shape.ToRegexString(word.Stratum.CharacterDefinitionTable, true)
: bracketed ? ToBracketedString(word.Shape, word.Stratum.CharacterDefinitionTable)
: word.Shape.ToString(word.Stratum.CharacterDefinitionTable, true);
return new XElement(name, wordStr);
}
private static string ToBracketedString(IEnumerable<ShapeNode> nodes, CharacterDefinitionTable table)
{
StringBuilder stringBuilder = new StringBuilder();
foreach (ShapeNode node in nodes)
{
string text = table.GetMatchingStrReps(node).FirstOrDefault();
if (text != null)
{
if (text.Length > 1)
text = "(" + text + ")";
stringBuilder.Append(text);
}
}
return stringBuilder.ToString();
}
private XElement CreateMorphemeElement(Morpheme morpheme)
{
var msaID = (int?) morpheme.Properties[HCParser.MsaID] ?? 0;
IMoMorphSynAnalysis msa;
if (msaID == 0 || !m_cache.ServiceLocator.GetInstance<IMoMorphSynAnalysisRepository>().TryGetObject(msaID, out msa))
return null;
var inflTypeID = (int?) morpheme.Properties[HCParser.InflTypeID] ?? 0;
ILexEntryInflType inflType = null;
if (inflTypeID != 0 && !m_cache.ServiceLocator.GetInstance<ILexEntryInflTypeRepository>().TryGetObject(inflTypeID, out inflType))
return null;
return HCParser.CreateMorphemeElement(msa, inflType);
}
private static XElement CreateMorphologicalRuleElement(IMorphologicalRule rule)
{
XElement elem = CreateHCRuleElement("MorphologicalRule", rule);
elem.Add(new XAttribute("type", rule is AffixProcessRule ? "affix" : "compound"));
return elem;
}
private static XElement CreateHCRuleElement(string name, IHCRule rule)
{
int id = 0;
var morpheme = rule as Morpheme;
if (morpheme != null)
id = (int?) morpheme.Properties[HCParser.MsaID] ?? 0;
return new XElement(name, new XAttribute("id", id), rule.Name);
}
private XElement CreateAllomorphElement(Allomorph allomorph)
{
bool isNull = (bool?) allomorph.Properties[HCParser.IsNull] ?? false;
if (isNull)
{
var slotID = (int) allomorph.Morpheme.Properties[HCParser.SlotID];
IMoInflAffixSlot slot;
if (!m_cache.ServiceLocator.GetInstance<IMoInflAffixSlotRepository>().TryGetObject(slotID, out slot))
return null;
var nullInflTypeID = (int) allomorph.Morpheme.Properties[HCParser.InflTypeID];
ILexEntryInflType nullInflType;
if (!m_cache.ServiceLocator.GetInstance<ILexEntryInflTypeRepository>().TryGetObject(nullInflTypeID, out nullInflType))
return null;
var isPrefix = (bool) allomorph.Properties[HCParser.IsPrefix];
return new XElement("Allomorph", new XAttribute("id", 0), new XAttribute("type", isPrefix ? MoMorphTypeTags.kMorphPrefix : MoMorphTypeTags.kMorphSuffix),
new XElement("Form", "^0"),
new XElement("Morpheme", new XAttribute("id", 0), new XAttribute("type", "infl"),
new XElement("HeadWord", string.Format("Automatically generated null affix for the {0} irregularly inflected form", nullInflType.Name.BestAnalysisAlternative.Text)),
new XElement("Gloss", (nullInflType.GlossPrepend.BestAnalysisAlternative.Text == "***" ? "" : nullInflType.GlossPrepend.BestAnalysisAlternative.Text)
+ (nullInflType.GlossAppend.BestAnalysisAlternative.Text == "***" ? "" : nullInflType.GlossAppend.BestAnalysisAlternative.Text)),
new XElement("Category", slot.OwnerOfClass<IPartOfSpeech>().Abbreviation.BestAnalysisAlternative.Text),
new XElement("Slot", new XAttribute("optional", slot.Optional), slot.Name.BestAnalysisAlternative.Text)));
}
var formID = (int?) allomorph.Properties[HCParser.FormID] ?? 0;
IMoForm form;
if (formID == 0 || !m_cache.ServiceLocator.GetInstance<IMoFormRepository>().TryGetObject(formID, out form))
return null;
var formID2 = (int?) allomorph.Properties[HCParser.FormID2] ?? 0;
var msaID = (int) allomorph.Morpheme.Properties[HCParser.MsaID];
IMoMorphSynAnalysis msa;
if (!m_cache.ServiceLocator.GetInstance<IMoMorphSynAnalysisRepository>().TryGetObject(msaID, out msa))
return null;
var inflTypeID = (int?) allomorph.Morpheme.Properties[HCParser.InflTypeID] ?? 0;
ILexEntryInflType inflType = null;
if (inflTypeID != 0 && !m_cache.ServiceLocator.GetInstance<ILexEntryInflTypeRepository>().TryGetObject(inflTypeID, out inflType))
return null;
string guessedString = allomorph.Guessed ? allomorph.Morpheme.Gloss : null;
return HCParser.CreateAllomorphElement("Allomorph", form, msa, inflType, formID2 != 0, guessedString);
}
}
}