Esempio n. 1
0
        public void GenerateWords_CannotGenerate_ReturnsEmptyEnumerable()
        {
            var any = FeatureStruct.New().Symbol(HCFeatureSystem.Segment).Value;

            var edSuffix = new AffixProcessRule
            {
                Id    = "PL",
                Name  = "ed_suffix",
                Gloss = "PL",
                RequiredSyntacticFeatureStruct = FeatureStruct.New(Language.SyntacticFeatureSystem).Symbol("N").Value
            };

            edSuffix.Allomorphs.Add(new AffixProcessAllomorph
            {
                Lhs = { Pattern <Word, ShapeNode> .New("1").Annotation(any).OneOrMore.Value },
                Rhs = { new CopyFromInput("1"), new InsertSegments(Table3, "+ɯd") }
            });
            Morphophonemic.MorphologicalRules.Add(edSuffix);

            var morpher = new Morpher(TraceManager, Language);

            var analysis = new WordAnalysis(new IMorpheme[] { Entries["32"], edSuffix }, 0, "V");

            Assert.That(morpher.GenerateWords(analysis), Is.Empty);
        }
        public ActionResult Create(string word, string sentence)
        {
            WordAnalysis newWordAnalysis = new WordAnalysis(word, sentence);
            int          finalCount      = newWordAnalysis.CountWordsInPhrase();

            return(View("Index", finalCount));
        }
Esempio n. 3
0
        public void AddWordAnalysis(WordAnalysis lexemes)
        {
            NonUndoableUnitOfWorkHelper.Do(m_cache.ActionHandlerAccessor, () =>
            {
                IWfiWordform wordform;
                if (!TryGetWordform(lexemes.Word, out wordform))
                {
                    wordform = m_cache.ServiceLocator.GetInstance <IWfiWordformFactory>().Create(
                        TsStringUtils.MakeString(lexemes.Word.Normalize(NormalizationForm.FormD), DefaultVernWs));
                }

                IWfiAnalysis analysis = m_cache.ServiceLocator.GetInstance <IWfiAnalysisFactory>().Create();
                wordform.AnalysesOC.Add(analysis);
                analysis.ApprovalStatusIcon = (int)Opinions.approves;

                foreach (Lexeme lexeme in lexemes)
                {
                    var entryLexeme = (FdoLexEntryLexeme)lexeme;
                    ILexEntry entry;
                    if (TryGetEntry(entryLexeme.Key, out entry))
                    {
                        IWfiMorphBundle mb = m_cache.ServiceLocator.GetInstance <IWfiMorphBundleFactory>().Create();
                        analysis.MorphBundlesOS.Add(mb);
                        mb.MorphRA = entry.LexemeFormOA;
                        mb.SenseRA = entry.SensesOS[0];
                        mb.MsaRA   = entry.SensesOS[0].MorphoSyntaxAnalysisRA;
                    }
                }
            });
        }
Esempio n. 4
0
        public void CountWordsInPhrase_ReturnsNumberOfInstancesOfWordInPhrase_Int()
        {
            WordAnalysis compareStrings = new WordAnalysis("seent", "I seent a dog");
            int          expectedCound  = 1;
            int          actualCount    = compareStrings.CountWordsInPhrase();

            Assert.AreEqual(expectedCound, actualCount);
        }
Esempio n. 5
0
        public void SplitSentence_CompareWordAndSentence_True()
        {
            WordAnalysis  compareStrings = new WordAnalysis("seent", "I seent, a dog");
            List <string> strings        = compareStrings.SplitSentence();
            List <string> expected       = new List <string>()
            {
                "seent"
            };

            CollectionAssert.AreEqual(expected, strings);
        }
Esempio n. 6
0
        public Dictionary <int, String> MorphologyWord(string GetWord)
        {
            TurkishMorphology TrMorpo = GetMorphology(GetWord);              //nlp morpoloji sınıfını tanımla
            WordAnalysis      result  = TrMorpo.Analyze(GetWord.ToString()); //kelime analizi sınıfını tanımal

            foreach (SingleAnalysis analysis in result)                      //tekil analizi yap
            {
                morpo++;
                var Key = analysis.FormatLong();
                Keys.Add(morpo, Key.ToString());
            }

            return(Keys);
        }
