-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
BIRDInterlinearImporter.cs
1027 lines (969 loc) · 38 KB
/
BIRDInterlinearImporter.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
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-2018 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;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SIL.LCModel.Core.Text;
using SIL.LCModel.Core.WritingSystems;
using SIL.LCModel.Core.KernelInterfaces;
using SIL.FieldWorks.Common.FwUtils;
using SIL.LCModel;
using SIL.LCModel.DomainServices;
using SIL.FieldWorks.IText.FlexInterlinModel;
using SIL.LCModel.Application.ApplicationServices;
using SIL.LCModel.Core.Cellar;
using SIL.LCModel.Infrastructure;
using SIL.LCModel.Utils;
using Gecko.WebIDL;
using SIL.FieldWorks.Common.Framework.DetailControls;
using SIL.Extensions;
using SIL.Machine.DataStructures;
namespace SIL.FieldWorks.IText
{
public partial class LinguaLinksImport
{
//this delegate is used for alerting the user of new writing systems found in the import
//or a text that is already found.
private delegate DialogResult ShowDialogAboveProgressbarDelegate(IThreadedProgress progress,
string text, string title, MessageBoxButtons buttons);
private static ImportInterlinearOptions s_importOptions;
private static Dictionary<string, ILgWritingSystem> s_wsMapper = new Dictionary<string, ILgWritingSystem>();
/// <summary>
/// This method will display a message box above the progress dialog.
/// </summary>
/// <param name="progress"></param>
/// <param name="text"></param>
/// <param name="title"></param>
/// <param name="buttons"></param>
/// <returns></returns>
private static DialogResult ShowDialogAboveProgressbar(IThreadedProgress progress,
string text, string title, MessageBoxButtons buttons)
{
return MessageBox.Show(
text,
title,
buttons,
MessageBoxIcon.Warning);
}
internal class TextCreationParams
{
internal Interlineartext InterlinText;
internal LcmCache Cache;
internal IThreadedProgress Progress;
internal ImportInterlinearOptions ImportOptions;
internal int Version;
}
/// <summary>
/// This method will create a new Text document from the given BIRD format Interlineartext. If this fails
/// for some reason then return false to tell the calling method to abort the import.
/// </summary>
/// <param name="newText">The text to populate, could be set to null.</param>
/// <param name="textParams">This contains the interlinear text.</param>
/// <returns>The imported text may be in a writing system that is not part of this project. Return false if the user
/// rejects the text which tells the caller of this method to abort the import.</returns>
private static bool PopulateTextFromBIRDDoc(ref LCModel.IText newText, TextCreationParams textParams)
{
s_importOptions = textParams.ImportOptions;
Interlineartext interlinText = textParams.InterlinText;
LcmCache cache = textParams.Cache;
IThreadedProgress progress = textParams.Progress;
if (s_importOptions.CheckAndAddLanguages == null)
s_importOptions.CheckAndAddLanguages = CheckAndAddLanguagesInternal;
ILgWritingSystemFactory wsFactory = cache.WritingSystemFactory;
const char space = ' ';
//handle the languages(writing systems) section alerting the user if new writing systems are encountered
if (!s_importOptions.CheckAndAddLanguages(cache, interlinText, wsFactory, progress))
return false;
//handle the header(info or meta) information
SetTextMetaAndMergeMedia(cache, interlinText, wsFactory, newText, false);
//create all the paragraphs
foreach (var paragraph in interlinText.paragraphs)
{
if (newText.ContentsOA == null)
{
newText.ContentsOA = cache.ServiceLocator.GetInstance<IStTextFactory>().Create();
}
IStTxtPara newTextPara = newText.ContentsOA.AddNewTextPara("");
int offset = 0;
if (paragraph.phrases == null)
{
continue;
}
foreach (var phrase in paragraph.phrases)
{
ICmObject oldSegment = null;
//Try and locate a segment with this Guid.
if (!String.IsNullOrEmpty(phrase.guid))
{
if (cache.ServiceLocator.ObjectRepository.TryGetObject(new Guid(phrase.guid), out oldSegment))
{
//We aren't merging, but we have this guid in our system; ignore the file Guid
oldSegment = cache.ServiceLocator.GetInstance<ISegmentFactory>().Create(newTextPara, offset);
}
else
{
//The segment is identified by a Guid, but apparently we don't have it in our current document, so make one with the guid
oldSegment = cache.ServiceLocator.GetInstance<ISegmentFactory>().Create(newTextPara, offset, cache,
new Guid(phrase.guid));
}
}
//set newSegment to the old, or create a brand new one.
ISegment newSegment = oldSegment as ISegment ?? cache.ServiceLocator.GetInstance<ISegmentFactory>().Create(newTextPara, offset);
//Fill in the ELAN time information if it is present.
AddELANInfoToSegment(cache, phrase, newSegment);
ITsString phraseText = null;
bool textInFile = false;
//Add all of the data from <item> elements into the segment.
AddSegmentItemData(cache, wsFactory, phrase, newSegment, ref textInFile, ref phraseText);
bool lastWasWord = false;
if (phrase.WordsContent != null && phrase.WordsContent.Words != null)
{
if (textParams.Version == 0 && PhraseHasExactlyOneTxtItemNotAKnownWordform(newSegment.Cache, phrase))
{
// It might be a SayMore text that makes the whole segment a single txt item.
// We want to add the text anyway (unless a higher level did so), but we will skip making
// a wordform. Eventual parsing of the text will do so.
if (!textInFile)
{
UpdatePhraseTextForWordItems(wsFactory, ref phraseText, phrase.WordsContent.Words[0], ref lastWasWord, space);
}
}
else
{
foreach (var word in phrase.WordsContent.Words)
{
//If the text of the phrase was not given in the document build it from the words.
if (!textInFile)
{
UpdatePhraseTextForWordItems(wsFactory, ref phraseText, word, ref lastWasWord, space);
}
AddWordToSegment(newSegment, word);
}
}
}
UpdateParagraphTextForPhrase(newTextPara, ref offset, phraseText);
}
}
return true;
}
/// <summary>
/// Return true if the phrase has exactly one word which has exactly one item of type txt,
/// and that item is not a known wordform.
/// </summary>
/// <param name="fdoCache"></param>
/// <param name="phrase"></param>
/// <returns></returns>
private static bool PhraseHasExactlyOneTxtItemNotAKnownWordform(LcmCache fdoCache, Phrase phrase)
{
if (phrase.WordsContent.Words.Length != 1 || phrase.WordsContent.Words[0].Items.Length != 1
|| phrase.WordsContent.Words[0].Items[0].type != "txt")
return false;
var wsFact = fdoCache.WritingSystemFactory;
var wordItem = phrase.WordsContent.Words[0].Items[0];
int ws = GetWsEngine(wsFact, wordItem.lang).Handle;
if (string.IsNullOrEmpty(wordItem.Value))
return true; // if it has no text, it can't be a known wordform...
var wf =
fdoCache.ServiceLocator.GetInstance<IWfiWordformRepository>().GetMatchingWordform(ws, wordItem.Value);
return wf == null;
}
/// <summary>
/// Merge the contents of the given Text into the exising one. If this fails
/// for some reason then return false to tell the calling method to abort the import.
/// </summary>
/// <param name="newText"></param>
/// <param name="textParams"></param>
/// <returns>The imported text may be in a writing system that is not part of this project. Return false if the user
/// rejects the text which tells the caller of this method to abort the import.</returns>
private static bool MergeTextWithBIRDDoc(ref LCModel.IText newText, TextCreationParams textParams)
{
s_importOptions = textParams.ImportOptions;
Interlineartext interlinText = textParams.InterlinText;
LcmCache cache = textParams.Cache;
IThreadedProgress progress = textParams.Progress;
if (s_importOptions.CheckAndAddLanguages == null)
s_importOptions.CheckAndAddLanguages = CheckAndAddLanguagesInternal;
ILgWritingSystemFactory wsFactory = cache.WritingSystemFactory;
char space = ' ';
//handle the languages(writing systems) section alerting the user if new writing systems are encountered
if (!s_importOptions.CheckAndAddLanguages(cache, interlinText, wsFactory, progress))
return false;
//handle the header(info or meta) information as well as any media-files sections
SetTextMetaAndMergeMedia(cache, interlinText, wsFactory, newText, true);
IStText newContents = newText.ContentsOA;
//create or reuse the paragraphs available. NOTE: Currently the paragraph guids are being ignored, this might be wrong.
foreach (var paragraph in interlinText.paragraphs)
{
IStTxtPara newTextPara = null;
if (newContents == null)
{
newContents = cache.ServiceLocator.GetInstance<IStTextFactory>().Create();
newText.ContentsOA = newContents;
newTextPara = newContents.AddNewTextPara("");
}
int offset = 0;
if (paragraph.phrases == null)
{
continue;
}
foreach (var phrase in paragraph.phrases)
{
ICmObject oldSegment = null;
//Try and locate a segment with this Guid. Assign newTextPara to the paragraph we're working on if we haven't already
if(!String.IsNullOrEmpty(phrase.guid))
{
if (cache.ServiceLocator.ObjectRepository.TryGetObject(new Guid(phrase.guid), out oldSegment))
{
if (oldSegment as ISegment != null) //The segment matches, add it into our paragraph.
{
IStTxtPara segmentOwner = newContents.ParagraphsOS.FirstOrDefault(para =>
para.Guid.Equals((oldSegment as ISegment).Owner.Guid)) as IStTxtPara;
if (segmentOwner != null && newTextPara == null) //We found the StTxtPara that correspond to this paragraph
newTextPara = segmentOwner;
}
else if (oldSegment == null) //The segment is identified by a Guid, but apparently we don't have it in our current document, so make one
{
if (newTextPara == null)
newTextPara = newContents.AddNewTextPara("");
oldSegment = cache.ServiceLocator.GetInstance<ISegmentFactory>().Create(newTextPara, offset, cache, new Guid(phrase.guid));
}
else //The Guid is in use, but not by a segment. This is bad.
{
return false;
}
}
}
//If newTextPara is null, try to use a paragraph that a sibling phrase belongs to.
//Note: newTextPara is only assigned once, and won't be reassigned until we iterate through a new paragraph.
if (newTextPara == null)
{
var phraseGuids = paragraph.phrases.Select(p => p.guid);
foreach (IStTxtPara para in newContents.ParagraphsOS)
{
if (para.SegmentsOS.Any(seg => phraseGuids.Contains(seg.Guid.ToString())))
{
newTextPara = para;
break;
}
}
}
//Can't find any paragraph for our phrase, create a brand new paragraph
if (newTextPara == null)
newTextPara = newContents.AddNewTextPara("");
//set newSegment to the old, or create a brand new one.
ISegment newSegment = oldSegment as ISegment;
if (newSegment == null)
{
if (!string.IsNullOrEmpty(phrase.guid))
{
//The segment is identified by a Guid, but apparently we don't have it in our current document, so make one with the guid
newSegment = cache.ServiceLocator.GetInstance<ISegmentFactory>().Create(newTextPara, offset, cache, new Guid(phrase.guid));
}
else
newSegment = cache.ServiceLocator.GetInstance<ISegmentFactory>().Create(newTextPara, offset);
}
//Fill in the ELAN time information if it is present.
AddELANInfoToSegment(cache, phrase, newSegment);
ITsString phraseText = null;
bool textInFile = false;
//Add all of the data from <item> elements into the segment.
AddSegmentItemData(cache, wsFactory, phrase, newSegment, ref textInFile, ref phraseText);
bool lastWasWord = false;
if (phrase.WordsContent != null && phrase.WordsContent.Words != null)
{
//Rewrite our analyses
newSegment.AnalysesRS.Clear();
foreach (var word in phrase.WordsContent.Words)
{
//If the text of the phrase was not found in a "txt" item for this segment then build it from the words.
if (!textInFile)
{
UpdatePhraseTextForWordItems(wsFactory, ref phraseText, word, ref lastWasWord, space);
}
MergeWordToSegment(newSegment, word);
}
}
UpdateParagraphTextForPhrase(newTextPara, ref offset, phraseText);
}
}
return true;
}
/// <summary>
/// This method will update the newTextPara param appending the phraseText and possibly modifying the segment ending
/// to add an end of segment character. The offset parameter will be set to the value where a following segment would start
/// from. The paragraph text will be replaced if the offset is 0.
/// </summary>
/// <param name="newTextPara"></param>
/// <param name="offset"></param>
/// <param name="phraseText"></param>
private static void UpdateParagraphTextForPhrase(IStTxtPara newTextPara, ref int offset, ITsString phraseText)
{
if (phraseText != null && phraseText.Length > 0)
{
var bldr = newTextPara.Contents.GetBldr();
if (offset == 0)
{
bldr.Replace(0, bldr.Length, "", null);
}
offset += phraseText.Length;
var oldText = (bldr.Text ?? "").Trim();
if (oldText.Length > 0 && !TsStringUtils.IsEndOfSentenceChar(oldText[oldText.Length - 1],
Icu.Character.UCharCategory.OTHER_PUNCTUATION))
{
// 'segment' does not end with recognizable EOS character. Add our special one.
bldr.Replace(bldr.Length, bldr.Length, "\x00A7", null);
}
// Insert a space between phrases unless there is already one
if (bldr.Length > 0 && phraseText.Text[0] != ' ' && bldr.Text[bldr.Length - 1] != ' ')
bldr.Replace(bldr.Length, bldr.Length, " ", null);
bldr.ReplaceTsString(bldr.Length, bldr.Length, phraseText);
newTextPara.Contents = bldr.GetString();
}
}
private static ILgWritingSystem GetWsEngine(ILgWritingSystemFactory wsFactory, string langCode)
{
ILgWritingSystem result;
if (s_wsMapper.TryGetValue(langCode, out result))
return result;
return wsFactory.get_Engine(langCode);
}
/// <summary>
/// This method will update the phraseText ref item with the contents of the item entries under the word
/// </summary>
private static void UpdatePhraseTextForWordItems(ILgWritingSystemFactory wsFactory, ref ITsString phraseText, Word word, ref bool lastWasWord, char space)
{
bool isWord = false;
foreach (var item in word.Items)
{
switch (item.type)
{
case "txt": //intentional fallthrough
isWord = true;
goto case "punct";
case "punct":
ITsString wordString = TsStringUtils.MakeString(item.Value,
GetWsEngine(wsFactory, item.lang).Handle);
if (phraseText == null)
{
phraseText = wordString;
}
else
{
var phraseBldr = phraseText.GetBldr();
if (lastWasWord && isWord) //two words next to each other deserve a space between
{
phraseBldr.ReplaceTsString(phraseText.Length, phraseText.Length,
TsStringUtils.MakeString("" + space, GetWsEngine(wsFactory, item.lang).Handle));
}
else if (!isWord) //handle punctuation
{
wordString = GetSpaceAdjustedPunctString(wsFactory,
item, wordString, space, lastWasWord);
}
phraseBldr.ReplaceTsString(phraseBldr.Length, phraseBldr.Length, wordString);
phraseText = phraseBldr.GetString();
}
lastWasWord = isWord;
return; // only handle the baseline "txt" or "punct" once per "word" bundle, especially don't want extra writing system content in the baseline.
}
}
}
/// <summary>
/// Add all the data from items in the FLExText file into their proper spots in the segment.
/// </summary>
/// <param name="cache"></param>
/// <param name="wsFactory"></param>
/// <param name="phrase"></param>
/// <param name="newSegment"></param>
/// <param name="textInFile">This reference boolean indicates if there was a text item in the phrase</param>
/// <param name="phraseText">This reference string will be filled with the contents of the "txt" item in the phrase if it is there</param>
private static void AddSegmentItemData(LcmCache cache, ILgWritingSystemFactory wsFactory, Phrase phrase, ISegment newSegment, ref bool textInFile, ref ITsString phraseText)
{
if (phrase.Items != null)
{
foreach (var item in phrase.Items)
{
switch (item.type)
{
case "reference-label":
newSegment.Reference = TsStringUtils.MakeString(item.Value,
GetWsEngine(wsFactory, item.lang).Handle);
break;
case "gls":
newSegment.FreeTranslation.set_String(GetWsEngine(wsFactory, item.lang).Handle, item.Value);
break;
case "lit":
newSegment.LiteralTranslation.set_String(GetWsEngine(wsFactory, item.lang).Handle, item.Value);
break;
case "note":
int ws = GetWsEngine(wsFactory, item.lang).Handle;
INote newNote = newSegment.NotesOS.FirstOrDefault(note => note.Content.get_String(ws).Text == item.Value);
if (newNote == null)
{
newNote = cache.ServiceLocator.GetInstance<INoteFactory>().Create();
newSegment.NotesOS.Add(newNote);
newNote.Content.set_String(GetWsEngine(wsFactory, item.lang).Handle, item.Value);
}
break;
case "txt":
phraseText = TsStringUtils.MakeString(item.Value, GetWsEngine(wsFactory, item.lang).Handle);
textInFile = true;
break;
case "segnum":
break; // The segnum item is not associated to a property, and also not a custom field. Skip merging it.
default:
var classId = cache.MetaDataCacheAccessor.GetClassId("Segment");
var mdc = (IFwMetaDataCacheManaged) cache.MetaDataCacheAccessor;
foreach (int flid in mdc.GetFields(classId, false, (int) CellarPropertyTypeFilter.All))
{
if (!mdc.IsCustom(flid))
continue;
var customId = mdc.GetFieldId2(classId, item.type, true);
if (customId != 0)
{
var customWs = GetWsEngine(wsFactory, item.lang).Handle;
var customTierText = TsStringUtils.MakeString(item.Value, customWs);
cache.MainCacheAccessor.SetString(newSegment.Hvo, customId, customTierText);
}
}
break;
}
}
}
}
private static void AddELANInfoToSegment(LcmCache cache, Phrase phrase, ISegment newSegment)
{
if (!String.IsNullOrEmpty(phrase.mediaFile))
{
if (!String.IsNullOrEmpty(phrase.speaker))
{
newSegment.SpeakerRA = FindOrCreateSpeaker(phrase.speaker, cache);
}
newSegment.BeginTimeOffset = phrase.beginTimeOffset;
newSegment.EndTimeOffset = phrase.endTimeOffset;
newSegment.MediaURIRA = cache.ServiceLocator.ObjectRepository.GetObject(new Guid(phrase.mediaFile)) as ICmMediaURI;
}
}
private static ICmPerson FindOrCreateSpeaker(string speaker, LcmCache cache)
{
if(cache.LanguageProject.PeopleOA != null)
{
//find and return a person in this project whose name matches the speaker
foreach (var person in cache.LanguageProject.PeopleOA.PossibilitiesOS)
{
if (person.Name.BestVernacularAnalysisAlternative.Text.Equals(speaker))
{
return (ICmPerson)person;
}
}
}
else
{
cache.LanguageProject.PeopleOA = cache.ServiceLocator.GetInstance<ICmPossibilityListFactory>().Create();
}
//person not found create one and add it.
var newPerson = cache.ServiceLocator.GetInstance<ICmPersonFactory>().Create();
cache.LanguageProject.PeopleOA.PossibilitiesOS.Add(newPerson);
newPerson.Name.set_String(cache.DefaultVernWs, speaker);
return newPerson;
}
private static void MergeWordToSegment(ISegment newSegment, Word word)
{
if(!String.IsNullOrEmpty(word.guid))
{
ICmObject repoObj;
newSegment.Cache.ServiceLocator.ObjectRepository.TryGetObject(new Guid(word.guid), out repoObj);
IAnalysis modelWord = repoObj as IAnalysis;
if(modelWord != null)
{
UpgradeToWordGloss(word, ref modelWord);
newSegment.AnalysesRS.Add(modelWord);
}
else
{
AddWordToSegment(newSegment, word);
}
}
else
{
AddWordToSegment(newSegment, word);
}
}
private static bool SomeLanguageSpecifiesVernacular(Interlineartext interlinText)
{
// return true if any language in the languages section is vernacular
return interlinText.languages.language.Any(lang => lang.vernacularSpecified);
}
/// <summary>
/// The imported text may be in a writing system that is not part of this project. Return false if the user
/// rejects the text which tells the caller of this method to abort the import.
/// </summary>
/// <param name="cache"></param>
/// <param name="interlinText"></param>
/// <param name="wsFactory"></param>
/// <param name="progress"></param>
/// <returns>return false to abort import</returns>
private static bool CheckAndAddLanguagesInternal(LcmCache cache, Interlineartext interlinText, ILgWritingSystemFactory wsFactory, IThreadedProgress progress)
{
DialogResult result;
if (interlinText.languages != null && interlinText.languages.language != null)
{
if (!SomeLanguageSpecifiesVernacular(interlinText))
{
// Saymore file? something else that doesn't know to do this? We will confuse the user if we try to treat all as analysis.
SetVernacularLanguagesByUsage(interlinText);
}
foreach (var lang in interlinText.languages.language)
{
bool fIsVernacular;
var writingSystem = SafelyGetWritingSystem(cache, wsFactory, lang, out fIsVernacular);
if (fIsVernacular)
{
if (!cache.LanguageProject.CurrentVernacularWritingSystems.Contains(writingSystem.Handle))
{
//we need to invoke the dialog on the main thread so we can use the progress dialog as the parent.
//otherwise the message box can be displayed behind everything
var instructions = GetInstructions(interlinText, writingSystem.LanguageName, ITextStrings.ksImportVernacLangMissing);
IAsyncResult asyncResult = progress.SynchronizeInvoke.BeginInvoke(new ShowDialogAboveProgressbarDelegate(ShowDialogAboveProgressbar),
new object[]
{
progress,
instructions,
ITextStrings.ksImportVernacLangMissingTitle,
MessageBoxButtons.OKCancel
});
result = (DialogResult)progress.SynchronizeInvoke.EndInvoke(asyncResult);
if (result == DialogResult.OK)
{
cache.LanguageProject.AddToCurrentVernacularWritingSystems((CoreWritingSystemDefinition) writingSystem);
}
else if (result == DialogResult.Cancel)
{
return false;
}
}
}
else
{
if (!cache.LanguageProject.CurrentAnalysisWritingSystems.Contains(writingSystem.Handle))
{
var instructions = GetInstructions(interlinText, writingSystem.LanguageName,
ITextStrings.ksImportAnalysisLangMissing);
IAsyncResult asyncResult = progress.SynchronizeInvoke.BeginInvoke(new ShowDialogAboveProgressbarDelegate(ShowDialogAboveProgressbar),
new object[]
{
progress,
instructions,
ITextStrings.ksImportAnalysisLangMissingTitle,
MessageBoxButtons.OKCancel
});
result = (DialogResult)progress.SynchronizeInvoke.EndInvoke(asyncResult);
//alert the user
if (result == DialogResult.OK)
{
//alert the user
cache.LanguageProject.AddToCurrentAnalysisWritingSystems((CoreWritingSystemDefinition) writingSystem);
// We already have progress indications up.
XmlTranslatedLists.ImportTranslatedListsForWs(writingSystem.Id, cache, FwDirectoryFinder.TemplateDirectory, null);
}
else if (result == DialogResult.Cancel)
{
return false;
}
}
}
}
}
return true;
}
private static string GetInstructions(Interlineartext interlinText, String wsName, String instructions)
{
var strBldr = new StringBuilder(wsName);
strBldr.Append(instructions);
strBldr.Append(Environment.NewLine); strBldr.Append(Environment.NewLine);
strBldr.Append(GetPartOfPhrase(interlinText));
return strBldr.ToString();
}
private static string GetPartOfPhrase(Interlineartext interlinText)
{
int i = 0;
var strBldr = new StringBuilder(ITextStrings.ksImportLangMissingTextStartsWith);
foreach (var paragraph in interlinText.paragraphs)
{
foreach (var phrase in paragraph.phrases)
{
foreach (var word in phrase.WordsContent.Words)
{
strBldr.Append(word.Items[0].Value);
strBldr.Append(" ");
i++;
if (i > 6)
{
strBldr.Append(" ...");
return strBldr.ToString();
}
}
}
}
return strBldr.ToString();
}
private static void SetVernacularLanguagesByUsage(Interlineartext interlinText)
{
foreach (var para in interlinText.paragraphs)
{
if(para.phrases == null) // if there are no phrases, they have no languages we are interested in.
{
continue;
}
foreach (var phrase in para.phrases)
{
foreach (var item in phrase.Items)
{
if (item.type == "txt")
EnsureVernacularLanguage(interlinText, item.lang);
}
if(phrase.WordsContent.Words != null)
{
foreach (var word in phrase.WordsContent.Words)
{
foreach (var item in word.Items)
{
if (item.type == "txt")
EnsureVernacularLanguage(interlinText, item.lang);
}
// We could dig into the morphemes, but any client generating morphemes probably
// does things right, and anyway we don't import that yet.
}
}
}
}
}
private static void EnsureVernacularLanguage(Interlineartext interlinText,string langName)
{
foreach (var lang in interlinText.languages.language)
{
if (lang.lang == langName)
{
lang.vernacularSpecified = true;
lang.vernacular = true;
return;
}
}
}
private static ILgWritingSystem SafelyGetWritingSystem(LcmCache cache, ILgWritingSystemFactory wsFactory,
FlexInterlinModel.Language lang, out bool fIsVernacular)
{
fIsVernacular = lang.vernacularSpecified && lang.vernacular;
ILgWritingSystem writingSystem = null;
try
{
writingSystem = wsFactory.get_Engine(lang.lang);
}
catch (ArgumentException e)
{
CoreWritingSystemDefinition ws;
WritingSystemServices.FindOrCreateSomeWritingSystem(cache, FwDirectoryFinder.TemplateDirectory, lang.lang,
!fIsVernacular, fIsVernacular, out ws);
writingSystem = ws;
s_wsMapper.Add(lang.lang, writingSystem); // old id string -> new langWs mapping
}
return writingSystem;
}
private static void AddWordToSegment(ISegment newSegment, Word word)
{
//use the items under the word to determine what kind of thing to add to the segment
var cache = newSegment.Cache;
IAnalysis analysis = CreateWordAnalysisStack(cache, word);
// Add to segment
if (analysis != null)
{
newSegment.AnalysesRS.Add(analysis);
}
}
private static IAnalysis CreateWordAnalysisStack(LcmCache cache, Word word)
{
if (word.Items == null || word.Items.Length <= 0) return null;
IAnalysis analysis = null;
var wsFact = cache.WritingSystemFactory;
ILgWritingSystem wsMainVernWs = null;
IWfiMorphBundle bundle = null;
foreach (var wordItem in word.Items)
{
ITsString wordForm = null;
switch (wordItem.type)
{
case "txt":
wsMainVernWs = GetWsEngine(wsFact, wordItem.lang);
wordForm = TsStringUtils.MakeString(wordItem.Value, wsMainVernWs.Handle);
analysis = WfiWordformServices.FindOrCreateWordform(cache, wordForm);
break;
case "punct":
wordForm = TsStringUtils.MakeString(wordItem.Value,
GetWsEngine(wsFact, wordItem.lang).Handle);
analysis = WfiWordformServices.FindOrCreatePunctuationform(cache, wordForm);
break;
}
if (wordForm != null)
break;
}
// now add any alternative word forms. (overwrite any existing)
if (analysis != null && analysis.HasWordform)
{
AddAlternativeWssToWordform(analysis, word, wsMainVernWs);
}
if (analysis != null)
{
UpgradeToWordGloss(word, ref analysis);
}
else
{
Debug.Assert(analysis != null, "What else could this do?");
}
// Fill in morphemes, lex. entries, lex. gloss, and lex.gram.info
if (word.morphemes != null && word.morphemes.morphs.Length > 0)
{
int morphIdx = 0;
foreach (var morpheme in word.morphemes.morphs)
{
ILexEntryRepository lex_entry_repo = cache.ServiceLocator.GetInstance<ILexEntryRepository>();
IMoMorphSynAnalysisRepository msa_repo = cache.ServiceLocator.GetInstance<IMoMorphSynAnalysisRepository>();
var itemDict = new Dictionary<string, Tuple<string, string>>();
if (analysis.Analysis == null)
{
break;
}
foreach (item item in morpheme.items)
{
itemDict[item.type] = new Tuple<string, string>(item.lang, item.Value);
}
if (itemDict.ContainsKey("txt")) // Morphemes
{
int ws = GetWsEngine(wsFact, itemDict["txt"].Item1).Handle;
ITsString wf = TsStringUtils.MakeString(itemDict["txt"].Item2, ws);
// If we already have a bundle use that one
bundle =
analysis.Analysis.MorphBundlesOS.FirstOrDefault(b => b.Form.get_String(ws).Text == itemDict["txt"].Item2);
if (bundle == null)
{
// Otherwise create a new bundle and add it to analysis
bundle = cache.ServiceLocator.GetInstance<IWfiMorphBundleFactory>().Create();
if (analysis.Analysis.MorphBundlesOS.Count >= word.morphemes.morphs.Length)
{
analysis.Analysis.MorphBundlesOS.RemoveAt(morphIdx);
}
analysis.Analysis.MorphBundlesOS.Insert(morphIdx, bundle);
}
bundle.Form.set_String(ws, wf);
}
if (itemDict.ContainsKey("cf")) // Lex. Entries
{
int ws_cf = GetWsEngine(wsFact, itemDict["cf"].Item1).Handle;
var entries = lex_entry_repo.AllInstances().Where(
m => StringServices.CitationFormWithAffixTypeStaticForWs(m, ws_cf, string.Empty) == itemDict["cf"].Item2);
if (entries != null)
{
ILexEntry entry = null;
if (entries.Count() == 1)
{
entry = entries.First();
}
else if (itemDict.ContainsKey("hn")) // Homograph Number
{
entry = entries.FirstOrDefault(m => m.HomographNumber.ToString() == itemDict["hn"].Item2);
}
else // Didn't find a match
{
break;
}
bundle.MorphRA = entry.LexemeFormOA;
if (itemDict.ContainsKey("gls")) // Lex. Gloss
{
int ws_gls = GetWsEngine(wsFact, itemDict["gls"].Item1).Handle;
ILexSense sense = entry.SensesOS.FirstOrDefault(s => s.Gloss.get_String(ws_gls).Text == itemDict["gls"].Item2);
if (sense != null)
{
bundle.SenseRA = sense;
}
}
}
}
if (itemDict.ContainsKey("msa")) // Lex. Gram. Info
{
IMoMorphSynAnalysis match = msa_repo.AllInstances().FirstOrDefault(m => m.InterlinearAbbr == itemDict["msa"].Item2);
if (match != null)
{
bundle.MsaRA = match;
}
}
morphIdx++;
}
}
return analysis;
}
/// <summary>
/// add any alternative forms (in alternative writing systems) to the wordform.
/// Overwrite any existing alternative form in a given alternative writing system.
/// </summary>
private static void AddAlternativeWssToWordform(IAnalysis analysis, Word word, ILgWritingSystem wsMainVernWs)
{
ILgWritingSystemFactory wsFact = analysis.Cache.WritingSystemFactory;
var wf = analysis.Wordform;
foreach (var wordItem in word.Items)
{
switch (wordItem.type)
{
case "txt":
var wsAlt = GetWsEngine(wsFact, wordItem.lang);
if (wsAlt.Handle == wsMainVernWs.Handle)
continue;
ITsString wffAlt = TsStringUtils.MakeString(wordItem.Value, wsAlt.Handle);
if (wffAlt.Length > 0)
wf.Form.set_String(wsAlt.Handle, wffAlt);
break;
}
}
}
/// <summary>
///
/// </summary>
/// <param name="word"></param>
/// <param name="analysis">the new analysis Gloss. If multiple glosses, returns the last one created.</param>
private static void UpgradeToWordGloss(Word word, ref IAnalysis analysis)
{
LcmCache cache = analysis.Cache;
var wsFact = cache.WritingSystemFactory;
if (s_importOptions.AnalysesLevel == ImportAnalysesLevel.WordGloss)
{
// test for adding multiple glosses in the same language. If so, create separate analyses with separate glosses.
bool fHasMultipleGlossesInSameLanguage = false;
var dictMapLangToGloss = new Dictionary<string, string>();
foreach (var wordGlossItem in word.Items.Select(i => i).Where(i => i.type == "gls"))
{
string gloss;
if (!dictMapLangToGloss.TryGetValue(wordGlossItem.lang, out gloss))
{
dictMapLangToGloss.Add(wordGlossItem.lang, wordGlossItem.Value);
continue;
}
if (wordGlossItem.Value == gloss) continue;
fHasMultipleGlossesInSameLanguage = true;
break;
}
AnalysisTree analysisTree = null;
foreach (var wordGlossItem in word.Items.Select(i => i).Where(i => i.type == "gls"))
{
if (wordGlossItem == null) continue;
if (wordGlossItem.analysisStatusSpecified &&
wordGlossItem.analysisStatus != analysisStatusTypes.humanApproved) continue;
// first make sure that an existing gloss does not already exist. (i.e. don't add duplicate glosses)
int wsNewGloss = GetWsEngine(wsFact, wordGlossItem.lang).Handle;
ITsString newGlossTss = TsStringUtils.MakeString(wordGlossItem.Value,
wsNewGloss);
var wfiWord = analysis.Wordform;
bool hasGlosses = wfiWord.AnalysesOC.Any(wfia => wfia.MeaningsOC.Any());
IWfiGloss matchingGloss = null;
if (hasGlosses)
{
foreach (var wfa in wfiWord.AnalysesOC)
{
matchingGloss = wfa.MeaningsOC.FirstOrDefault(wfg => wfg.Form.get_String(wsNewGloss).Equals(newGlossTss));
if (matchingGloss != null)
break;
}
}
if (matchingGloss != null)
analysis = matchingGloss;
else
{
// TODO: merge with analysis having same morpheme breakdown (or at least the same stem)
if (analysisTree == null || dictMapLangToGloss.Count == 1 || fHasMultipleGlossesInSameLanguage)
{
// create a new WfiAnalysis to store a new gloss
analysisTree = WordAnalysisOrGlossServices.CreateNewAnalysisTreeGloss(wfiWord);
}
// else, reuse the same analysisTree for setting a gloss alternative
analysisTree.Gloss.Form.set_String(wsNewGloss, wordGlossItem.Value);
// Make sure this analysis is marked as user-approved (green check mark)
cache.LangProject.DefaultUserAgent.SetEvaluation(analysisTree.WfiAnalysis, Opinions.approves);
// Create a morpheme form that matches the wordform.
var morphemeBundle = cache.ServiceLocator.GetInstance<IWfiMorphBundleFactory>().Create();
var wordItem = word.Items.Select(i => i).First(i => i.type == "txt");
int wsWord = GetWsEngine(wsFact, wordItem.lang).Handle;
analysisTree.WfiAnalysis.MorphBundlesOS.Add(morphemeBundle);
morphemeBundle.Form.set_String(wsWord, wordItem.Value);
analysis = analysisTree.Gloss;
}
}
}
}
/// <summary>
/// Set text metadata, create or merge media file URI's.
/// <note>media files (ELAN initiated) need to be processed before the paragraphs, as segments could reference these parts.</note>
/// </summary>
/// <param name="cache"></param>
/// <param name="interlinText">The source text</param>
/// <param name="wsFactory"></param>
/// <param name="newText">The target text</param>
/// <param name="merging">True if we are merging into an existing text; False if we are creating everything new</param>
private static void SetTextMetaAndMergeMedia(LcmCache cache, Interlineartext interlinText, ILgWritingSystemFactory wsFactory,
LCModel.IText newText, bool merging)
{
if (interlinText.Items != null) // apparently it is null if there are no items.
{
foreach (var item in interlinText.Items)
{
switch (item.type)
{
case "title":
newText.Name.set_String(GetWsEngine(wsFactory, item.lang).Handle, item.Value);
break;
case "title-abbreviation":
newText.Abbreviation.set_String(GetWsEngine(wsFactory, item.lang).Handle, item.Value);
break;
case "source":
newText.Source.set_String(GetWsEngine(wsFactory, item.lang).Handle, item.Value);
break;
case "comment":
newText.Description.set_String(GetWsEngine(wsFactory, item.lang).Handle, item.Value);
break;
}
}
}
if (interlinText.mediafiles != null)
{
if (newText.MediaFilesOA == null)
newText.MediaFilesOA = cache.ServiceLocator.GetInstance<ICmMediaContainerFactory>().Create();
newText.MediaFilesOA.OffsetType = interlinText.mediafiles.offsetType;
foreach (var mediaFile in interlinText.mediafiles.media)
{
ICmObject extantObject;