Пример #1
0
    public static void Main()
    {
        // Create an AlchemyAPI object.
        AlchemyAPI.AlchemyAPI alchemyObj = new AlchemyAPI.AlchemyAPI();
        AlchemyAPI_KeywordParams keywordParams = new AlchemyAPI_KeywordParams();
        AlchemyAPI_EntityParams entityParams = new AlchemyAPI_EntityParams();

        // Load an API key from disk.
        alchemyObj.LoadAPIKey("api_key.txt");
        keywordParams.setMaxRetrieve(1);
        keywordParams.setShowSourceText(true);
        keywordParams.setSourceText(AlchemyAPI_KeywordParams.SourceTextMode.RAW);
        keywordParams.setSentiment(true);
        // Extract a ranked list of named entities from a web URL with parameters.
        string xml = alchemyObj.URLGetRankedKeywords("http://www.techcrunch.com/", keywordParams);
        Console.WriteLine (xml);

        // Load a HTML document to analyze.
        StreamReader streamReader = new StreamReader("data/example.html");
        string htmlDoc = streamReader.ReadToEnd();
        streamReader.Close();

        entityParams.setMaxRetrieve(3);
        entityParams.setDisambiguate(true);
        entityParams.setOutputMode(AlchemyAPI_BaseParams.OutputMode.RDF);
        entityParams.setSentiment(true);
        // Extract a ranked list of named entities from a HTML document with parameters.
        xml = alchemyObj.HTMLGetRankedNamedEntities(htmlDoc, "http://www.test.com/", entityParams);
        Console.WriteLine (xml);
    }
Пример #2
0
        public static void ProcessUrl(string url, DateTime date, string source, string title)
        {
            if (LinkList.Count < 1)
                LoadProcessedLinks(date);

            if (CheckIfUrlProcessed(url))
                return;

            try
            {
                AlchemyAPI.AlchemyAPI alchemyObj = new AlchemyAPI.AlchemyAPI();
                alchemyObj.LoadAPIKey("api_key.txt");
                AlchemyAPI.AlchemyAPI_EntityParams prms = new AlchemyAPI.AlchemyAPI_EntityParams();
                prms.setSentiment(true);
                prms.setMaxRetrieve(1000);
                prms.setOutputMode(AlchemyAPI_BaseParams.OutputMode.XML);
                string in_xml = alchemyObj.URLGetRankedNamedEntities(url, prms);
                string mid_xml = in_xml.Replace("<sentiment>", "");
                string s_xml = mid_xml.Replace("</sentiment>", "");
                string xml_01 = s_xml.Replace(@"<type>Person</type>", @"<Stype>Person</Stype>");
                string xml_02 = xml_01.Replace(@"<type>PrintMedia</type>", @"<Stype>PrintMedia</Stype>");
                string xml_03 = xml_02.Replace(@"<type>JobTitle</type>", @"<Stype>JobTitle</Stype>");
                string xml_04 = xml_03.Replace(@"<type>FieldTerminology</type>", @"<Stype>FieldTerminology</Stype>");
                string xml_05 = xml_04.Replace(@"<type>Company</type>", @"<Stype>Company</Stype>");
                string xml_06 = xml_05.Replace(@"<type>City</type>", @"<Stype>City</Stype>");
                string xml_07 = xml_06.Replace(@"<type>State</type>", @"<Stype>State</Stype>");  //
                string xml_08 = xml_07.Replace(@"<type>Country</type>", @"<Stype>Country</Stype>");
                string xml_09 = xml_08.Replace(@"<type>Automobile</type>", @"<Stype>Automobile</Stype>");
                string xml_10 = xml_09.Replace(@"<type>TelevisionStation</type>", @"<Stype>TelevisionStation</Stype>");
                string xml_11 = xml_10.Replace(@"<type>StateOrCounty</type>", @"<Stype>StateOrCounty</Stype>");
                string xml_12 = xml_11.Replace(@"<type>Organization</type>", @"<Stype>Organization</Stype>");
                string xml_13 = xml_12.Replace(@"<type>OperatingSystem</type>", @"<Stype>OperatingSystem</Stype>");
                string xml_14 = xml_13.Replace(@"<type>Crime</type>", @"<Stype>Crime</Stype>");
                string xml_15 = xml_14.Replace(@"<type>Technology</type>", @"<Stype>Technology</Stype>");
                string xml_16 = xml_15.Replace(@"<type>Facility</type>", @"<Stype>Facility</Stype>");
                string xml_17 = xml_16.Replace(@"<type>Continent</type>", @"<Stype>Continent</Stype>");
                string xml_18 = xml_17.Replace(@"<type>FinancialMarketIndex</type>", @"<Stype>FinancialMarketIndex</Stype>");
                string xml = xml_18.Replace(@"<type>HealthCondition</type>", @"<Stype>HealthCondition</Stype>");

                if (Historical.printAPIresultXML)
                {
                    try
                    {
                        File.Delete("f.xml");
                        File.WriteAllText("f.xml", xml);
                    }
                    catch { }
                }

                DataSet newN = new DataSet();
                newN.ReadXml(new StringReader(xml), XmlReadMode.Auto);

                DataTable table = new DataTable();
                table = newN.Tables[2];

                lock (table)
                {
                    using (MySqlConnection cn1 = new MySqlConnection(connectMySQL))
                    {
                        MySqlCommand cmd;

                        foreach (DataRow row in table.Rows)
                        {
                            cn1.Open();
                            string txt = row["text"].ToString().Replace("'", "").Replace("`", "").Replace("’", "");
                            string mxd = "";
                            try
                            {
                                mxd = row["mixed"].ToString().Replace("'", "").Replace("`", "").Replace("’", "");
                            }
                            catch { }

                            var sqlDate = date.Date.ToString("yyyy-MM-dd");
                            string query = @"Insert INTO api_results (Stype, artdate, source, url, api, relevance, score, mixed, count, text)  values ('" + row["Stype"] + "','" + sqlDate + "','" + source + "','" + url + "','Alchemy','" + row["relevance"] + "','" + row["score"] + "','" + mxd + "','" + row["count"] + "','" + txt + "')";
                            cmd = new MySqlCommand(query, cn1);
                            cmd.ExecuteNonQuery();
                            cn1.Close();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                using (StreamWriter writer = new StreamWriter("errors-exceptions-Log.txt", true))
                {
                    writer.WriteLine(DateTime.Now.ToString("dd/MM/yy | hh:mm:ss :: ")+ url +" ||"  + ex.ToString());
                }
                return;
            }
        }