Exemplo n.º 1
0
 private void DrawNode(INode node, StreamWriter sw)
 {
     if (_scheme.GetIsContraintTree())
     {
         sw.Write("-{0}-\\n", _scheme.GetTopicID(node));
         foreach (var kvp in _scheme.GetInterTreeEdges(node))
         {
             sw.Write("~{0} ({1},{2})~\\n", kvp.Key, kvp.Value.Item1, kvp.Value.Item2);
         }
     }
     else
     {
         if (_tree.GetChildCount(node) == 0)
         {
             sw.Write("-{0}-\\n", _scheme.GetTopicID(node));
             sw.Write("[{0}]\\n", _scheme.GetDocIndex(node));
         }
         else
         {
             sw.Write("-{0}-\\n", _scheme.GetTopicID(node));
             foreach (var kvp in _scheme.GetTopicWords(node))
             {
                 sw.Write("{0}({1})\\n", kvp.Key, kvp.Value);
             }
         }
     }
 }
Exemplo 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));
        }