Ejemplo n.º 1
0
        public void RemoveTieredNodeTest()
        {
            IBaseTree tree = new BaseTree();

            String root  = "World";
            String child = "US";

            tree.AddWord((string)null, root);
            tree.AddWord(root, child);

            Assert.IsTrue(tree.Contains(root));
            Assert.IsTrue(tree.Contains(child));

            tree.RemoveNode(tree.GetNode(root));

            Assert.IsFalse(tree.Contains(root));
            Assert.IsFalse(tree.Contains(child));

            try {
                tree.GetNode(child);
                Assert.Fail();
            } catch (KeyNotFoundException) {
            }

            try {
                tree.GetNode(root);
                Assert.Fail();
            } catch (KeyNotFoundException) {
            }
        }
Ejemplo n.º 2
0
        public void AddDuplicateTest()
        {
            IBaseTree tree = new BaseTree();

            String root = "World";

            tree.AddWord((string)null, root);
            tree.AddWord(root, root);
        }
Ejemplo n.º 3
0
        public void AddParentedNodesTest()
        {
            IBaseTree tree = new BaseTree();

            String root  = "world";
            String child = "us";

            tree.AddWord((string)null, root);
            tree.AddWord(root, child);

            Assert.IsTrue(tree.Contains(root));
            Assert.IsTrue(tree.Contains(child));
            Assert.AreEqual(root, tree.Root.KeyWord);
            Assert.AreEqual(root, tree.GetNode(root).KeyWord);
            Assert.AreEqual(child, tree.GetNode(child).KeyWord);
            Assert.AreEqual(root, tree.GetNode(child).Parent.KeyWord);
        }
Ejemplo n.º 4
0
        public IBaseTree setUpTree()
        {
            IBaseTree tree = new BaseTree();

            tree.AddWord((string)null, "Root");
            tree.AddWord(root, "tree");
            tree.AddWord(root, "Others");
            tree.AddWord("tree", "content");
            tree.AddWord("tree", "mapped");
            tree.AddWord("others", "series");
            tree.AddWord("others", "going");
            tree.AddWord(root, "nonexistent");

            return(tree);
        }
Ejemplo n.º 5
0
        public void AddWordNodeParentTest()
        {
            IBaseTree tree = new BaseTree();

            Assert.IsNull(tree.Root);
            Assert.IsFalse(tree.Contains("all"));

            tree.AddWord((Node)null, "all");

            Assert.IsNotNull(tree.Root);
            Assert.AreEqual("all", tree.Root.KeyWord);
            Assert.IsTrue(tree.Contains("all"));
            Assert.AreEqual("all", tree.GetNode("all").KeyWord);
        }
Ejemplo n.º 6
0
        public void RenameTest()
        {
            IBaseTree tree = new BaseTree();

            string root      = "all";
            string different = "different";

            tree.AddWord((string)null, root);
            Assert.AreEqual(root, tree.Root.KeyWord);

            tree.Rename(tree.Root, different);

            Assert.AreEqual(tree.Root.KeyWord, different);
        }
Ejemplo n.º 7
0
        public void GetEnumeratorTest()
        {
            IBaseTree tree = new BaseTree();

            String root = "World";

            tree.AddWord((string)null, root);

            IEnumerator <Node> e = (IEnumerator <Node>)tree.GetEnumerator();

            Assert.IsNotNull(e);
            e.MoveNext();
            Assert.IsNotNull(e.Current);
        }
