Ejemplo n.º 1
0
        public static void VisualizeTree(IEnumerable <string> brtFiles, string luceneIndex = null, string[] keywords = null, bool isRemoveLeafNodes = true)
        {
            List <ITree> trees = new List <ITree>();

            foreach (var brtFile in brtFiles)
            {
                //Read tree from file
                TreeDataParser parser = new TreeDataParser(brtFile, isRemoveLeafNodes);
                var            tree   = parser.GetTree();
                Trace.WriteLine(tree.GetDepth(tree.Root));
                if (luceneIndex != null)
                {
                    var scheme = TreeNodeScheme.Get(tree.Graph.NodeTable);
                    scheme.SetIndexReader(LuceneOperations.GetIndexReader(luceneIndex));
                    scheme.SetBRTFileName(brtFile);
                }
                trees.Add(tree);
            }

            //Print analyze info
            DoubleStatistics depthStat        = new DoubleStatistics();
            DoubleStatistics internalNodeStat = new DoubleStatistics();

            foreach (var tree in trees)
            {
                depthStat.AddNumber(tree.BFS(tree.Root).Max(node =>
                {
                    int depth      = 0;
                    INode ancestor = node;
                    while ((ancestor = tree.GetParent(ancestor)) != null)
                    {
                        depth++;
                    }
                    return(depth);
                }) + 1);
                internalNodeStat.AddNumber(tree.BFS(tree.Root).Count());
            }
            Console.WriteLine(depthStat.ToString());
            Console.WriteLine(internalNodeStat.ToString());

            //Visualize tree
            Thread NetServer = new Thread(new ThreadStart(() =>
            {
                TreeVisualization treeVis = new TreeVisualization(trees, keywords);
            }));

            NetServer.SetApartmentState(ApartmentState.STA);
            NetServer.IsBackground = true;
            NetServer.Start();
            System.Windows.Threading.Dispatcher.Run();
        }
Ejemplo n.º 2
0
        public static ITree GetCopiedTree(ITree tree, int copyFactor, ref int largestTopicID)
        {
            if (copyFactor < 2)
            {
                throw new NotImplementedException();
            }

            var scheme  = TreeNodeScheme.Get(tree.Graph.NodeTable);
            var graph2  = new Graph();
            var scheme2 = new TreeNodeScheme(graph2, isContraintTree: scheme.GetIsContraintTree());

            //root
            var root2 = graph2.AddNode();

            //below
            var nodeBFSList = TreeUtils.GetBreathFirstTraversalList(tree.Root, node => tree.GetChildren(node));

            if (largestTopicID == -1)
            {
                largestTopicID = nodeBFSList.Max(node => scheme.GetTopicID(node));
            }
            var offset = 0;

            for (int iFactor = 0; iFactor < copyFactor; iFactor++)
            {
                foreach (var node in nodeBFSList)
                {
                    //Add node
                    var node2 = graph2.AddNode();
                    scheme2.SetTopicID(node2, scheme.GetTopicID(node) + offset);
                    scheme2.SetDocIndex(node2, scheme.GetDocIndex(node));
                    var words = scheme.GetTopicWords(node);
                    if (words != null)
                    {
                        scheme2.SetTopicWords(node2, new Dictionary <string, double>(words));
                    }
                    var interTreeLinks = scheme.GetInterTreeEdges(node);
                    if (interTreeLinks != null)
                    {
                        var interTreeLinks2 = new Dictionary <int, Tuple <double, double> >();
                        foreach (var kvp in interTreeLinks)
                        {
                            interTreeLinks2.Add(kvp.Key + offset, Tuple.Create(kvp.Value.Item1, kvp.Value.Item2));
                        }
                        scheme2.SetInterTreeEdges(node2, interTreeLinks2);
                    }

                    //Add edge
                    var   parent = tree.GetParent(node);
                    INode parent2;
                    if (parent == null)
                    {
                        parent2 = root2;
                    }
                    else
                    {
                        parent2 = scheme2.GetNodeByTopicID(scheme.GetTopicID(parent) + offset);
                    }
                    graph2.AddEdge(parent2, node2);
                }

                offset += largestTopicID + 100;
            }

            //set root
            scheme2.SetTopicID(root2, 999999);
            if (scheme2.GetIsContraintTree())
            {
                var interTreeLinks2 = new Dictionary <int, Tuple <double, double> >();
                interTreeLinks2.Add(999999, Tuple.Create(1.0, 1.0));
                scheme2.SetInterTreeEdges(root2, interTreeLinks2);
            }
            else
            {
                var words  = scheme.GetTopicWords(tree.Root);
                var words2 = new Dictionary <string, double>();
                foreach (var kvp in words)
                {
                    words2.Add(kvp.Key, kvp.Value * copyFactor);
                }
                scheme2.SetTopicWords(root2, words2);
            }

            return(graph2.GetSpanningTree(root2));
        }
Ejemplo n.º 3
0
 public TreeDataWriter(ITree tree, string brtFileName)
 {
     _tree        = tree;
     _brtFileName = brtFileName;
     _scheme      = TreeNodeScheme.Get(_tree.Graph.NodeTable);
 }