public void Parse(JObject topicJSONObject, TopicItem topicItem)
        {
            JObject propertyJObject = topicJSONObject.Value<JObject>("property");

            if (propertyJObject != null) 
            {
                foreach (JToken child in propertyJObject.Children())
                {
                    var property = child as JProperty;
                    if (property != null && property.Name != null)
                    {
                        switch (property.Name)
                        {
                            case "/common/topic/description":
                                ParseTopicDescription((JObject)property.Value, topicItem);
                                break;
                            case "/common/topic/image":
                                ParseTopicImage((JObject)property.Value, topicItem);
                                break;
                            case "/common/topic/official_website":
                                ParseTopicOfficialWebsite((JObject)property.Value, topicItem);
                                break;
                            case "/common/topic/social_media_presence":
                                ParseTopicSocialMediaPresence((JObject)property.Value, topicItem);
                                break;
                            case "/common/topic/alias":
                                ParseTopicAlias((JObject)property.Value, topicItem);
                                break;
                        }
                    }
                }
            }
        }
        public void FillTopic(TopicItem topicItem)
        {
            String query = topicItem.Query.ToLower().Replace(" ", "_");
            query = TextCleaner.CleanQuery(query);

            if (query.Length > 0)
            {
                try
                {
                    string urlTemplate = "https://www.googleapis.com/freebase/v1/topic/en/{0}?key={1}&{2}";
                    string apiKey = ConfigService.GetConfig(ConfigKeys.GOOGLE_API_KEY, "");
                    string[] domains = new string[] { 
                                         "/common/topic/alias", 
                                         "/common/topic/description", 
                                         "/common/topic/image",
                                         "/common/topic/official_website",
                                         "/common/topic/social_media_presence",
                                         "/influence/influence_node/influenced_by", 
                                         "/people/person/date_of_birth"
                    };
                    string filters = "filter=" + String.Join("&filter=", domains);

                    string url = string.Format(urlTemplate, HttpUtility.UrlEncode(query),apiKey,filters);

                    JSONConnector JSONConnector = new JSONConnector();
                    JObject searchResultsJSONObject = JSONConnector.GetJSONObject(url);

                    new FreebaseTopicParser().Parse(searchResultsJSONObject, topicItem);
                }
                catch (Exception exception)
                {
                    ErrorService.Log("FreebaseTopicService", "FillTopic", topicItem.ToString(), exception);
                }
            }    
        }
        private void DisplayTopic(TopicItem topicItem)
        {
            string htmlEncodedTopic = HttpUtility.HtmlEncode(topicItem.Title);
            string url = URLBuilder.BuildURL("topic", topicItem.Title);

            Response.Write("<li><a href=\"" + url + "\">" + htmlEncodedTopic + "</a></li>");
        }
Ejemplo n.º 4
0
 public void AddTopic(TopicItem newTopic)
 {
     TopicItem topic = GetTopic(newTopic.ID);
     if (topic == null)
     {
         Add(topic);
     }
 }
