/** * <summary>The loadFromText method takes a String filename as an input. It reads given file line by line and splits according to space * and assigns each word to the String array. Then, adds these word with their flags to the words {@link java.util.ArrayList}. * At the end it sorts the words {@link java.util.ArrayList}.</summary> * * <param name="stream">File stream input.</param> */ private void LoadFromText(Stream stream) { var streamReader = new StreamReader(stream); var line = streamReader.ReadLine(); while (line != null) { var list = line.Split(" "); if (list.Length > 0) { var currentWord = new TxtWord(list[0]); int i; for (i = 1; i < list.Length; i++) { currentWord.AddFlag(list[i]); } words.Add(currentWord); } line = streamReader.ReadLine(); } words.Sort(comparator); }
/** * <summary>The addWithFlag method takes a String name and a flag as inputs. First it creates a {@link TxtWord} word, then if * given name is not in words {@link java.util.ArrayList} it creates new {@link TxtWord} with given name and assigns it to * the word and adds given flag to the word, it also add newly created word to the words {@link java.util.ArrayList}'s index * found by performing a binary search and return true at the end. If given name is in words {@link java.util.ArrayList}, * it adds it the given flag to the word.</summary> * * <param name="name">String input.</param> * <param name="flag">String flag.</param> * <returns>true if given name is in words {@link java.util.ArrayList}, false otherwise.</returns> */ public bool AddWithFlag(string name, string flag) { TxtWord word; if (GetWord(name.ToLower(new CultureInfo("tr"))) == null) { word = new TxtWord(name.ToLower(new CultureInfo("tr"))); word.AddFlag(flag); var insertIndex = words.BinarySearch(word, comparator); if (insertIndex >= 0) { words.Insert(insertIndex, word); } return(true); } word = (TxtWord)GetWord(name.ToLower(new CultureInfo("tr"))); if (!word.ContainsFlag(flag)) { word.AddFlag(flag); } return(false); }
/** * <summary>The samePos method takes {@link TxtWord} as input and returns true if; * flags {@link ArrayList} contains CL_ISIM * CL_ISIM: The bare-form of the word is a noun. e.g. Abla * flags {@link ArrayList} contains CL_FIIL * CL_FIIL: The bare-form of the word is a verb. e.g. Affet * flags {@link ArrayList} contains IS_ADJ * IS_ADJ: The bare-form of the word is a adjective. e.g. Acayip * flags {@link ArrayList} contains IS_ZM * IS_ZM: The bare-form of the word is a pronoun. e.g. Başkası * flags {@link ArrayList} contains IS_ADVERB * IS_ADVERB: The bare-form of the word is a adverb. e.g. Tekrar, açıktan, adeta</summary> * * <param name="word">{@link TxtWord} type input.</param> * <returns>true if given word is nominal, verb, adjective, pronoun or adverb, false otherwise.</returns> */ public bool SamePos(TxtWord word) { if (IsNominal() && word.IsNominal()) { return(true); } if (IsVerb() && word.IsVerb()) { return(true); } if (IsAdjective() && word.IsAdjective()) { return(true); } if (IsPronoun() && word.IsPronoun()) { return(true); } if (IsAdverb() && word.IsAdverb()) { return(true); } return(false); }
/** * <summary>The clone method creates {@link TxtWord} type copy with name and add items of flags {@link ArrayList} to the copy.</summary> * * <returns>TxtWord type copy.</returns> */ public new TxtWord Clone() { var copy = new TxtWord(name); foreach (var t in flags) { copy.AddFlag(t); } return(copy); }
/** * <summary>The addWordWhenRootSoften is used to add word to Trie whose last consonant will be soften. * For instance, in the case of Dative Case Suffix, the word is 'müzik' when '-e' is added to the word, the last * char is drooped and root became 'müzi' and by changing 'k' into 'ğ' the word transformed into 'müziğe' as in the * example of 'Herkes müziğe doğru geldi'. * In the case of accusative, possessive of third person and a derivative suffix, the word is 'kanat' when '-i' is * added to word, last char is dropped, root became 'kana' then 't' transformed into 'd' and added to Trie. The word is * changed into 'kanadı' as in the case of 'Kuşun kırık kanadı'.</summary> * * <param name="trie">the name of the Trie to add the word.</param> * <param name="last">the last char of the word to be soften.</param> * <param name="root">the substring of the word whose last one or two chars are omitted from the word to bo softed.</param> * <param name="word">the original word.</param> */ private void AddWordWhenRootSoften(Trie.Trie trie, char last, string root, TxtWord word) { switch (last) { case 'p': trie.AddWord(root + 'b', word); break; case '\u00e7': //ç trie.AddWord(root + 'c', word); break; case 't': trie.AddWord(root + 'd', word); break; case 'k': case 'g': trie.AddWord(root + '\u011f', word); //ğ break; } }
/** * <summary>The prepareTrie method is used to create a Trie with the given dictionary. First, it gets the word from dictionary, * then checks some exceptions like 'ben' which does not fit in the consonant softening rule and transforms into 'bana', * and later on it generates a root by removing the last char from the word however if the length of the word is greater * than 1, it also generates the root by removing the last two chars from the word. * Then, it gets the last char of the root and adds root and word to the result Trie. There are also special cases such as; * lastIdropsDuringSuffixation condition, if it is true then addWordWhenRootSoften method will be used rather than addWord. * Ex : metin + i = metni * isPortmanteauEndingWithSI condition, if it is true then addWord method with rootWithoutLastTwo will be used. * Ex : ademelması + lar = ademelmaları * isPortmanteau condition, if it is true then addWord method with rootWithoutLast will be used. * Ex : mısıryağı + lar = mısıryağları * vowelEChangesToIDuringYSuffixation condition, if it is then addWord method with rootWithoutLast will be used * depending on the last char whether it is 'e' or 'a'. * Ex : ye + iniz - yiyiniz * endingKChangesIntoG condition, if it is true then addWord method with rootWithoutLast will be used with added 'g'. * Ex : ahenk + i = ahengi</summary> * * <returns>the resulting Trie.</returns> */ public Trie.Trie PrepareTrie() { Trie.Trie result = new Trie.Trie(); String root, rootWithoutLast, rootWithoutLastTwo; char last, lastBefore = ' '; int length; for (int i = 0; i < Size(); i++) { TxtWord word = (TxtWord)GetWord(i); root = word.GetName(); length = root.Length; if (root == "ben") { result.AddWord("bana", word); } if (root == "sen") { result.AddWord("sana", word); } rootWithoutLast = root.Substring(0, length - 1); if (length > 1) { rootWithoutLastTwo = root.Substring(0, length - 2); } else { rootWithoutLastTwo = ""; } if (length > 1) { lastBefore = root[length - 2]; } last = root[length - 1]; result.AddWord(root, word); if (word.LastIdropsDuringSuffixation() || word.LastIdropsDuringPassiveSuffixation()) { if (word.RootSoftenDuringSuffixation()) { AddWordWhenRootSoften(result, last, rootWithoutLastTwo, word); } else { result.AddWord(rootWithoutLastTwo + last, word); } } // NominalRootNoPossessive if (word.IsPortmanteauEndingWithSI()) { result.AddWord(rootWithoutLastTwo, word); } if (word.RootSoftenDuringSuffixation()) { AddWordWhenRootSoften(result, last, rootWithoutLast, word); } if (word.IsPortmanteau()) { if (word.IsPortmanteauFacedVowelEllipsis()) { result.AddWord(rootWithoutLastTwo + last + lastBefore, word); } else { if (word.IsPortmanteauFacedSoftening()) { switch (lastBefore) { case 'b': result.AddWord(rootWithoutLastTwo + 'p', word); break; case 'c': result.AddWord(rootWithoutLastTwo + 'ç', word); break; case 'd': result.AddWord(rootWithoutLastTwo + 't', word); break; case 'ğ': result.AddWord(rootWithoutLastTwo + 'k', word); break; } } else { result.AddWord(rootWithoutLast, word); } } } if (word.VowelEChangesToIDuringYSuffixation() || word.VowelAChangesToIDuringYSuffixation()) { switch (last) { case 'e': result.AddWord(rootWithoutLast, word); break; case 'a': result.AddWord(rootWithoutLast, word); break; } } if (word.EndingKChangesIntoG()) { result.AddWord(rootWithoutLast + 'g', word); } } return(result); }