Esempio n. 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);
    }
Esempio n. 2
0
        public string TextGetRankedNamedEntities(string text, AlchemyAPI_EntityParams parameters)
        {
            CheckText(text);
            parameters.setText(text);

            return(POST("TextGetRankedNamedEntities", "text", parameters));
        }
Esempio n. 3
0
        public string URLGetRankedNamedEntities(string url, AlchemyAPI_EntityParams parameters)
        {
            CheckURL(url);
            parameters.setUrl(url);

            return(GET("URLGetRankedNamedEntities", "url", parameters));
        }
Esempio n. 4
0
        public string HTMLGetRankedNamedEntities(string html, string url, AlchemyAPI_EntityParams parameters)
        {
            CheckHTML(html, url);
            parameters.setHtml(html);
            parameters.setUrl(url);

            return(POST("HTMLGetRankedNamedEntities", "html", parameters));
        }
Esempio n. 5
0
        public void Initialize()
        {
            alchemyObj = new AlchemyAPI.AlchemyAPI();
            alchemyObj.LoadAPIKey("alchemyapikey.txt");

            eparams = new AlchemyAPI_EntityParams();
            eparams.setMaxRetrieve(250);

            kparams = new AlchemyAPI_KeywordParams();
            kparams.setMaxRetrieve(250);

            cparams = new AlchemyAPI_ConceptParams();
            cparams.setMaxRetrieve(250);
        }
Esempio n. 6
0
    public static void Main()
    {
        // Create an AlchemyAPI object.
        AlchemyAPI.AlchemyAPI alchemyObj = new AlchemyAPI.AlchemyAPI();

        // Load an API key from disk.
        alchemyObj.LoadAPIKey("api_key.txt");

        // Extract sentiment for a web URL.
        string xml = alchemyObj.URLGetTextSentiment("http://www.techcrunch.com/");
        Console.WriteLine (xml);

        // Extract sentiment from a text string.
        xml = alchemyObj.TextGetTextSentiment("Hello there, my name is Bob Jones.  I live in the United States of America.  Where do you live, Fred?");
        Console.WriteLine (xml);

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

        // Extract sentiment from a HTML document.
        xml = alchemyObj.HTMLGetTextSentiment(htmlDoc, "http://www.test.com/");
        Console.WriteLine (xml);

        // Extract named entities with entity-targeted sentiment.
        AlchemyAPI_EntityParams entityParams = new AlchemyAPI_EntityParams();
        entityParams.setSentiment(true);
        xml = alchemyObj.TextGetRankedNamedEntities("Bryan Adams' genius is unsurpassed.", entityParams);
        Console.WriteLine (xml);

        // Extract keywords with keyword-targeted sentiment.
        AlchemyAPI_KeywordParams keywordParams = new AlchemyAPI_KeywordParams();
        keywordParams.setSentiment(true);
        xml = alchemyObj.TextGetRankedKeywords("Bryan Adams' genius is unsurpassed.", keywordParams);
        Console.WriteLine (xml);

        // Extract Targeted Sentiment
        AlchemyAPI_TargetedSentimentParams sentimentParams = new AlchemyAPI_TargetedSentimentParams();
        sentimentParams.setShowSourceText(true);
        xml = alchemyObj.TextGetTargetedSentiment("This car is terrible.", "car", sentimentParams);
        Console.WriteLine(xml);

        xml = alchemyObj.URLGetTargetedSentiment("http://techcrunch.com/2012/03/01/keen-on-anand-rajaraman-how-walmart-wants-to-leapfrog-over-amazon-tctv/", "Walmart",sentimentParams);
        Console.WriteLine(xml);

        xml = alchemyObj.HTMLGetTargetedSentiment(htmlDoc, "http://www.test.com/", "WujWuj", sentimentParams);
        Console.WriteLine(xml);
    }
Esempio n. 7
0
        public string URLGetRankedNamedEntities(string url, AlchemyAPI_EntityParams parameters)
        {
            CheckURL(url);
            parameters.setUrl(url);

            return GET("URLGetRankedNamedEntities", "url", parameters);
        }
Esempio n. 8
0
        public string TextGetRankedNamedEntities(string text, AlchemyAPI_EntityParams parameters)
        {
            CheckText(text);
            parameters.setText(text);

            return POST("TextGetRankedNamedEntities", "text", parameters);
        }
Esempio n. 9
0
        public string HTMLGetRankedNamedEntities(string html, string url, AlchemyAPI_EntityParams parameters)
        {
            CheckHTML(html, url);
            parameters.setHtml(html);
            parameters.setUrl(url);

            return POST("HTMLGetRankedNamedEntities", "html", parameters);
        }
Esempio n. 10
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;
            }
        }
        public string URLGetRankedNamedEntities(string url)
        {
            CheckURL(url);

            AlchemyAPI_EntityParams entityParams = new AlchemyAPI_EntityParams();
            entityParams.setSentiment(true);

            return URLGetRankedNamedEntities(url, entityParams);
        }
        public string TextGetRankedNamedEntities(string text)
        {
            CheckText(text);

            AlchemyAPI_EntityParams entityParams = new AlchemyAPI_EntityParams();
            entityParams.setSentiment(true);

            return TextGetRankedNamedEntities(text, entityParams);
        }
Esempio n. 13
0
		protected DataSet GetEntities(string url)
		{
			DataSet dsEntities = new DataSet();
#if TEST
			// Using previously captured dataset
			dsEntities.ReadXml("alchemyEntityTestResponse.xml");
#else
			if (!Cached("Entity", url, ref dsEntities))
			{
				AlchemyAPI_EntityParams eparams = new AlchemyAPI_EntityParams();
				eparams.setMaxRetrieve(250);
				string xml = alchemyObj.URLGetRankedNamedEntities(url, eparams);
				TextReader tr = new StringReader(xml);
				XmlReader xr = XmlReader.Create(tr);
				dsEntities.ReadXml(xr);
				xr.Close();
				tr.Close();
				Cache("Entity", url, dsEntities);
			}
#endif
			return dsEntities;
		}