Ejemplo n.º 1
0
        public AffixRules Parse(string file, Encoding encoding = null)
        {
            AffixRules rules = new AffixRules();

            using (StreamReader reader = new StreamReader(file, encoding))
            {
                string line;

                while (null != (line = reader.ReadLine()))
                {
                    string[] parts = this.ParseLine(line);
                    if (null != parts)
                    {
                        if (parts.Length == 4)
                        {
                            Combinable comb = parts[2] == "Y" ? Combinable.Y : Combinable.N;
                            RuleType rType;
                            switch (parts[0])
                            {
                                case "SFX": rType = RuleType.SFX; break;
                                case "PFX": rType = RuleType.PFX; break;
                                default: rType = RuleType.Unknown; break;
                            }

                            rules.Add(new Rule(parts[1], comb, rType));
                        }
                        else if (parts.Length == 5)
                        {
                            rules.AddItem(parts[1], new RuleItem(parts[2], parts[3], parts[4]));
                        }
                    }
                }
            }

            return rules;
        }
Ejemplo n.º 2
0
        public void WordGeneratorTest()
        {
            Rule ruleD = new Rule("D", Combinable.Y, RuleType.SFX);
            ruleD.Add(new RuleItem("0","d","e"));
            ruleD.Add(new RuleItem("y", "ied", "[^aeiou]y"));
            ruleD.Add(new RuleItem("0", "ed", "[^ey]"));
            ruleD.Add(new RuleItem("0", "ed", "[aeiou]y"));

            Rule ruleG = new Rule("G", Combinable.Y, RuleType.SFX);
            ruleG.Add(new RuleItem("e", "ing", "e"));
            ruleG.Add(new RuleItem("0", "ing", "[^e]"));

            AffixRules rules = new AffixRules();
            rules.Add(ruleD);
            rules.Add(ruleG);

            DictionaryItemWithFlags item = new DictionaryItemWithFlags("create", "DG");

            List<string> words = rules.GetPossibleWords(item);

            List<string> correctWords = new List<string>()
            {
                "create", "created", "creating"
            };

            CollectionAssert.AreEqual(correctWords, words);

            Rule ruleH = new Rule("H", Combinable.Y, RuleType.SFX);
            ruleH.Add(new RuleItem("0", "u", "[^ey]"));
            ruleH.Add(new RuleItem("0", "e", "[^eyghkc]"));
            ruleH.Add(new RuleItem("0", "em", "[^ey]"));
            ruleH.Add(new RuleItem("0", "y", "[^ey]"));
            ruleH.Add(new RuleItem("0", "ů", "[^ey]"));
            ruleH.Add(new RuleItem("0", "ům", "[^ey]"));
            ruleH.Add(new RuleItem("0", "ech", "[^eyghk]"));
            ruleH.Add(new RuleItem("g", "zích", "g"));
            ruleH.Add(new RuleItem("h", "zích", "[^c]h"));
            ruleH.Add(new RuleItem("ch", "ších", "ch"));
            ruleH.Add(new RuleItem("k", "cích", "k"));
            ruleH.Add(new RuleItem("e", "u", "e"));
            ruleH.Add(new RuleItem("0", "m", "e"));
            ruleH.Add(new RuleItem("e", "y", "e"));
            ruleH.Add(new RuleItem("e", "ů", "e"));
            ruleH.Add(new RuleItem("e", "ům", "e"));
            ruleH.Add(new RuleItem("e", "ech", "e"));
            ruleH.Add(new RuleItem("y", "ů", "y"));
            ruleH.Add(new RuleItem("y", "ům", "y"));
            ruleH.Add(new RuleItem("y", "ech", "[^ghk]y"));
            ruleH.Add(new RuleItem("gy", "zích", "gy"));
            ruleH.Add(new RuleItem("hy", "zích", "[^c]hy"));
            ruleH.Add(new RuleItem("chy", "ších", "chy"));
            ruleH.Add(new RuleItem("ky", "cích", "ky"));

            rules = new AffixRules();
            rules.Add(ruleH);

            words = rules.GetPossibleWords(new DictionaryItemWithFlags("abakus", "H"));

            correctWords = new List<string>()
            {
                "abakus",
                "abakusu",
                "abakuse",
                "abakusem",
                "abakusy",
                "abakusů",
                "abakusům",
                "abakusech"
            };

            CollectionAssert.AreEqual(correctWords, words);

            Rule ruleN = new Rule("N", Combinable.Y, RuleType.PFX);
            ruleN.Add(new RuleItem("0", "ne", "."));

            Rule ruleE = new Rule("E", Combinable.Y, RuleType.PFX);
            ruleE.Add(new RuleItem("0", "nej", "."));

            rules = new AffixRules();
            rules.Add(ruleN);
            rules.Add(ruleE);

            words = rules.GetPossibleWords(new DictionaryItemWithFlags("méně", "NE"));

            correctWords = new List<string>()
            {
                "méně",
                "neméně",
                "nejméně"
            };

            CollectionAssert.AreEqual(correctWords, words);

            Rule ruleX = new Rule("X", Combinable.Y, RuleType.SFX);
            ruleX.Add(new RuleItem("a", "ouc", "a"));
            ruleX.Add(new RuleItem("a", "ouce", "a"));

            rules = new AffixRules();
            rules.Add(ruleN);
            rules.Add(ruleX);

            words = rules.GetPossibleWords(new DictionaryItemWithFlags("pomra", "XN"));
            correctWords = new List<string>()
            {
                "pomra",
                "pomrouc",
                "pomrouce",
                "nepomra",
                "nepomrouc",
                "nepomrouce",
            };
        }