Beispiel #1
0
 public void SetUp()
 {
     _tree = new WordTree();
     _tree.StoreWord("Apple");
     _tree.StoreWord("Apples");
     _tree.StoreWord("application");
 }
        public void TestIsWordReturnsFalse()
        {
            var tree = new WordTree();

            Assert.False(tree.IsWord("cat"));
            Assert.False(tree.IsWord("dog"));
        }
Beispiel #3
0
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(WebApiConfig.Register);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);

            //WordTree for bad word detection --------------------------------------------------------------------
            string badWordFilename = System.Configuration.ConfigurationManager.AppSettings["BadWordList"];
            string charsetFilename = System.Configuration.ConfigurationManager.AppSettings["Charset"];

            badWordFilename = System.Web.HttpContext.Current.Server.MapPath("~/App_Data/" + badWordFilename);
            charsetFilename = System.Web.HttpContext.Current.Server.MapPath("~/App_Data/" + charsetFilename);

            //read charset data
            string[] charsetInfo  = System.IO.File.ReadAllLines(charsetFilename);
            string   allowedChars = charsetInfo[0];
            string   separators   = charsetInfo[1];
            string   wildcards    = charsetInfo[2];

            //create tree
            WordTree tree = new WordTree(allowedChars, separators, wildcards, new string[] { }, System.IO.File.ReadAllLines(badWordFilename));

            //add maps
            Dictionary <char, string> fuzzyMap = new Dictionary <char, string>();

            for (int i = 3; i < charsetInfo.Length; i++)
            {
                string[] keyValueMap = charsetInfo[i].Split(new char[] { '\t' });
                fuzzyMap.Add(keyValueMap[0][0], keyValueMap[1]);
            }
            Charset.ExtendFuzzyMap(fuzzyMap);

            Application["WordTree"] = tree;
            //-----------------------------------------------------------------------------------------------------
        }
        public void BasicTest()
        {
            WordTree wordTree = new WordTree();

            wordTree.Add("London");
            wordTree.Add("Londontowne");

            IEnumerable <string> words = wordTree.AutoComplete("Lond");

            CollectionAssert.AreEquivalent(new string[] { "London", "Londontowne" }, words.ToList());
            words = wordTree.AutoComplete("London");
            CollectionAssert.AreEquivalent(new string[] { "London", "Londontowne" }, words.ToList());
            words = wordTree.AutoComplete("Londont");
            CollectionAssert.AreEquivalent(new string[] { "Londontowne" }, words.ToList());
            words = wordTree.AutoComplete("Londontowne");
            CollectionAssert.AreEquivalent(new string[] { "Londontowne" }, words.ToList());
            words = wordTree.AutoComplete("Londontownes");
            CollectionAssert.AreEquivalent(new string[] { }, words.ToList());
            words = wordTree.AutoComplete("Test");
            CollectionAssert.AreEquivalent(new string[] { }, words.ToList());

            wordTree.Clear();

            words = wordTree.AutoComplete("Lond");
            CollectionAssert.AreEquivalent(new string[] { }, words.ToList());
        }
        public void TestRemoveFirstChar01()
        {
            char[] sample = "sample".ToCharArray();
            char[] result = new WordTree().RemoveFirstChar(sample);

            Assert.Equal("ample".ToCharArray(), result);
        }
Beispiel #6
0
 public void TestBuildTree(WordTree tree, List <string> words)
 {
     foreach (string word in words)
     {
         string lowerCase = word.ToLower();
         Assert.IsTrue(tree.FindWord(lowerCase));
     }
 }
        public void TestIsCarWordWhenCareIsInsertedBeforeCar()
        {
            var tree = new WordTree();

            tree.AddWord("cat");
            tree.AddWord("care");
            tree.AddWord("car");
            Assert.True(tree.IsWord("car"));
            Assert.True(tree.IsWord("care"));
        }
        public void TestIsWord01()
        {
            var tree = new WordTree();

            tree.AddWord("cat");
            Assert.True(tree.IsWord("cat"));
            Assert.False(tree.IsWord("cato"));
            Assert.False(tree.IsWord("c"));
            Assert.False(tree.IsWord("ca"));
        }
Beispiel #9
0
 public void TestPermutations(WordTree tree, List <string> words)
 {
     for (int i = 0; i < words.Count; i += 3000)
     {
         foreach (var w in tree.FindPermutations(words[i]))
         {
             Assert.IsTrue(tree.FindWord(w));
         }
     }
 }
Beispiel #10
0
        public void Constructor_WhenWordTreeIsNull_ThrowsException()
        {
            // Arrange
            WordTree wordTree = null !;

            // Act
            var exception = Assert.ThrowsException <ArgumentNullException>(() => new Solver(wordTree));

            // Assert
            Assert.AreEqual("Value cannot be null. (Parameter 'wordTree')", exception.Message);
        }
