private static void UpgradeToWordGloss(Word word, ref IAnalysis analysis)
{
FdoCache cache = analysis.Cache;
var tsStrFactory = cache.ServiceLocator.GetInstance<ITsStrFactory>();
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 = tsStrFactory.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;
}
}
}
}