Esempio n. 7
0
        public IEnumerable <string> GenerateWords(WordAnalysis wordAnalysis)
        {
            if (wordAnalysis.Morphemes.Count == 0)
            {
                return(Enumerable.Empty <string>());
            }

            List <Morpheme> morphemes       = wordAnalysis.Morphemes.Cast <Morpheme>().ToList();
            var             rootEntry       = (LexEntry)morphemes[wordAnalysis.RootMorphemeIndex];
            var             realizationalFS = new FeatureStruct();
            var             results         = new HashSet <string>();

            foreach (Stack <Morpheme> otherMorphemes in PermuteOtherMorphemes(morphemes,
                                                                              wordAnalysis.RootMorphemeIndex - 1, wordAnalysis.RootMorphemeIndex + 1))
            {
                results.UnionWith(GenerateWords(rootEntry, otherMorphemes, realizationalFS));
            }
            return(results);
        }
Esempio n. 8
0
        public void GenerateWords_CanGenerate_ReturnsCorrectWord()
        {
            var any = FeatureStruct.New().Symbol(HCFeatureSystem.Segment).Value;

            var siPrefix = new AffixProcessRule
            {
                Id    = "3SG",
                Name  = "si_prefix",
                Gloss = "3SG",
                RequiredSyntacticFeatureStruct = FeatureStruct.New(Language.SyntacticFeatureSystem).Symbol("V").Value
            };

            siPrefix.Allomorphs.Add(new AffixProcessAllomorph
            {
                Lhs = { Pattern <Word, ShapeNode> .New("1").Annotation(any).OneOrMore.Value },
                Rhs = { new InsertSegments(Table3, "si+"), new CopyFromInput("1") }
            });
            Morphophonemic.MorphologicalRules.Add(siPrefix);

            var edSuffix = new AffixProcessRule
            {
                Id    = "PAST",
                Name  = "ed_suffix",
                Gloss = "PAST",
                RequiredSyntacticFeatureStruct = FeatureStruct.New(Language.SyntacticFeatureSystem).Symbol("V").Value
            };

            edSuffix.Allomorphs.Add(new AffixProcessAllomorph
            {
                Lhs = { Pattern <Word, ShapeNode> .New("1").Annotation(any).OneOrMore.Value },
                Rhs = { new CopyFromInput("1"), new InsertSegments(Table3, "+ɯd") }
            });
            Morphophonemic.MorphologicalRules.Add(edSuffix);

            var morpher = new Morpher(TraceManager, Language);

            var analysis = new WordAnalysis(new IMorpheme[] { siPrefix, Entries["33"], edSuffix }, 1, "V");

            string[] words = morpher.GenerateWords(analysis).ToArray();
            Assert.That(words, Is.EquivalentTo(new[] { "sisasɯd" }));
        }
Esempio n. 9
0
        public int ExpressiveWords(string s, string[] words)
        {
            var baseWord = new WordAnalysis(s);
            int ans      = 0;

            foreach (var word in words)
            {
                if (s == word)
                {
                    ans++;
                    continue;
                }

                var wordAnalysis = new WordAnalysis(word);
                if (baseWord.Key != wordAnalysis.Key)
                {
                    continue;
                }

                bool check = true;
                for (int i = 0; i < baseWord.Counter.Count; i++)
                {
                    if (baseWord.Counter[i] == wordAnalysis.Counter[i])
                    {
                        continue;
                    }

                    if (baseWord.Counter[i] >= 3 && baseWord.Counter[i] >= wordAnalysis.Counter[i])
                    {
                        continue;
                    }

                    check = false;
                    break;
                }

                ans += check ? 1 : 0;
            }

            return(ans);
        }
