Пример #1
0
        /// <summary>
        /// Recursive function that finds and
        /// graphs Wikipedia links
        /// </summary>
        /// <param name="g">The graph</param>
        /// <param name="lookupValue">Name of orgin article</param>
        /// <param name="hops">How many degrees of separation from the original article</param>
        private void addLinks(Kitware.VTK.vtkMutableDirectedGraph g, string lookupValue, int hops)
        {
            vtkStringArray label  = (vtkStringArray)g.GetVertexData().GetAbstractArray("label");
            long           parent = label.LookupValue(lookupValue);

            //if lookupValue is not in the graph add it
            if (parent < 0)
            {
                rotateLogo();
                parent = g.AddVertex();
                label.InsertNextValue(lookupValue);
                arrListSmall.Add(lookupValue);
            }
            //Parse Wikipedia for the lookupValue
            string underscores = lookupValue.Replace(' ', '_');

            System.Net.HttpWebRequest webRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create("http://en.wikipedia.org/wiki/Special:Export/" + underscores);
            webRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;
            webRequest.Accept      = "text/xml";
            try
            {
                System.Net.HttpWebResponse webResponse    = (System.Net.HttpWebResponse)webRequest.GetResponse();
                System.IO.Stream           responseStream = webResponse.GetResponseStream();
                System.Xml.XmlReader       reader         = new System.Xml.XmlTextReader(responseStream);
                String NS = "http://www.mediawiki.org/xml/export-0.4/";
                System.Xml.XPath.XPathDocument doc = new System.Xml.XPath.XPathDocument(reader);
                reader.Close();
                webResponse.Close();
                System.Xml.XPath.XPathNavigator    myXPahtNavigator = doc.CreateNavigator();
                System.Xml.XPath.XPathNodeIterator nodesText        = myXPahtNavigator.SelectDescendants("text", NS, false);

                String fullText = "";
                //Parse the wiki page for links
                while (nodesText.MoveNext())
                {
                    fullText += nodesText.Current.InnerXml + " ";
                }
                System.Text.RegularExpressions.Match m = System.Text.RegularExpressions.Regex.Match(fullText, "\\[\\[.*?\\]\\]");
                int max;
                try
                {
                    max = System.Convert.ToInt32(toolStripTextBox2.Text);
                }
                catch (Exception)
                {
                    max = -1;
                }
                int count = 0;
                while (m.Success && ((count < max) || (max < 0)))
                {
                    String s         = m.ToString();
                    int    index     = s.IndexOf('|');
                    String substring = "";
                    if (index > 0)
                    {
                        substring = s.Substring(2, index - 2);
                    }
                    else
                    {
                        substring = s.Substring(2, s.Length - 4);
                    }
                    //if the new substring is not already there add it
                    long v = label.LookupValue(substring);
                    if (v < 0)
                    {
                        rotateLogo();
                        v = g.AddVertex();
                        label.InsertNextValue(substring);
                        arrListSmall.Add(substring);
                        if (hops > 1)
                        {
                            addLinks(g, substring, hops - 1);
                        }
                    }
                    else if (arrListSmall.IndexOf(substring) < 0)
                    {
                        arrListSmall.Add(substring);
                        if (hops > 1)
                        {
                            addLinks(g, substring, hops - 1);
                        }
                    }
                    //Make sure nothing is linked to twice by expanding the graph
                    vtkAdjacentVertexIterator avi = vtkAdjacentVertexIterator.New();
                    g.GetAdjacentVertices((int)parent, avi);
                    m = m.NextMatch();
                    ++count;

                    while (avi.HasNext())
                    {
                        long id = avi.Next();
                        if (id == v)
                        {
                            return;
                        }
                    }
                    rotateLogo();
                    g.AddGraphEdge((int)parent, (int)v);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }