public bool CreateOrUpdate(long sentenceId, long wordId, string originalText, int orderInSentence, GrammarWordType grammarWordType) { var parsedGrammarWordType = (int)grammarWordType; bool result = false; Adapter.ActionByContext(context => { SentenceWord sentenceWord = context.SentenceWord.FirstOrDefault( e => e.SentenceId == sentenceId && e.OrderInSentence == orderInSentence); if (sentenceWord == null) { sentenceWord = new SentenceWord { SentenceId = sentenceId, OrderInSentence = orderInSentence }; context.SentenceWord.Add(sentenceWord); } sentenceWord.WordId = wordId; sentenceWord.OriginalText = originalText; sentenceWord.GrammarType = parsedGrammarWordType; context.SaveChanges(); result = IdValidator.IsValid(sentenceWord.Id); }); return(result); }
public void SentenceWordCannotSpecifyPossibleFollowingWithSpaceInIt() { var word = new SentenceWord(DefaultWord); var exception = Assert.Throws <ArgumentOutOfRangeException>(() => word.AddPossibleFollowingWord(" ")); Assert.AreEqual("possibleNextWord", exception.ParamName); }
private static bool EndSentence(SentenceWord currentWord) { var random = new Random(DateTime.Now.Millisecond); var canEndSentence = currentWord.CanEndSentence; canEndSentence = canEndSentence && random.Next(0, currentWord.GetPossibleFollowingWords().Count() + 1) == 0; return(canEndSentence); }
private static void AddSentenceWordByText(Dictionary <string, List <SentenceWord> > sentenceWords, string text, SentenceWord sw) { if (!sentenceWords.ContainsKey(text)) { sentenceWords.Add(text, new List <SentenceWord>()); } sentenceWords[text].Add(sw); }
public void WithSentenceWordWithNextFollowingWord_WhenAddingSameFollowingWord_OnlyOneCopyOfWordIsContained() { // ARRANGE var word = new SentenceWord(DefaultWord); // ACT word.AddPossibleFollowingWord(PossibleNextWordA); word.AddPossibleFollowingWord(PossibleNextWordA); // ASSERT Assert.AreEqual(1, word.GetPossibleFollowingWords().Count(w => w == PossibleNextWordA)); }
public static Suffix FindSuffixOR(SentenceWord sw, params Semantics[] s) { return(Array.Find(sw.Suffixes, sem => { for (int i = 0; i < s.Length; i++) { if (sem.Semantics == s[i]) { return true; } } return false; })); }
public void SentenceWordCanSpecifyPossibleFollowingWords() { // ARRANGE var word = new SentenceWord(DefaultWord); // ACT word.AddPossibleFollowingWord(PossibleNextWordA); word.AddPossibleFollowingWord(PossibleNextWordB); // ASSERT Assert.IsTrue(word.GetPossibleFollowingWords().Contains(PossibleNextWordA)); Assert.IsTrue(word.GetPossibleFollowingWords().Contains(PossibleNextWordB)); }
public void WhenWordExistsInLexicon_AddedWordIsSameAsOldVersion() { // ARRANGE var lexicon = new Lexicon(); var sentenceWord = new SentenceWord(DefaultWord); // ACT lexicon.ManageWord(sentenceWord); lexicon.ManageWord(sentenceWord); // ASSERT Assert.IsTrue(lexicon.Words().Count(w => w.Word == DefaultWord) == 1); Assert.IsTrue(lexicon.Words().Count(w => w.CanStartSentence == false) == 1); Assert.IsTrue(lexicon.Words().Count(w => w.CanEndSentence == false) == 1); }
public void LexiconCanContainMultipleWords() { // ARRANGE var lexicon = new Lexicon(); var sentenceWord = new SentenceWord(DefaultWord); var updatedSentenceWord = new SentenceWord(PossibleFollowingWordOne); // ACT lexicon.ManageWord(sentenceWord); lexicon.ManageWord(updatedSentenceWord); var lexiconWords = lexicon.Words().ToList(); // ASSERT Assert.AreEqual(DefaultWord, lexiconWords.ElementAt(0).Word); Assert.AreEqual(PossibleFollowingWordOne, lexiconWords.ElementAt(1).Word); }
public void WhenWordExistsInLexicon_AddedWordWithPossibleEndSentencePropertyOverridesAncientProperty() { // ARRANGE var lexicon = new Lexicon(); var sentenceWord = new SentenceWord(DefaultWord); var updatedSentenceWord = new SentenceWord(DefaultWord, canEndSentence: true); // ACT lexicon.ManageWord(sentenceWord); lexicon.ManageWord(updatedSentenceWord); var lexiconWord = lexicon.Words().First(); // ASSERT Assert.AreEqual(DefaultWord, lexiconWord.Word); Assert.AreEqual(updatedSentenceWord.CanEndSentence, lexiconWord.CanEndSentence); }
public void WhenWordExistsInLexiconCanStartSentence_AddedWordDoesNotOverridesAncientProperty() { // ARRANGE var lexicon = new Lexicon(); var sentenceWord = new SentenceWord(DefaultWord, true); var updatedSentenceWord = new SentenceWord(DefaultWord); // ACT lexicon.ManageWord(sentenceWord); lexicon.ManageWord(updatedSentenceWord); var lexiconWord = lexicon.Words().First(); // ASSERT Assert.AreEqual(DefaultWord, lexiconWord.Word); Assert.AreEqual(true, lexiconWord.CanStartSentence); }
public static Suffix FindSuffixAND(SentenceWord sw, params Semantics[] s) { int count = 0; return(Array.Find(sw.Suffixes, sem => { for (int i = 0; i < s.Length; i++) { if (sem.Semantics == s[i]) { count++; } } if (count == s.Length) { return true; } else { return false; } })); }
public void WhenExistingWordInLexiconHasPossibleFollowingWord_SameWordWithPossibleFollowingWordIsAdded() { // ARRANGE var lexicon = new Lexicon(); var sentenceWord = new SentenceWord(DefaultWord); sentenceWord.AddPossibleFollowingWord(PossibleFollowingWordOne); var updatedSentenceWord = new SentenceWord(DefaultWord); sentenceWord.AddPossibleFollowingWord(PossibleFollowingWordTwo); // ACT lexicon.ManageWord(sentenceWord); lexicon.ManageWord(updatedSentenceWord); var lexiconWord = lexicon.Words().First(); // ASSERT Assert.AreEqual(PossibleFollowingWordOne, lexiconWord.GetPossibleFollowingWords().ElementAt(0)); Assert.AreEqual(PossibleFollowingWordTwo, lexiconWord.GetPossibleFollowingWords().ElementAt(1)); }
public void CanSpecifyWhenWordCouldEndSentence() { var word = new SentenceWord(DefaultWord, canEndSentence: true); Assert.AreEqual(true, word.CanEndSentence); }
public static Sentence ParseIntoSentence(string text, int no, bool report) { Sentence sentence = new Sentence(); List <string> words = text.Split(new char[] { ',', ' ', '.', '?', ':', '!' }).ToList(); words.RemoveAll(e => e.Equals(" ")); words.RemoveAll(e => e.Equals("")); for (int i = 0; i < words.Count; i++) { words[i] = words[i].ToLower(); } // Detect three-word compounds. for (int i = 0; i < words.Count - 2; i++) { if (Corpus.CorpusList.Find(w => w.Word.Contains(words[i] + " " + words[i + 1] + " " + words[i + 2])) != null) { words[i] = words[i] + " " + words[i + 1] + " " + words[i + 2]; for (int j = i + 3; j < words.Count; j++) { words[j - 2] = words[j]; } words.RemoveAt(words.Count - 1); words.RemoveAt(words.Count - 1); } } // Detect two-word compounds. for (int i = 0; i < words.Count - 1; i++) { if (Corpus.CorpusList.Find(w => w.Word.Contains(words[i] + " " + words[i + 1])) != null) { words[i] = words[i] + " " + words[i + 1]; for (int j = i + 2; j < words.Count; j++) { words[j - 1] = words[j]; } words.RemoveAt(words.Count - 1); } } for (int i = 0; i < words.Count; i++) { if (report) { Console.WriteLine(words[i]); } SentenceWord sw; sw = SentenceWord.MorphologicalParsing(words[i], words[i], report); sentence.sw.Add(sw); } if (report) { Console.Write("Sentence " + no + " stems: "); } int[] resultEachWord = new int[sentence.sw.Count]; try { for (int i = 0; i < sentence.sw.Count; i++) { resultEachWord[i] = Suffix.PolaritySummation(sentence.sw[i].Suffixes); if (sentence.sw[i].Stem.Attribute == Attribute.IVBN || sentence.sw[i].Stem.Attribute == Attribute.VBN || sentence.sw[i].Stem.Attribute == Attribute.JJN) { resultEachWord[i] = Suffix.PolaritySummation(resultEachWord[i], -1); } if (sentence.sw[i].Stem.Attribute == Attribute.IVBP || sentence.sw[i].Stem.Attribute == Attribute.VBP || sentence.sw[i].Stem.Attribute == Attribute.JJP) { resultEachWord[i] = Suffix.PolaritySummation(resultEachWord[i], +1); } } for (int i = 0; i < sentence.sw.Count; i++) { if (report) { Console.Write(sentence.sw[i].Stem.Word + "(" + sentence.sw[i].Stem.Attribute + ") "); } } if (report) { Console.WriteLine(); } int c = 0; // VE while (c < sentence.sw.Count && !sentence.sw[c].Stem.Word.Equals("ve")) { c++; } if (c < sentence.sw.Count) { List <int> changer = resultEachWord.ToList(); changer.Remove(changer[c - 1]); changer.Remove(changer[c]); changer[c - 1] = Suffix.PolarityAND(resultEachWord[c - 1], resultEachWord[c + 1]); resultEachWord = changer.ToArray(); } c = 0; // VEYA while (c < sentence.sw.Count && !sentence.sw[c].Stem.Word.Equals("veya")) { c++; } if (c < sentence.sw.Count) { List <int> changer = resultEachWord.ToList(); changer.Remove(changer[c - 1]); changer.Remove(changer[c]); changer[c - 1] = Suffix.PolarityOR(resultEachWord[c - 1], resultEachWord[c + 1]); resultEachWord = changer.ToArray(); } c = 0; // YA DA while (c < sentence.sw.Count && !sentence.sw[c].Stem.Word.Equals("ya da")) { c++; } if (c < sentence.sw.Count) { List <int> changer = resultEachWord.ToList(); changer.Remove(changer[c - 1]); changer.Remove(changer[c]); changer[c - 1] = Suffix.PolarityXOR(resultEachWord[c - 1], resultEachWord[c + 1]); resultEachWord = changer.ToArray(); } // VAR if (sentence.sw.IndexOf(sentence.sw.Find(s => s.Stem.Word.Equals("var"))) > 0) { resultEachWord[sentence.sw.IndexOf(sentence.sw.Find(s => s.Stem.Word.Equals("var")))] = 1; } // DEĞİL if (sentence.sw.IndexOf(sentence.sw.Find(s => s.Stem.Word.Equals("değil"))) > 0) { resultEachWord[sentence.sw.IndexOf(sentence.sw.Find(s => s.Stem.Word.Equals("değil")))] = -1; } // YOK if (sentence.sw.IndexOf(sentence.sw.Find(s => s.Stem.Word.Equals("yok"))) > 0) { resultEachWord[sentence.sw.IndexOf(sentence.sw.Find(s => s.Stem.Word.Equals("yok")))] = -1; } } catch (NullReferenceException) { MessageBox.Show("Error parsing the sentence " + no + "."); return(null); } catch (ArgumentOutOfRangeException) { MessageBox.Show("Error parsing the sentence " + no + "."); return(null); } sentence.polarity = (short)Suffix.PolaritySummation(resultEachWord); if (report) { Console.WriteLine("Sentence " + no + " polarity is " + sentence.polarity + "."); } return(sentence); }
public void CanSpecifyWhenWordCouldStartsSentence() { var word = new SentenceWord(DefaultWord, true); Assert.AreEqual(true, word.CanStartSentence); }
private void findAdditionalPredicates() { bool allDone = false; while (!allDone) { allDone = true; foreach (var predicateItem in Result.Items.FindAll(x => x.ObjectType == ObjectType.Predicate)) { List<int> idsToSearchFrom = new List<int>(); idsToSearchFrom.Add(predicateItem.Id); idsToSearchFrom.AddRange(predicateItem.AddedWordsCase1.Concat(predicateItem.AddedWordsCase2).Select(x => x.Id).ToList()); foreach (var addWord in _sentence.WordList.FindAll(x => idsToSearchFrom.Contains(x.DOM) && x.Link.Value == LinkType.Prisvyaz.Value)) { SentenceWord addItem = new SentenceWord(); addItem.CopyFromSourceWord(addWord); if (!predicateItem.AddedWordsCase1.Exists(x => x.Id == addItem.Id)) { predicateItem.AddedWordsCase1.Add(addItem); allDone = false; } } foreach (var addWord2 in _sentence.WordList.FindAll(x => idsToSearchFrom.Contains(x.DOM) && x.Link.Value == LinkType.Kompl1.Value && x.Representation.Value == Representation.Infinitive.Value)) { SentenceWord addItem2 = new SentenceWord(); addItem2.CopyFromSourceWord(addWord2); if (!predicateItem.AddedWordsCase2.Exists(x => x.Id == addItem2.Id)) { predicateItem.AddedWordsCase2.Add(addItem2); allDone = false; } } } } }
private void findAdditionalSubjects() { foreach (var item in Result.Items.FindAll(x => x.ObjectType == ObjectType.Subject)) { foreach (var addWord in _sentence.WordList.FindAll(x => x.DOM == item.Id && x.Link.Value == LinkType.Kolichest.Value)) { SentenceWord addItem = new SentenceWord(); addItem.CopyFromSourceWord(addWord); if (item.AddedWordsCase1.Find(x => x.Id == addItem.Id) == null) item.AddedWordsCase1.Add(addItem); } foreach (var addWord in _sentence.WordList.FindAll(x => x.DOM == item.Id && x.Link.Value == LinkType.Elekt.Value)) { SentenceWord addItem = new SentenceWord(); addItem.CopyFromSourceWord(addWord); if (item.AddedWordsCase2.Find(x => x.Id == addItem.Id) == null) item.AddedWordsCase2.Add(addItem); foreach (var addWord2 in _sentence.WordList.FindAll(x => x.DOM == addItem.Id && x.Link.Value == LinkType.Predl.Value)) { SentenceWord addItem2 = new SentenceWord(); addItem2.CopyFromSourceWord(addWord2); if (item.AddedWordsCase2.Find(x => x.Id == addItem2.Id) == null) item.AddedWordsCase2.Add(addItem2); } } } }
public override void ProcessSentence(Sentence sentence) { _sentence = sentence; Debug.Assert(_stage3Result != null); // ищем частицы и вспомогательные глаголы List<SentenceWord> particles = sentence.WordList.Where(x => x.PartOfSpeech.Value == PartOfSpeech.Particle.Value).ToList(); List<SentenceWord> auxVerbs = sentence.WordList.Where(x => x.IsAuxVerb).ToList(); foreach (var itemStage3 in _stage3Result.Items) { Stage4ResultElement item = new Stage4ResultElement(); item.CopyFromSourceWord(itemStage3); List<int> itemStag3Ids = itemStage3.AddedWordsCase1.Concat(itemStage3.AddedWordsCase2).Concat(itemStage3.ServiceParts).Select(x => x.Id).ToList(); SentenceWord particle; SentenceWord auxVerb; particle = particles.Find(x => x.DOM == item.Id); if (particle != null) { SentenceWord word = new SentenceWord(); word.CopyFromSourceWord(particle); if (!itemStag3Ids.Contains(word.Id)) item.ServiceParts.Add(word); } particle = particles.Find(x => item.ServiceParts.Concat(item.AddedWordsCase1).Concat(item.AddedWordsCase2).Select(y => y.Id).ToList().Contains(x.DOM)); if (particle != null) { SentenceWord word = new SentenceWord(); word.CopyFromSourceWord(particle); if (!itemStag3Ids.Contains(word.Id)) item.ServiceParts.Add(word); } auxVerb = auxVerbs.Find(x => x.DOM == item.Id); if (auxVerb != null) { SentenceWord word = new SentenceWord(); word.CopyFromSourceWord(auxVerb); if (!itemStag3Ids.Contains(word.Id)) item.ServiceParts.Add(word); } auxVerb = auxVerbs.Find(x => item.AddedWordsCase1.Concat(item.AddedWordsCase2).Select(y => y.Id).ToList().Contains(x.DOM)); if (auxVerb != null) { SentenceWord word = new SentenceWord(); word.CopyFromSourceWord(auxVerb); if (!itemStag3Ids.Contains(word.Id)) item.ServiceParts.Add(word); } Result.Items.Add(item); } Result.Items.RemoveAll(x => x.IsConjuction); foreach (var item in Result.Items) { item.ServiceParts.RemoveAll(x => x.IsConjuction); } // определяем принадлежность к ПП foreach (var item in Result.Items) { foreach (var ssItem in _simpleSentenceStage3Result.Items) { var ssWordIds = ssItem.Id.Concat(ssItem.ChainWordsIds.All.Values); if (ssWordIds.Contains(item.Id)) { item.SimpleSentenceNr = ssItem.SimpleSentenceNr; break; } } } }
public void SentenceWordHasWord() { var word = new SentenceWord(DefaultWord); Assert.AreEqual(DefaultWord, word.Word); }