-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathSandboxBase.GetRealyAnalysisMethod.cs
More file actions
1105 lines (1047 loc) · 43.4 KB
/
Copy pathSandboxBase.GetRealyAnalysisMethod.cs
File metadata and controls
1105 lines (1047 loc) · 43.4 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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2015 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.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using SIL.LCModel.Core.KernelInterfaces;
using SIL.FieldWorks.Common.FwUtils;
using SIL.LCModel;
using SIL.LCModel.DomainServices;
namespace SIL.FieldWorks.IText
{
partial class SandboxBase
{
/// <summary>
/// Determine the real analysis corresponding to the current state of the sandbox.
/// The 'real analysis' may be a WfiWordform (if it hasn't been analyzed at all), a WfiAnalysis,
/// or a WfiGloss.
/// </summary>
public class GetRealAnalysisMethod
{
protected CachePair m_caches;
protected int m_hvoSbWord;
IWfiWordform m_wf;
IWfiGloss m_wg;
private AnalysisTree m_oldAnalysis;
protected SandboxBase m_sandbox; // The sandbox we're working from.
protected int[] m_analysisMorphs;
protected int[] m_analysisMsas;
protected int[] m_analysisSenses;
int m_hvoCategoryReal;
protected IWfiAnalysis m_wa;
protected bool m_fWantOnlyWfiAnalysis;
protected InterlinLineChoices m_choices;
protected IHelpTopicProvider m_helpTopicProvider;
protected ISilDataAccess m_sda;
protected ISilDataAccess m_sdaMain;
protected int m_cmorphs;
ITsString m_tssForm; // the form to use if we have to create a new WfiWordform.
// These variables get filled in by CheckItOut. The Long message is suitable for a
// MessageBox, the short one should fit in a status line. Currently always null.
string m_LongMessage;
string m_ShortMessage;
/// <summary>
/// Only used to make the UpdateRealAnalysisMethod construcotr happy. Do not use directly.
/// </summary>
public GetRealAnalysisMethod()
{
}
public GetRealAnalysisMethod(IHelpTopicProvider helpTopicProvider, SandboxBase owner,
CachePair caches, int hvoSbWord, AnalysisTree oldAnalysis, IWfiAnalysis wa,
IWfiGloss gloss, InterlinLineChoices choices, ITsString tssForm,
bool fWantOnlyWfiAnalysis) : this()
{
m_helpTopicProvider = helpTopicProvider;
m_sandbox = owner;
m_caches = caches;
m_hvoSbWord = hvoSbWord;
m_oldAnalysis = oldAnalysis;
m_wf = oldAnalysis.Wordform;
m_wa = wa;
m_wg = gloss;
m_sda = m_caches.DataAccess;
m_sdaMain = m_caches.MainCache.MainCacheAccessor;
m_cmorphs = m_sda.get_VecSize(m_hvoSbWord, ktagSbWordMorphs);
m_choices = choices;
m_tssForm = tssForm;
m_fWantOnlyWfiAnalysis = fWantOnlyWfiAnalysis;
}
/// <summary>
/// Rather than deleting an obsolete analysis internally, we need to pass it
/// back to the caller. See LT-11502.
/// </summary>
public IWfiAnalysis ObsoleteAnalysis { get; private set; }
public string ShortMessage
{
get { return m_ShortMessage; }
}
/// <summary>
/// Run the algorithm, returning the 'analysis' hvo (WfiWordform, WfiAnalysis, or WfiGloss).
/// </summary>
/// <returns></returns>
public IAnalysis Run()
{
CheckItOut();
if (m_LongMessage != null)
{
MessageBox.Show(m_LongMessage, ITextStrings.ksProblem);
return null;
}
return FinishItOff();
}
public void CheckItOut()
{
m_LongMessage = null;
m_ShortMessage = null;
}
/// <summary>
/// Do the bulk of the computation, everything after initial error checking, which is now nonexistent.
/// </summary>
/// <returns>HVO of analysis (WfiWordform, WfiAnalyis, or WfiGloss)</returns>
private IAnalysis FinishItOff()
{
LcmCache fdoCache = m_caches.MainCache;
var wfRepository = fdoCache.ServiceLocator.GetInstance<IWfiWordformRepository>();
if (m_wf == null)
{
IWfiWordform wf;
// first see if we can find a matching form
if (wfRepository.TryGetObject(m_tssForm, false, out wf))
m_wf = wf;
else
{
// The user selected a case form that did not previously have a WfiWordform.
// Since he is confirming this analysis, we now need to create one.
// Note: if in context of the wordforms DummyRecordList, the RecordList is
// smart enough to handle inserting one object without having to Reload the whole list.
m_wf = fdoCache.ServiceLocator.GetInstance<IWfiWordformFactory>().Create(m_tssForm);
}
}
// If sandbox contains only an empty morpheme string, don't consider this to be a true analysis.
// Assume that the user was not finished with his analysis (cf. LT-1621).
if (m_sandbox.IsAnalysisEmpty)
{
return m_wf;
}
// Update the wordform with any additional wss.
List<int> wordformWss = m_choices.OtherEnabledWritingSystemsForFlid(InterlinLineChoices.kflidWord, 0);
// we need another way to detect the static ws for kflidWord.
foreach (int wsId in wordformWss)
{
UpdateMlaIfDifferent(m_hvoSbWord, ktagSbWordForm, wsId, m_wf.Hvo, WfiWordformTags.kflidForm);
}
// (LT-7807 later refined by FWR-3536)
// if we're in a special mode for adding monomorphemic words to lexicon and the user's proposed analysis is monomorphemic,
// if there is an existing possible analysis that matches on form, gloss, and POS, use it.
// else if there is an existing possible analysis that matches on form and gloss and has no POS,
// update the POS and use it.
// else if the occurrence is currently analyzed as a particular sense and there are no other occurrences
// of that sense, update the gloss and/or POS of the sense to match what we want (and use it)
// else if there is a matching entry with the right form, add a suitable sense to use
// else make a new entry and sense to use.
if (m_sandbox.ShouldAddWordGlossToLexicon)
{
IhMorphEntry.MorphItem matchingMorphItem = new IhMissingEntry.MorphItem(0, null);
ITsString tssWf = m_wf.Form.get_String(m_sandbox.RawWordformWs);
// go through the combo options for lex entries / senses to see if we can find any existing matches.
using (IhMorphEntry handler = InterlinComboHandler.MakeCombo(m_helpTopicProvider, ktagWordGlossIcon, m_sandbox, 0) as SandboxBase.IhMorphEntry)
{
List<IhMorphEntry.MorphItem> morphItems = handler.MorphItems;
// see if we can use an existing Sense, if it matches the word gloss and word MSA
foreach (IhMorphEntry.MorphItem morphItem in morphItems)
{
// skip lex senses that do not match word gloss and pos in the Sandbox
if (!SbWordGlossMatchesSenseGloss(morphItem))
continue;
if (!SbWordPosMatchesSenseMsaPos(morphItem))
continue;
// found a LexSense matching our Word Gloss and MSA POS
matchingMorphItem = morphItem;
break;
}
if (matchingMorphItem.m_hvoSense == 0)
{
// Next see if we can find an existing analysis where the gloss matches and POS is null.
foreach (IhMorphEntry.MorphItem morphItem in morphItems)
{
// skip lex senses that do not match word gloss and pos in the Sandbox
if (!SbWordGlossMatchesSenseGloss(morphItem))
continue;
// found a LexSense matching our Word Gloss and that has no POS.
var pos = m_caches.RealObject(m_sda.get_ObjectProp(m_hvoSbWord, ktagSbWordPos)) as IPartOfSpeech;
var sense = m_caches.MainCache.ServiceLocator.GetObject(morphItem.m_hvoSense) as ILexSense;
if (sense == null)
continue; // don't think this can happen but play safe.
var msa = sense.MorphoSyntaxAnalysisRA as IMoStemMsa;
if (msa == null || msa.PartOfSpeechRA != null)
continue; // for this case we can only use one that doesn't already have a POS.
msa.PartOfSpeechRA = pos; // adjust it
if (m_oldAnalysis.WfiAnalysis != null) // always?
{
if (m_oldAnalysis.WfiAnalysis.CategoryRA != pos)
m_oldAnalysis.WfiAnalysis.CategoryRA = pos;
}
matchingMorphItem = morphItem; // and use it.
break;
}
}
if (matchingMorphItem.m_hvoSense == 0 && m_oldAnalysis != null && m_oldAnalysis.WfiAnalysis != null)
{
// still don't have one we can use; see whether it is legitimate to modify the current
// analysis.
var oldAnalysis = m_oldAnalysis.WfiAnalysis;
if (oldAnalysis.MorphBundlesOS.Count == 1
&& oldAnalysis.MorphBundlesOS[0].SenseRA != null
&& oldAnalysis.MorphBundlesOS[0].SenseRA.MorphoSyntaxAnalysisRA is IMoStemMsa
&& OnlyUsedThisOnce(oldAnalysis)
&& OnlyUsedThisOnce(oldAnalysis.MorphBundlesOS[0].SenseRA))
{
// We're allowed to change the existing sense and analysis! A side effect of updating the sense
// is updating the MSA of the morph bundle of the oldAnalysis.
var pos = m_caches.RealObject(m_sda.get_ObjectProp(m_hvoSbWord, ktagSbWordPos)) as IPartOfSpeech;
UpdateSense(oldAnalysis.MorphBundlesOS[0].SenseRA, pos);
if (oldAnalysis.CategoryRA != pos)
oldAnalysis.CategoryRA = pos;
if (m_oldAnalysis.Gloss != null)
{
// if the old analysis is a gloss, update it also.
CopyGlossesToWfiGloss(m_oldAnalysis.Gloss);
return m_oldAnalysis.Gloss;
}
// Don't have an old gloss, create one.
var newGloss = oldAnalysis.Services.GetInstance<IWfiGlossFactory>().Create();
oldAnalysis.MeaningsOC.Add(newGloss);
CopyGlossesToWfiGloss(newGloss);
return newGloss;
}
}
// If we get here we could not use an existing analysis with any safe modification.
// if we couldn't use an existing sense but we match a LexEntry form,
// add a new sense to an existing entry.
ILexEntry bestEntry = null;
if (morphItems.Count > 0 && matchingMorphItem.m_hvoSense == 0)
{
// Tried using FindBestLexEntryAmongstHomographs() but it matches
// only CitationForm which MorphItems doesn't know anything about,
// and doesn't match against Allomorphs which MorphItems do track
// so this could lead to a crash (LT-9430).
//
// Solution: if the user specified a category, see if we can find an entry
// with a sense using that same category
// otherwise just add the new sense to the first entry in MorphItems.
IhMorphEntry.MorphItem bestMorphItem = morphItems[0];
foreach (IhMorphEntry.MorphItem morphItem in morphItems)
{
// skip items that do not match word main pos in the Sandbox
if (!SbWordMainPosMatchesSenseMsaMainPos(morphItem))
continue;
bestMorphItem = morphItem;
break;
}
bestEntry = bestMorphItem.GetPrimaryOrOwningEntry(m_caches.MainCache);
// lookup this entry;
matchingMorphItem = FindLexEntryMorphItem(morphItems, bestEntry);
}
if (matchingMorphItem.m_hvoMorph == 0)
{
// we didn't find a matching lex entry, so create a new entry
ILexEntry newEntry;
ILexSense newSense;
IMoForm allomorph;
handler.CreateNewEntry(true, out newEntry, out allomorph, out newSense);
}
else if (bestEntry != null)
{
// we found matching lex entry, so create a new sense for it
var senseFactory = fdoCache.ServiceLocator.GetInstance<ILexSenseFactory>();
ILexSense newSense = senseFactory.Create(bestEntry, new SandboxGenericMSA(), "");
// copy over any word glosses we're showing.
CopyGlossesToSense(newSense);
// copy over the Word POS
var pos = m_caches.RealObject(m_sda.get_ObjectProp(m_hvoSbWord, ktagSbWordPos)) as IPartOfSpeech;
(newSense.MorphoSyntaxAnalysisRA as IMoStemMsa).PartOfSpeechRA = pos;
var morph = fdoCache.ServiceLocator.GetInstance<IMoFormRepository>().GetObject(matchingMorphItem.m_hvoMorph);
handler.UpdateMorphEntry(morph, bestEntry, newSense);
}
else
{
// we found a matching lex entry and sense, so select it.
int iMorphItem = morphItems.IndexOf(matchingMorphItem);
handler.HandleSelect(iMorphItem);
}
}
}
BuildMorphLists(); // Used later on in the code.
m_hvoCategoryReal = m_caches.RealHvo(m_sda.get_ObjectProp(m_hvoSbWord, ktagSbWordPos));
// We may need to create a new WfiAnalysis based on whether we have any sandbox gloss content.
bool fNeedGloss = false;
bool fWordGlossLineIsShowing = false; // Set to 'true' if the wrod gloss line is included in the m_choices fields.
foreach (InterlinLineSpec ilc in m_choices.EnabledLineSpecs)
{
if (ilc.Flid == InterlinLineChoices.kflidWordGloss)
{
fWordGlossLineIsShowing = true;
break;
}
}
if (fWordGlossLineIsShowing)
{
// flag that we need to create wfi gloss information if any configured word gloss lines have content.
foreach (int wsId in m_choices.EnabledWritingSystemsForFlid(InterlinLineChoices.kflidWordGloss))
{
if (m_sda.get_MultiStringAlt(m_hvoSbWord, ktagSbWordGloss, wsId).Length > 0)
{
fNeedGloss = true;
break;
}
}
}
// We decided to take this logic out (see LT-1653) because it is annoying when
// deliberately removing a wrong morpheme breakdown guess.
// // We need to create (or find an existing) WfiAnalysis if we have any morphemes,
// // a word gloss, or a word category.
// bool fNeedAnalysis = fNeedGloss || m_cmorphs > 1 || m_hvoCategoryReal != 0;
//
// // If we have exactly one morpheme, see if it has some non-trivial information
// // associated. If not, don't make an analysis.
// if (!fNeedGloss && m_cmorphs == 1 && m_hvoCategoryReal == 0 && m_analysisMsas[0] == 0
// && m_analysisSenses[0] == 0 && m_analysisMorphs[0] == 0)
// {
// fNeedAnalysis = false;
// }
// // If there's no information at all, the 'analysis' is just the original wordform.
// if (!fNeedAnalysis)
// return m_hvoWordform;
// OK, we have some information that corresponds to an analysis. Find or create
// an analysis that matches.
int wsVern = m_sandbox.RawWordformWs;
m_wa = FindMatchingAnalysis(true);
bool fFoundAnalysis = m_wa != null;
if (!fFoundAnalysis)
{
// Clear the checksum on the wordform. This allows the parser filer to re-evaluate it and
// delete the old analysis if it is just a simpler, parser-generated form of the one we're now making.
m_wf.Checksum = 0;
// Check whether there's a parser-generated analysis that the current settings
// subsume. If so, reuse that analysis by filling in the missing data (word gloss,
// word category, and senses).
// Another option is that there is an existing 'analysis' that is a trivial one,
// created by word-only glossing. We can re-use that, filling in the other details
// now supplied.
var partialWa = FindMatchingAnalysis(false);
bool fNewAnal = partialWa == null;
if (fNewAnal)
{
foreach (var ana in m_wf.AnalysesOC)
{
if (m_oldAnalysis != null &&
ana == m_oldAnalysis.WfiAnalysis &&
OnlyUsedThisOnce(ana) &&
IsAnalysisHumanApproved(m_caches.MainCache, ana))
{
ObsoleteAnalysis = ana;
break;
}
}
// Create one.
var waFactory = fdoCache.ServiceLocator.GetInstance<IWfiAnalysisFactory>();
var waNew = waFactory.Create();
m_wf.AnalysesOC.Add(waNew);
m_wa = waNew;
}
else
{
m_wa = partialWa;
// For setting word glosses, we should treat this as a 'found' not new analysis
// if it has any glosses, so we will search for and find any existing ones that match.
fFoundAnalysis = m_wa.MeaningsOC.Count > 0;
}
IPartOfSpeech pos = null;
if (m_hvoCategoryReal != 0)
pos = fdoCache.ServiceLocator.GetInstance<IPartOfSpeechRepository>().GetObject(m_hvoCategoryReal);
m_wa.CategoryRA = pos;
var mbFactory = fdoCache.ServiceLocator.GetInstance<IWfiMorphBundleFactory>();
var msaRepository = fdoCache.ServiceLocator.GetInstance<IMoMorphSynAnalysisRepository>();
var mfRepository = fdoCache.ServiceLocator.GetInstance<IMoFormRepository>();
var senseRepository = fdoCache.ServiceLocator.GetInstance<ILexSenseRepository>();
var inflTypeRepository = fdoCache.ServiceLocator.GetInstance<ILexEntryInflTypeRepository>();
for (int imorph = 0; imorph < m_cmorphs; imorph++)
{
IWfiMorphBundle mb;
if (imorph >= m_wa.MorphBundlesOS.Count)
{
mb = mbFactory.Create();
m_wa.MorphBundlesOS.Insert(imorph, mb);
}
else
{
mb = m_wa.MorphBundlesOS[imorph];
}
// An undo operation can leave stale information in the sandbox. If
// that happens, the stored database id values are invalid. Set them
// all to zero if the morph is invalid. (See LT-3824 for a crash
// scenario.) This fix prevents a crash, but doesn't do anything for
// restoring the original values before the operation that is undone.
if (m_analysisMorphs[imorph] != 0 &&
!m_sdaMain.get_IsValidObject(m_analysisMorphs[imorph]))
{
m_analysisMorphs[imorph] = 0;
m_analysisMsas[imorph] = 0;
m_analysisSenses[imorph] = 0;
}
// Set the Morph of the bundle if we know a real one; otherwise, just set its Form
if (m_analysisMorphs[imorph] == 0)
{
int hvoSbMorph = m_sda.get_VecItem(m_hvoSbWord, ktagSbWordMorphs, imorph);
mb.Form.set_String(wsVern, m_sandbox.GetFullMorphForm(hvoSbMorph));
// Copy any other wss over, without any funny business about morpheme breaks
// Per LT-14891, we don't allow editing of the other morph lines, so no need to set them.
//foreach (int ws in m_choices.OtherWritingSystemsForFlid(InterlinLineChoices.kflidMorphemes, 0))
//{
// mb.Form.set_String(ws,
// m_caches.DataAccess.get_MultiStringAlt(hvoSbMorph, ktagSbNamedObjName, ws));
//}
}
else
{
mb.MorphRA = mfRepository.GetObject(m_analysisMorphs[imorph]);
if (mb.MorphRA != null && IsLexicalPattern(mb.MorphRA.Form))
{
// If mb.MorphRA.Form is a lexical pattern then set mb.Form to the guessed root.
int hvoSbMorph = m_sda.get_VecItem(m_hvoSbWord, ktagSbWordMorphs, imorph);
mb.Form.set_String(wsVern, m_sandbox.GetFullMorphForm(hvoSbMorph));
}
}
// Set the MSA if we have one. Note that it is (pathologically) possible that the user has done
// something in another window to destroy the MSA we remember, so don't try to set it if so.
if (m_analysisMsas[imorph] != 0 && m_sdaMain.get_IsValidObject(m_analysisMsas[imorph]))
{
mb.MsaRA = msaRepository.GetObject(m_analysisMsas[imorph]);
}
// Likewise the Sense
if (m_analysisSenses[imorph] != 0)
{
mb.SenseRA = senseRepository.GetObject(m_analysisSenses[imorph]);
// set the InflType if we have one.
var hvoSbInflType = GetRealHvoFromSbWmbInflType(imorph);
mb.InflTypeRA = hvoSbInflType != 0 ? inflTypeRepository.GetObject(hvoSbInflType) : null;
}
}
}
else if (fWordGlossLineIsShowing) // If the line is not showing at all, don't bother.
{
// (LT-1428) Since we're using an existing WfiAnalysis,
// We will find or create a new WfiGloss (even for blank lines)
// if WfiAnalysis already has WfiGlosses
// or m_hvoWordGloss is nonzero
// or Sandbox has gloss content.
bool fSbGlossContent = fNeedGloss;
int cGloss = m_wa.MeaningsOC.Count;
fNeedGloss = cGloss > 0 || m_wg != null || fSbGlossContent;
}
if (m_wa != null)
EnsureCorrectMorphForms();
if (!fNeedGloss || m_fWantOnlyWfiAnalysis)
{
return m_wa;
}
if (m_wg != null)
{
// We may consider editing it instead of making a new one.
// But ONLY if it belongs to the right analysis!!
if (m_wg.Owner != m_wa)
m_wg = null;
}
/* These are the types of things we are trying to accomplish here.
Problem 1 -- Correcting a spelling mistake.
Gloss1: mn <-
User corrects spelling to men
Desired:
Gloss1: men <-
Bad result:
Gloss1: mn
Gloss2: men <-
Problem 2 -- Switching to a different gloss via typing.
Gloss1: he <-
Gloss2: she
User types in she rather than using dropdown box to select it
Desired:
Gloss1: he
Gloss2: she <-
Bad result:
Gloss1: she <-
Gloss2: she
Problem 2A
Gloss1: he <-
User types in she without first using dropdown box to select "add new gloss"
Desired:
Gloss1: he (still used for N other occurrences)
Gloss2: she <-
Bad (at least dubious) result:
Gloss1: she <- (used for this and all other occurrences)
Problem 3 -- Adding a missing alternative when there are not duplications.
Gloss1: en=green <-
User adds the French equivalent
Desired:
Gloss1: en=green, fr=vert <-
Bad result:
Gloss1: en=green
Gloss2: en=green, fr=vert <-
The logic used to be to look for glosses with all alternatives matching or else it
creates a new one. So 2 would actually work, but 1 and 3 were both bad.
New logic: keep track of the starting WfiAnalysis and WfiGloss.
Assuming we haven't changed to a new WfiAnalysis based on other changes, if there
is a WfiGloss that matches any of the existing alternatives, we switch to that.
Otherwise we set the alternatives of the starting WfiGloss to whatever the user
entered. This logic would work for all three examples above, but has problems
with the following.
Problem -- add a missing gloss where there are identical glosses in another ws.
Gloss1: en=them <-
User adds Spanish gloss
Desired:
Gloss1: en=them, es=ellas <-
This works ok with above logic. But now in another location the user needs to
use the masculine them in Spanish, so changes ellas to ellos
Desired:
Gloss1: en=them, es=ellas
Gloss2: en=them, es=ellos <-
Bad result:
Gloss1: en=them, es=ellos <-
Eventually, we'll probably want to display a dialog box to ask users what they really want.
"There are 15 places where "XXX" analyzed as 3pp is currently glossed
en->"them". Would you like to
<radio button, selected> change them all to en->"them" sp->"ellas"?
<radio button> leave the others glossed en->"them" and let just this one
be en->"them" sp->"ellas"?
<radio button> see a concordance and choose which ones to change to
en->"them" sp->"ellas"?
*/
// (LT-1428, LT-12472)
// -----------------------------
// When the user edits a gloss,
// (1) If there is an existing gloss matching what they just changed it to
// then switch this instance to point to that one.
// (2) Else if the gloss is used only in this instance, or if it matches on all WSs that are not blank in the gloss,
// then apply the edits directly to the gloss.
// (3) Else, create a new gloss.
//-------------------------------
var gloss = fFoundAnalysis ? FindMatchingGloss() : null;
if (gloss == null && m_sandbox.WordGlossReferenceCount == 1)
{
gloss = m_wg; // update the existing gloss.
}
if (gloss == null)
{
// Create one.
var wgFactory = fdoCache.ServiceLocator.GetInstance<IWfiGlossFactory>();
gloss = wgFactory.Create();
m_wa.MeaningsOC.Add(gloss);
}
foreach (int wsId in m_choices.EnabledWritingSystemsForFlid(InterlinLineChoices.kflidWordGloss))
{
ITsString tssGloss = m_sda.get_MultiStringAlt(m_hvoSbWord, ktagSbWordGloss, wsId);
if (!tssGloss.Equals(gloss.Form.get_String(wsId)))
{
gloss.Form.set_String(wsId, tssGloss);
}
}
return gloss;
}
private void CopyGlossesToSense(ILexSense sense)
{
foreach (int wsId in m_choices.EnabledWritingSystemsForFlid(InterlinLineChoices.kflidWordGloss))
{
UpdateMlaIfDifferent(m_hvoSbWord, ktagSbWordGloss, wsId, sense.Hvo, LexSenseTags.kflidGloss);
}
}
private void CopyGlossesToWfiGloss(IWfiGloss gloss)
{
foreach (int wsId in m_choices.EnabledWritingSystemsForFlid(InterlinLineChoices.kflidWordGloss))
{
UpdateMlaIfDifferent(m_hvoSbWord, ktagSbWordGloss, wsId, gloss.Hvo, WfiGlossTags.kflidForm);
}
}
private void UpdateSense(ILexSense sense, IPartOfSpeech pos)
{
CopyGlossesToSense(sense);
var msa = (IMoStemMsa)sense.MorphoSyntaxAnalysisRA;
var lexEntry = sense.Entry;
if (msa.PartOfSpeechRA != pos)
{
// is there another MSA we can use?
foreach (var msaOther in lexEntry.MorphoSyntaxAnalysesOC)
{
var stem = msaOther as IMoStemMsa;
if (stem == null)
continue;
if (stem.PartOfSpeechRA == pos)
{
sense.MorphoSyntaxAnalysisRA = msaOther; // also updates WfiMorphBundle and deletes old obsolete MSA if obsolete.
return;
}
}
// Is this msa used elsewhere or can we modify it?
if (lexEntry.SensesOS.Where(s => s != sense && s.MorphoSyntaxAnalysisRA == msa).Take(1).Count() > 0)
{
// Used; have to make a new one.
msa = sense.Services.GetInstance<IMoStemMsaFactory>().Create();
lexEntry.MorphoSyntaxAnalysesOC.Add(msa);
sense.MorphoSyntaxAnalysisRA = msa;
}
msa.PartOfSpeechRA = pos;
}
}
// Answer true if the analysis is only used in one place (typically the current one).
// It must have at most one WfiGloss and a net of one Segment that references it and its gloss if any.
// That one segment must only reference it once.
private bool OnlyUsedThisOnce(IWfiAnalysis oldAnalysis)
{
if (oldAnalysis.MeaningsOC.Count > 1)
return false; // It's technically possible there might only be one use, but which one would we update?
// No need to enumerate more than two! This will speed it up for words used a lot.
var segsUsingAnalysis = oldAnalysis.ReferringObjects.Where(obj => obj is ISegment).Take(2);
if (segsUsingAnalysis.Count() > 1)
return false;
ISegment seg = segsUsingAnalysis.FirstOrDefault() as ISegment;
IAnalysis target = oldAnalysis;
if (oldAnalysis.MeaningsOC.Count == 1)
{
var wfiGloss = oldAnalysis.MeaningsOC.ToArray()[0];
var segsUsingGloss = wfiGloss.ReferringObjects.Where(obj => obj is ISegment).Take(2);
if (segsUsingAnalysis.Count() + segsUsingGloss.Count() > 1)
return false;
if (seg == null)
{
seg = segsUsingGloss.FirstOrDefault() as ISegment;
target = wfiGloss;
}
}
if (seg == null)
return true; // no uses at all...probably can't happen
return seg.AnalysesRS.Where(a => a == target).Take(2).Count() <= 1;
}
// Answer true if the sense is only used in one WfiAnalysis.
private bool OnlyUsedThisOnce(ILexSense sense)
{
return sense.ReferringObjects.Where(obj => obj is IWfiMorphBundle).Take(2).Count() <= 1;
}
/// <summary>
/// Find the morph item referring to the LexEntry (only), not
/// a sense or msa.
/// </summary>
/// <param name="morphItems"></param>
/// <param name="leTarget"></param>
/// <returns></returns>
private static IhMorphEntry.MorphItem FindLexEntryMorphItem(List<IhMorphEntry.MorphItem> morphItems, ILexEntry leTarget)
{
if (leTarget != null)
{
foreach (IhMorphEntry.MorphItem mi in morphItems)
{
if (mi.m_hvoSense == 0 && mi.m_hvoMorph != 0)
{
var entryCandidate = mi.GetPrimaryOrOwningEntry(leTarget.Cache);
if (entryCandidate == leTarget)
return mi;
}
}
}
return new IhMissingEntry.MorphItem(0, null);
}
private bool SbWordPosMatchesSenseMsaPos(IhMorphEntry.MorphItem morphItem)
{
var pos = m_caches.RealObject(m_sda.get_ObjectProp(m_hvoSbWord, ktagSbWordPos)) as IPartOfSpeech;
// currently only support MoStemMsa, since that is what a WordPos expects to match against.
// (but -- see FWR-3475 part 2 -- the user can pathologically analyze a whole word as an affix
// in which case we MIGHT see another kind here, so use the root repository and 'as'.
if (morphItem.m_hvoMsa != 0)
{
IMoStemMsa msa =
m_caches.MainCache.ServiceLocator.GetInstance<IMoMorphSynAnalysisRepository>().GetObject(
morphItem.m_hvoMsa) as IMoStemMsa;
return msa != null && msa.PartOfSpeechRA == pos;
}
return pos == null;
}
/// <summary>
/// see if the MainPossibilities match for the given morphItem and
/// the Word Part of Speech in the sandbox
/// </summary>
/// <param name="morphItem"></param>
/// <returns></returns>
private bool SbWordMainPosMatchesSenseMsaMainPos(IhMorphEntry.MorphItem morphItem)
{
var targetPos = m_caches.RealObject(m_sda.get_ObjectProp(m_hvoSbWord, ktagSbWordPos)) as IPartOfSpeech;
int hvoMainPosCandidate = 0;
int hvoMainPosTarget = 0;
// currently only support MoStemMsa, since that is what a WordPos expects to match against.
// (but -- see FWR-3475 part 2 -- the user can pathologically analyze a whole word as an affix
// in which case we MIGHT see another kind here, so use the root repository and 'as'.
IPartOfSpeech posCandidate = null;
if (morphItem.m_hvoMsa != 0)
{
var msa = m_caches.MainCache.ServiceLocator.GetInstance<IMoMorphSynAnalysisRepository>().GetObject(morphItem.m_hvoMsa) as IMoStemMsa;
if (msa != null && msa.PartOfSpeechRA != null)
{
posCandidate = msa.PartOfSpeechRA;
ICmPossibility mainPosCandidate = posCandidate.MainPossibility;
if (mainPosCandidate != null)
hvoMainPosCandidate = mainPosCandidate.Hvo;
}
}
if (targetPos != null)
{
if (targetPos != null)
{
ICmPossibility mainPosTarget = targetPos.MainPossibility;
if (mainPosTarget != null)
hvoMainPosTarget = mainPosTarget.Hvo;
}
}
return hvoMainPosCandidate == hvoMainPosTarget;
}
private bool SbWordGlossMatchesSenseGloss(IhMorphEntry.MorphItem morphItem)
{
if (morphItem.m_hvoSense <= 0)
return false;
// compare our gloss information.
List<int> wordGlossWss = m_choices.EnabledWritingSystemsForFlid(InterlinLineChoices.kflidWordGloss);
foreach (int wsId in wordGlossWss)
{
if (!IsMlSame(m_hvoSbWord, ktagSbWordGloss, wsId, morphItem.m_hvoSense, LexSenseTags.kflidGloss))
{
// the sandbox word gloss differs from the sense gloss, so go to the next morphItem.
return false;
}
}
return true;
}
protected void BuildMorphLists()
{
// Build lists of morphs, msas, and senses, that we can use in subsequent code.
m_analysisMorphs = new int[m_cmorphs];
m_analysisMsas = new int[m_cmorphs];
m_analysisSenses = new int[m_cmorphs];
for (int imorph = m_cmorphs; --imorph >= 0; )
{
int hvoMorph = m_sda.get_VecItem(m_hvoSbWord, ktagSbWordMorphs, imorph);
int hvoMorphForm = m_sda.get_ObjectProp(hvoMorph, ktagSbMorphForm);
m_analysisMorphs[imorph] = m_caches.RealHvo(hvoMorphForm);
m_analysisMsas[imorph] = m_caches.RealHvo(m_sda.get_ObjectProp(hvoMorph, ktagSbMorphPos));
m_analysisSenses[imorph] = m_caches.RealHvo(m_sda.get_ObjectProp(hvoMorph, ktagSbMorphGloss));
}
}
/// <summary>
/// Ensure that the specified writing system of property flidDest in object hvoDst in m_sdaMain
/// is the same as property flidSrc in object hvoSrc in m_sda. If not, update.
/// </summary>
/// <param name="hvoSrc"></param>
/// <param name="flidSrc"></param>
/// <param name="wsId"></param>
/// <param name="hvoDst"></param>
/// <param name="flidDest"></param>
void UpdateMlaIfDifferent(int hvoSrc, int flidSrc, int wsId, int hvoDst, int flidDest)
{
ITsString tss;
ITsString tssOld;
if (IsMlSame(hvoSrc, flidSrc, wsId, hvoDst, flidDest, out tss, out tssOld))
return;
m_sdaMain.SetMultiStringAlt(hvoDst, flidDest, wsId, tss);
}
private bool IsMlSame(int hvoSrc, int flidSrc, int wsId, int hvoDst, int flidDest)
{
ITsString tss;
ITsString tssOld;
return IsMlSame(hvoSrc, flidSrc, wsId, hvoDst, flidDest, out tss, out tssOld);
}
private bool IsMlSame(int hvoSrc, int flidSrc, int wsId, int hvoDst, int flidDest, out ITsString tss, out ITsString tssOld)
{
tss = m_sda.get_MultiStringAlt(hvoSrc, flidSrc, wsId);
tssOld = m_sdaMain.get_MultiStringAlt(hvoDst, flidDest, wsId);
return tss.Equals(tssOld);
}
/// <summary>
/// m_hvoAnalysis is the selected analysis. However, we did not consider writing systems
/// of the morpheme line except the default vernacular one in deciding to use it.
/// If additional WS information has been supplied, save it.
/// </summary>
void EnsureCorrectMorphForms()
{
List<int> otherWss = m_choices.OtherEnabledWritingSystemsForFlid(InterlinLineChoices.kflidMorphemes, 0);
foreach (int wsId in otherWss)
{
for (int imorph = 0; imorph < m_cmorphs; imorph++)
{
var mb = m_wa.MorphBundlesOS[imorph];
int hvoSbMorph = m_sda.get_VecItem(m_hvoSbWord, ktagSbWordMorphs, imorph);
int hvoSecMorph = m_sda.get_ObjectProp(hvoSbMorph, ktagSbMorphForm);
if (m_analysisMorphs[imorph] == 0)
{
// We have no morph, set it on the WfiMorphBundle.
UpdateMlaIfDifferent(hvoSecMorph, ktagSbNamedObjName, wsId, mb.Hvo, WfiMorphBundleTags.kflidForm);
}
else
{
// Set it on the MoForm.
UpdateMlaIfDifferent(hvoSecMorph, ktagSbNamedObjName, wsId, m_analysisMorphs[imorph], MoFormTags.kflidForm);
}
}
}
}
/// <summary>
/// Find one of the WfiGlosses of m_hvoWfiAnalysis where the form matches for each analysis writing system.
/// Review: We probably want to find a WfiGloss that matches any (non-zero length) alternative since we don't want to
/// force the user to type in every alternative each time they enter a gloss.
/// Otherwise, if we match one with all non-zero length, return that one. (LT-1428)
/// </summary>
/// <returns></returns>
public IWfiGloss FindMatchingGloss()
{
var wsIds = m_choices.EnabledWritingSystemsForFlid(InterlinLineChoices.kflidWordGloss);
var sda = m_sda;
var wfiAnalysis = m_wa;
var hvoWord = m_hvoSbWord;
return GetBestGloss(wfiAnalysis, wsIds, sda, hvoWord);
}
/// <summary>
/// Get the most appropriate WfiGloss from among those belonging to the given WfiAnalysis.
/// The goal is to match as closely as possible the strings found in sda.get_MultiStringAlt(hvoWord, ktagSbWordGloss, ws)
/// for each ws in wsIds. Ideally we find an alternative where all writing systems match exactly.
/// Failing that, one where as many as possible match, and the others are blank.
/// </summary>
/// <param name="wfiAnalysis"></param>
/// <param name="wsIds"></param>
/// <param name="sda"></param>
/// <param name="hvoWord"></param>
/// <returns></returns>
internal static IWfiGloss GetBestGloss(IWfiAnalysis wfiAnalysis, List<int> wsIds, ISilDataAccess sda, int hvoWord)
{
IWfiGloss best = null;
int cBestMatch = 0;
int cBestBlanks = 0; // number of non-matching alternatives that are blank, in the best match so far
foreach (IWfiGloss possibleGloss in wfiAnalysis.MeaningsOC)
{
int cMatch = 0;
int cBlanks = 0; // number of non-matching alternatives that are blank, in the current item.
foreach (int wsId in wsIds)
{
ITsString tssGloss = sda.get_MultiStringAlt(hvoWord, ktagSbWordGloss, wsId);
ITsString tssMainGloss = possibleGloss.Form.get_String(wsId);
string sdaGloss = tssGloss.Text;
string smainGloss = tssMainGloss.Text;
if (tssGloss.Equals(tssMainGloss))
cMatch++;
else if (tssMainGloss.Length == 0)
cBlanks++;
else
{
cMatch = int.MinValue; // non-matching alternative on the WfiGloss, this one is no good for sure
cBlanks = int.MinValue;
}
}
// This one is better if:
// - it has more matches
// - it has as many matches and more relevant empty alternatives
// - it is as good otherwise and has fewer non-relevant alternatives.
if (cMatch > cBestMatch
|| (cMatch == cBestMatch && cBlanks > cBestBlanks)
|| (cMatch == cBestMatch && cBlanks == cBestBlanks && best != null && AdditionalAlternatives(possibleGloss, wsIds) < AdditionalAlternatives(best, wsIds)))
{
cBestMatch = cMatch;
cBestBlanks = cBlanks;
best = possibleGloss;
}
}
// we won't return something where nothing matched, just because it had a blank alternative
return cBestMatch > 0 ? best : null;
}
/// <summary>
/// Count how many non-blank alternatives tss has that are not listed in wsIds.
/// </summary>
/// <param name="tss"></param>
/// <param name="wsIds"></param>
/// <returns></returns>
static int AdditionalAlternatives(IWfiGloss wg, List<int> wsIds)
{
int result = 0;
foreach (var ws in wg.Form.AvailableWritingSystemIds)
{
if (wsIds.Contains(ws))
continue;
if (wg.Form.get_String(ws).Length > 0)
result++;
}
return result;
}
/// <summary>
/// Find the analysis that matches the info in the secondary cache.
/// </summary>
/// <param name="fExactMatch"></param>
/// <returns></returns>
public IWfiAnalysis FindMatchingAnalysis(bool fExactMatch)
{
foreach (IWfiAnalysis possibleAnalysis in m_wf.AnalysesOC)
{
if (fExactMatch)
{
if (CheckAnalysis(possibleAnalysis, true))
return possibleAnalysis;
}
else
{
// If this possibility is Human evaluated, it must match exactly regardless
// of the input parameter to count as a match on the analysis.
bool fIsHumanApproved = SandboxBase.IsAnalysisHumanApproved(m_caches.MainCache, possibleAnalysis);
if (CheckAnalysis(possibleAnalysis, fIsHumanApproved))
return possibleAnalysis;
}
}
if (fExactMatch)
return null;
// in this inexact case, another way to match is to have correct gloss(es) and trivial analysis.
// Todo JohnT: do this and adjust caller.
foreach (var possibleAnalysis in m_wf.AnalysesOC)
{
if (!IsTrivialAnalysis(possibleAnalysis))
continue;
foreach (var gloss in possibleAnalysis.MeaningsOC)
{
if (MatchesCurrentGlosses(gloss))
{
// We want to reuse this gloss. If possible we will reuse and modify this
// analalysis. However, if it has other glosses, we don't want to change them.
if (possibleAnalysis.MeaningsOC.Count == 1)
return possibleAnalysis;
var result = possibleAnalysis.Services.GetInstance<IWfiAnalysisFactory>().Create();
m_wf.AnalysesOC.Add(result);
result.MeaningsOC.Add(gloss); // moves it to its own new private analysis
return result;
}
}
}
return null; // no match found.
}
private bool MatchesCurrentGlosses(IWfiGloss gloss)
{
foreach (int wsId in m_choices.EnabledWritingSystemsForFlid(InterlinLineChoices.kflidWordGloss))
{
ITsString tssGloss = m_sda.get_MultiStringAlt(m_hvoSbWord, ktagSbWordGloss, wsId);
if (!tssGloss.Equals(gloss.Form.get_String(wsId)))
return false;
}
return true;
}
/// <summary>
/// A trivial analysis, the sort created when just doing word glossing, doesn't really specify anything,
/// though it may have one morph bundle that just matches the whole wordform.
/// It may possibly specify a part of speech, in which case it must match the one the user entered
/// to be reusable.
/// </summary>
/// <param name="possibleAnalysis"></param>
/// <returns></returns>
private bool IsTrivialAnalysis(IWfiAnalysis possibleAnalysis)
{
if (possibleAnalysis.CategoryRA != null