Beispiel #11
0
    public IList <string> GetValidT9Words(string num, string[] words)
    {
        WordTree tree = new WordTree('\0');

        foreach (string word in words)
        {
            WordTree node = tree;
            foreach (char w in word)
            {
                if (!node.next.ContainsKey(w))
                {
                    node.next[w] = new WordTree(w);
                }
                node = node.next[w];
            }
            node.finish = true;
        }
        Dictionary <char, char[]> dict = new Dictionary <char, char[]>()
        {
            { '2', new char[] { 'a', 'b', 'c' } },
            { '3', new char[] { 'd', 'e', 'f' } },
            { '4', new char[] { 'g', 'h', 'i' } },
            { '5', new char[] { 'j', 'k', 'l' } },
            { '6', new char[] { 'm', 'n', 'o' } },
            { '7', new char[] { 'p', 'q', 'r', 's' } },
            { '8', new char[] { 't', 'u', 'v' } },
            { '9', new char[] { 'w', 'x', 'y', 'z' } }
        };

        List <string> res = new List <string>();

        void dfs(int index, WordTree node, System.Text.StringBuilder sb)
        {
            if (index >= num.Length && node.finish)
            {
                res.Add(sb.ToString());
                return;
            }

            foreach (char c in dict[num[index]])
            {
                if (!node.next.ContainsKey(c))
                {
                    continue;
                }
                sb.Append(c);
                dfs(index + 1, node.next[c], sb);
                sb.Remove(sb.Length - 1, 1);
            }
        }

        dfs(0, tree, new System.Text.StringBuilder());
        return(res);
    }
Beispiel #12
0
        private IList <Solution> Solve(WordTree wordTree)
        {
            var solver    = new Solver(wordTree);
            var solutions = new List <Solution>();

            foreach (var solution in solver.Solve(_puzzle, _progress))
            {
                solutions.Add(solution);
                _progress.ClearReport();
                _stdout.WriteLine(solution);
            }
            _progress.ClearReport();
            return(solutions);
        }
Beispiel #13
0
        public void ToString_WhenPartiallySolved_ReturneCorrectValue()
        {
            // Arrange
            Puzzle   puzzle   = CreatePuzzle();
            WordTree wordTree = new WordTree(new[] { "OAR" });
            Sequence sequence = new Sequence(puzzle, wordTree).Extend().First().Extend().First().Extend().First();

            sequence.TryPlay(out puzzle !);

            // Act
            var result = puzzle.Solution.ToString();

            // Assert
            Assert.AreEqual("OAR ___ ___", result);
        }
Beispiel #14
0
        public void Words_WhenPartiallySolved_ReturnsCorrectValue()
        {
            // Arrange
            Puzzle   puzzle   = CreatePuzzle();
            WordTree wordTree = new WordTree(new[] { "OAR" });
            Sequence sequence = new Sequence(puzzle, wordTree).Extend().First().Extend().First().Extend().First();

            sequence.TryPlay(out puzzle !);

            // Act
            var result = puzzle.Solution.Words.ToList();

            // Assert
            CollectionAssert.AreEquivalent(new[] { "OAR", "___", "___" }, result);
        }
Beispiel #15
0
        public void ToString_WhenPartiallySolved_ReturnsCorrectFormat()
        {
            // Arrange
            Puzzle   puzzle   = CreatePuzzle();
            WordTree wordTree = CreateWordTree(new[] { "OAR" });
            Sequence sequence = new Sequence(puzzle, wordTree).Extend().First().Extend().First().Extend().First();

            sequence.TryPlay(out puzzle !);

            // Act
            string result = puzzle.ToString();

            // Assert
            Assert.AreEqual($". . O{Environment.NewLine}F . Z{Environment.NewLine}B A B{Environment.NewLine}" +
                            $"OAR ___ ___{Environment.NewLine}", result);
        }
Beispiel #16
0
        public void Start()
        {
            // Read word list
            var start    = _now();
            var words    = _fileReader(_path);
            var wordTree = new WordTree(words);

            _stdout.WriteLine(Strings.Program_ReadWordsFormat, words.Length, _now() - start);
            _stdout.WriteLine();

            // Solve puzzle
            start = _now();
            _stdout.WriteLine(_puzzle);
            var solutions = Solve(wordTree);

            _stdout.WriteLine();
            _stdout.WriteLine(Strings.Program_FoundSolutionsFormat, solutions.Count, _now() - start);
        }
Beispiel #17
0
 public Correction(BitmapSource image, WordTree tree)
     : base(image)
 {
     Tree = tree;
 }