Ejemplo n.º 5
0
 private void DisplayTopic(TopicItem topicItem)
 {
     Response.Write("<url>");
     Response.Write("<loc>" + URLBuilder.BuildFullLiveURL("topic", topicItem.Title) + "</loc>");
     Response.Write("<changefreq>daily</changefreq>");
     Response.Write("<priority>0.1</priority>");
     Response.Write("</url>");
 }
 private void ParseTopicOfficialWebsite(JObject descriptionJSONObject, TopicItem topicItem)
 {
     JArray valuesJArray = descriptionJSONObject.Value<JArray>("values");
     if (valuesJArray != null && valuesJArray.Count > 0)
     {
         string description = valuesJArray[0].Value<string>("value");
         topicItem.WebsiteURL = description;
     }
 }
 private void ParseTopicImage(JObject descriptionJSONObject, TopicItem topicItem)
 {
     JArray valuesJArray = descriptionJSONObject.Value<JArray>("values");
     if (valuesJArray != null && valuesJArray.Count > 0)
     {
         string id = valuesJArray[0].Value<string>("id");
         topicItem.FreebaseImageURL = "https://usercontent.googleapis.com/freebase/v1/image" + id;
     }
 }
 private void ParseTopicDescription(JObject descriptionJSONObject, TopicItem topicItem)
 {
     JArray valuesJArray = descriptionJSONObject.Value<JArray>("values");
     if (valuesJArray != null && valuesJArray.Count > 0)
     {
         string description = valuesJArray[0].Value<string>("value");
         topicItem.FreebaseSummary = description;
     }
 }
        public void FreebaseTopicTest()
        {
            TopicItem topicItem = new TopicItem("ladygaga", "Lady Gaga");
            topicItem.Query = "Lady Gaga";

            new FreebaseTopicService().FillTopic(topicItem);

            Assert.That(topicItem.FreebaseSummary != null);
            Assert.That(topicItem.FreebaseSummary.Length > 0);

            Console.WriteLine(topicItem.FreebaseSummary);
        }
        public void FillTopic(TopicItem topicItem)
        {
            String query = topicItem.Query.Replace(" ", "_");

            try
            {
                XMLConnector XMLConnector = new XMLConnector();

                bool isRedirect = true;

                while (isRedirect) 
                {

                    isRedirect = false;

                    string url = "http://en.wikipedia.org/wiki/Special:Export/" + query;
                    XmlDocument xmlDocument = XMLConnector.GetXMLDocument(url);

                    XmlNodeList nodeList = xmlDocument.GetElementsByTagName("text");
                    string content = nodeList[0].InnerText;
                    if (content != null && content.Length > 0)
                    {
                        // "#REDIRECT [[Dan Quayle]] {{R from other capitalisation}}"
                        if (content.IndexOf("#REDIRECT") >= 0) 
                        {
                            string newQuery = ParseRedirect(content);
                            if (newQuery != null && newQuery.Length > 0)
                            {
                                if (!newQuery.Equals(query))
                                {
                                    query = newQuery;
                                    isRedirect = true;
                                }
                            }
                        }
                        else 
                        {
                            topicItem.WikipediaSummary = BuildSummary(content);
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                ErrorService.Log("WikipediaTopicService", "FillTopic", topicItem.ToString(), exception);
            }
        }
Ejemplo n.º 11
0
 private void ParseTopicSocialMediaPresence(JObject descriptionJSONObject, TopicItem topicItem)
 {
     JArray valuesJArray = descriptionJSONObject.Value<JArray>("values");
     if (valuesJArray != null)
     {
         foreach (JObject valueJObject in valuesJArray)
         {
             string value = valueJObject.Value<string>("value");
             if (value != null)
             {
                 if (value.Contains("twitter")) topicItem.TwitterURL = value;
                 else if (value.Contains("facebook")) topicItem.FacebookURL = value;
                 else if (value.Contains("myspace")) topicItem.MySpaceURL = value;
             }
         }
     }
 }
Ejemplo n.º 12
0
        public TopicItem GetTopic(string query)
        {
            AuditServiceItem auditServiceItem = AuditService.Register("TopicService", "GetTopic", query);
            AuditService.Start(auditServiceItem);

            TopicItem topicItem = TopicCacheHelper.ReadTopicItem(query);

            if (topicItem == null)
            {
                try
                {
                    String displayQuery = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(query);
                    topicItem = new TopicItem(displayQuery, displayQuery);
                    topicItem.Query = displayQuery;

                    ManualResetEvent[] doneEvents = new ManualResetEvent[2];

                    doneEvents[0] = new ManualResetEvent(false);
                    TopicWorkerThread wikipediaTopicWorkerThread = new TopicWorkerThread(ProviderEnum.Wikipedia, topicItem, doneEvents[0]);
                    ThreadPool.QueueUserWorkItem(wikipediaTopicWorkerThread.ThreadPoolCallback);

                    doneEvents[1] = new ManualResetEvent(false);
                    TopicWorkerThread freebaseTopicWorkerThread = new TopicWorkerThread(ProviderEnum.Freebase, topicItem, doneEvents[1]);
                    ThreadPool.QueueUserWorkItem(freebaseTopicWorkerThread.ThreadPoolCallback);

                    WaitHandle.WaitAll(doneEvents);

                    TopicCacheHelper.CacheTopicItem(query, topicItem);
                }
                catch (Exception exception)
                {
                    ErrorService.Log("TopicService", "GetTopic", query, exception);
                }
            }

            AuditService.End(auditServiceItem);

            return topicItem;
        }
        private CategoriesList BuildCategories()
        {
            CategoriesList categoriesList = new CategoriesList();

            try
            {
                string filepath =  System.Web.Hosting.HostingEnvironment.MapPath("~/data/Categories.xml"); 
                XmlDocument xmlDocument = new XMLConnector().GetXMLDocument(filepath);

                XmlNodeList categoryNodes = xmlDocument.SelectNodes("categories/category");
                if (categoryNodes != null)
                {
                    foreach (XmlNode categoryNode in categoryNodes)
                    {
                        string id = categoryNode.Attributes["id"].Value;                        
                        CategoryItem categoryItem = new CategoryItem(id);
                        categoryItem.Title = categoryNode.Attributes["title"].Value;

                        XmlNodeList providerMappingsNodes = categoryNode.SelectNodes("provider-mappings/provider-mapping");
                        if (providerMappingsNodes != null)
                        {
                            ProviderMappingList providerMappingList = categoryItem.ProviderMappingList;
                            foreach (XmlNode providerMappingNode in providerMappingsNodes)
                            {
                                ProviderMapping providerMapping = new ProviderMapping();
                                providerMapping.Provider = (ProviderEnum) Enum.Parse(typeof(ProviderEnum), providerMappingNode.Attributes["id"].Value);
                                providerMapping.Category = providerMappingNode.Attributes["category"].Value;

                                providerMappingList.Add(providerMapping);
                            }
                        }

                        XmlNodeList topicsNodes = categoryNode.SelectNodes("topics/topic");
                        if (topicsNodes != null)
                        {
                            TopicList topicList = categoryItem.TopicList;
                            foreach (XmlNode topicNode in topicsNodes)
                            {
                                string title = topicNode.Attributes["title"].Value;
                                TopicItem topicItem = new TopicItem(title, title);
                                topicList.Add(topicItem);
                            }
                        }

                        categoriesList.Add(categoryItem);
                    }
                }
            }
            catch (Exception exception)
            {
                ErrorService.Log("CategorizationService", "BuildCategories", null, exception);
            }

            return categoriesList;
        }
Ejemplo n.º 14
0
 public TopicWorkerThread(ProviderEnum provider, TopicItem topicItem, ManualResetEvent doneEvent)
 {
     this.provider = provider;
     this.topicItem = topicItem;
     this.DoneEvent = doneEvent;
 }
Ejemplo n.º 15
0
 private void ParseTopicAlias(JObject descriptionJSONObject, TopicItem topicItem)
 {
     JArray valuesJArray = descriptionJSONObject.Value<JArray>("values");
     if (valuesJArray != null)
     {
         topicItem.FreebaseAliases = new string[valuesJArray.Count];
         int i = 0;
         foreach (JObject valueJObject in valuesJArray)
         {
             string value = valueJObject.Value<string>("value");
             topicItem.FreebaseAliases[i] = value;
             i++;
         }
     }
 }
Ejemplo n.º 16
0
 public static void CacheTopicItem(string query, TopicItem topicItem)
 {
     string cacheName = "topic-" + query;
     int cacheExpiration = ConfigService.GetConfig(ConfigKeys.THEINTERNETBUZZ_TOPIC_EXPIRATION, 60);
     CacheService.Put(cacheName, cacheExpiration, topicItem);
 }