Ejemplo n.º 8
0
        public void RemoveTieredNodeWithMultipleChildrenTest()
        {
            IBaseTree tree = new BaseTree();

            String root     = "World";
            String child    = "US";
            String child2   = "UK";
            String child3   = "France";
            String child4   = "Australia";
            String childt2  = "Colorado";
            String childt22 = "Utah";
            String childt23 = "California";

            tree.AddWord((string)null, root);
            tree.AddWord(root, child);
            tree.AddWord(root, child2);
            tree.AddWord(root, child3);
            tree.AddWord(root, child4);
            tree.AddWord(child, childt2);
            tree.AddWord(child, childt22);
            tree.AddWord(child, childt23);

            Assert.IsTrue(tree.Contains(root));
            Assert.IsTrue(tree.Contains(child));
            Assert.IsTrue(tree.Contains(child2));
            Assert.IsTrue(tree.Contains(child3));
            Assert.IsTrue(tree.Contains(child4));
            Assert.IsTrue(tree.Contains(childt2));
            Assert.IsTrue(tree.Contains(childt22));
            Assert.IsTrue(tree.Contains(childt23));

            tree.RemoveNode(tree.GetNode(root));

            Assert.IsFalse(tree.Contains(root));
            Assert.IsFalse(tree.Contains(child));
            Assert.IsFalse(tree.Contains(child2));
            Assert.IsFalse(tree.Contains(child3));
            Assert.IsFalse(tree.Contains(child4));
            Assert.IsFalse(tree.Contains(childt2));
            Assert.IsFalse(tree.Contains(childt22));
            Assert.IsFalse(tree.Contains(childt23));
        }
Ejemplo n.º 9
0
        public IBaseTree BuildTreeGoodMatches()
        {
            //change the .1?
            var orderedByIDF = WordDocumentAppearances.Where(x => x.Value.IDF > .2).OrderBy(x => x.Value.IDF).ToList();
            //int b = 0;
            //var orderedByIDF = WordDocumentAppearances.Where(x => !Int32.TryParse(x.Key, out b)).OrderByDescending(x => x.Value.IDF).Take((int)(WordDocumentAppearances.Count * .7)).ToList();
            IBaseTree tree = new BaseTree();

            tree.AddWord((string)null, "the");
            orderedByIDF = orderedByIDF.OrderBy(x => x.Value.IDF).ToList();
            //Make the cutoff greater than 0, so not quite all words are added? - taken care of above?
            while (orderedByIDF.Count > 0)
            {
                //decide next word to add
                string word = orderedByIDF.First().Key;
                Console.WriteLine("Adding word '" + word + "' to tree");

                //find lowest branch in which all significant occurences of the word/document happen
                int  numDif = Int32.MaxValue;
                Node parent = null;
                foreach (Node n in tree)   //for every branch in the tree
                //Console.WriteLine("Checking branch");
                //first check to make sure it is completely contained
                {
                    bool contained = true;
                    foreach (var element in WordDocumentAppearances[word].Docs)
                    {
                        //TODO: Make this check to make sure appearances are significant
                        if (!WordDocumentAppearances[n.KeyWord].Docs.Contains(element) && PossibleContent[new KeyValuePair <string, Document>(word, new Document()
                        {
                            Name = element
                        })].Frequency > 1)
                        {
                            contained = false;
                            break;
                        }
                    }
                    //if it is,
                    if (contained)
                    {
                        //Console.WriteLine("Determining fit");
                        int numDifTemp = 0;
                        foreach (string s2 in WordDocumentAppearances[n.KeyWord].Docs)   //for each document the branch's word appears in
                        {
                            if ((!WordDocumentAppearances[word].Docs.Contains(s2) && PossibleContent[new KeyValuePair <string, Document>(n.KeyWord, new Document()
                            {
                                Name = s2
                            })].Frequency > 1))                                                                                                                                                    // if this word doesn't appear in a document which the other does
                            {
                                numDifTemp++;
                            }
                        }
                        //if the number of different branches is less than the last node's,
                        if (numDifTemp < numDif)
                        {
                            Console.WriteLine("New parent: " + n.KeyWord);
                            //make this the new parent of the new word
                            numDif = numDifTemp;
                            parent = n;
                        }
                    }
                }

                if (parent != null)
                {
                    Console.WriteLine("Adding " + word + " to tree");
                    //add to that branch
                    tree.AddNode(parent, new Node(word));
                }

                orderedByIDF.Remove(orderedByIDF.First());
                Console.WriteLine("Remaining words: " + orderedByIDF.Count);
            }

            return(tree);
        }
Ejemplo n.º 10
0
        public void AddNodeToNonexistentParentNodeTest()
        {
            IBaseTree tree = new BaseTree();

            tree.AddWord(new Node("World"), "US");
        }