Esempio n. 10
0
        public void RemoveWordAnalysis(WordAnalysis lexemes)
        {
            using (m_activationContext.Activate())
            {
                IWfiWordform wordform;
                if (!TryGetWordform(lexemes.Word, out wordform))
                {
                    return;
                }

                foreach (IWfiAnalysis analysis in wordform.AnalysesOC.Where(a => a.MorphBundlesOS.Count > 0 && a.ApprovalStatusIcon == (int)Opinions.approves))
                {
                    bool match = true;
                    int  i     = 0;
                    foreach (Lexeme lexeme in lexemes)
                    {
                        if (i == analysis.MorphBundlesOS.Count || analysis.MorphBundlesOS[i].MorphRA == null)
                        {
                            match = false;
                            break;
                        }

                        var entry = analysis.MorphBundlesOS[i].MorphRA.OwnerOfClass <ILexEntry>();
                        if (!GetEntryLexeme(entry).Equals(lexeme))
                        {
                            match = false;
                            break;
                        }
                        i++;
                    }
                    if (match && !analysis.OccurrencesInTexts.Any())
                    {
                        NonUndoableUnitOfWorkHelper.Do(m_cache.ActionHandlerAccessor, () => wordform.AnalysesOC.Remove(analysis));
                        break;
                    }
                }
            }
        }
Esempio n. 11
0
        private bool GetWordAnalysis(IWfiAnalysis analysis, out WordAnalysis lexemes)
        {
            var lexemeArray = new Lexeme[analysis.MorphBundlesOS.Count];

            for (int i = 0; i < analysis.MorphBundlesOS.Count; i++)
            {
                IWfiMorphBundle mb = analysis.MorphBundlesOS[i];
                if (mb.MorphRA == null)
                {
                    lexemes = null;
                    return(false);
                }
                var entry = mb.MorphRA.OwnerOfClass <ILexEntry>();
                if (entry.LexemeFormOA != mb.MorphRA)
                {
                    lexemes = null;
                    return(false);
                }
                lexemeArray[i] = GetEntryLexeme(entry);
            }

            lexemes = new FdoWordAnalysis(analysis.Wordform.Form.get_String(DefaultVernWs).Text.Normalize(), lexemeArray);
            return(true);
        }
 public static void AddGeneratedWords(this IMorphologicalGenerator targetGenerator, WordAnalysis analysis,
                                      params string[] words)
 {
     targetGenerator.GenerateWords(Arg.Is(analysis)).Returns(words);
 }
        public IList <FeatureValue> Aggregate(
            IList <EncounterValues> values, RawRecord record,
            FeatureGroup featureGroup, FeatureSelectionContext context)
        {
            var wordAnalyses        = new WordAnalysis[values.Count];
            var hasNullWordAnalysis = false;

            for (var i = 0; i < values.Count; i++)
            {
                if (values[i].Encounter == RawWordEncounter.EmptyWordEncounter)
                {
                    return(FeatureValue.NewArray(0, values[i].Values.Count));
                }

                wordAnalyses[i] = context.Project.DataAnalysis.GetByName(values[i].Encounter.Word);

                if (wordAnalyses[i] == null)
                {
                    hasNullWordAnalysis = true;
                }
            }

            var weights = new double[wordAnalyses.Length];

            if (hasNullWordAnalysis)
            {
                var weight = 1d / wordAnalyses.Length;

                for (var i = 0; i < weights.Length; i++)
                {
                    weights[i] = weight;
                }
            }
            else
            {
                var trainEncounters    = new double[wordAnalyses.Length];
                var trainEncountersSum = 0;

                for (var i = 0; i < weights.Length; i++)
                {
                    var currentTrainingEncounters = wordAnalyses[i].TrainEncounters.Values
                                                    .Sum(x => x.Encounters);

                    trainEncounters[i]  = currentTrainingEncounters;
                    trainEncountersSum += currentTrainingEncounters;
                }

                if (trainEncountersSum == 0)
                {
                    return(FeatureValue.NewArray(0, values[0].Values.Count));
                }

                for (var i = 0; i < weights.Length; i++)
                {
                    weights[i] = trainEncounters[i] / trainEncountersSum;
                }
            }

            var result = new float[values[0].Values.Count];

            for (var vectorIndex = 0; vectorIndex < result.Length; vectorIndex++)
            {
                for (var encounterIndex = 0; encounterIndex < values.Count; encounterIndex++)
                {
                    result[vectorIndex] += (float)(weights[encounterIndex] *
                                                   values[encounterIndex].Values[vectorIndex].NumericValue);
                }
            }

            return(FeatureValue.NewArray(result));
        }