public News ( String Query, String Options, String Market, String Adult, Double Latitude, Double Longitude, String NewsLocationOverride, String NewsCategory, String NewsSortBy ) : DataServiceQuery |
||
Query | String | Bing search query Sample Values : xbox |
Options | String | Specifies options for this request for all Sources. Valid values are: DisableLocationDetection, EnableHighlighting. Sample Values : EnableHighlighting |
Market | String | Market. Note: Not all Sources support all markets. Sample Values : en-US |
Adult | String | Adult setting is used for filtering sexually explicit content Sample Values : Moderate |
Latitude | Double | Latitude Sample Values : 47.603450 |
Longitude | Double | Longitude Sample Values : -122.329696 |
NewsLocationOverride | String | Overrides Bing location detection. This parameter is only applicable in en-US market. Sample Values : US.WA |
NewsCategory | String | The category of news for which to provide results Sample Values : rt_Business |
NewsSortBy | String | The sort order of results returned Sample Values : Date |
return | DataServiceQuery |
public List<BingResult> GetBingNewsResult(string parameter) { //const string bingKey = "9F3eqLyfmNP0PInNOmS13FdSlPoajoJqLvrbvndqZFM"; const string bingKey = "wHln8JsLTWNIEDHZTxU3mTjnIr9T7HCGZPZ86BPYZKg"; var bing = new BingSearchContainer( new Uri("https://api.datamarket.azure.com/Bing/Search/")) { Credentials = new NetworkCredential(bingKey, bingKey) }; var query = bing.News(parameter, null, "en-US", null, null, null, null, "rt_Business","Date"); var results = query.Execute(); List<BingResult> binglist = new List<BingResult> (); foreach (var result in results) { //if (!string.IsNullOrEmpty (result.Url)) { BingResult bingResult = new BingResult (); bingResult.ID = result.ID; bingResult.Url = result.Url; bingResult.Description = result.Description; bingResult.Date = result.Date; bingResult.Title = result.Title; binglist.Add (bingResult); //} } return binglist; }
private static List<NewsResult> GetNewArticles() { var client = new BingSearchContainer( new Uri("https://api.datamarket.azure.com/Bing/search/") ); client.Credentials = new NetworkCredential("accountKey", "5/397uT61db2oYMqva78buLleEq9XbhFhz9iuOX+c5U"); return client.News("Trump", null, "en-US", "Moderate", null, null, null, null, "Date").Execute().ToList(); }
static void updateArticlesForPlayer(string name, string id) { string topics = name; string playerID = id; int numOfArticles = 15; string[] topicsArray = topics.Split('_'); string results = ""; List <string> resultTitles = new List <string>(); List <string> articleBodies = new List <string>(); List <string> urls = new List <string>(); try { string AccountKey = "W9xfglp9hpPxpxgoSsWqXzu7k6f+bqUA9Z+S7QHwwsg"; //For each user topic foreach (string query in topicsArray) { // Root URL for bing search API string rootUrl = "https://api.datamarket.azure.com/Bing/Search"; //Create a Bing Container per API specifications var bingContainer = new Bing.BingSearchContainer(new Uri(rootUrl)); // The market location to use. string location = "en-us"; // Add credentials bingContainer.Credentials = new NetworkCredential(AccountKey, AccountKey); // Create the search, getting top 5 results. var newsQuery = bingContainer.News(query, null, location, null, null, null, null, null, null); newsQuery = newsQuery.AddQueryOption("$top", numOfArticles); // Execute search. var newsResults = newsQuery.Execute(); //Console.WriteLine("Number of articles: " + newsResults.Count()); //Add results to list. foreach (var result in newsResults) { string article = getArticleBody(result.Url); if (!article.Equals("ERROR")) { articleBodies.Add(article); resultTitles.Add(result.Title); urls.Add(result.Url); } else { numOfArticles--; } } } } //Error running the query. catch { results += "Error Processing Request. Please try again"; } Console.WriteLine("Num of articles: {0}", numOfArticles); Console.WriteLine("Num of article bodies: {0}", articleBodies.Count); //Gets the unique words from each of the titles and stores them in a list //Also get number of words in each title List <string> words = new List <string>(); int[] numOfWords = new int[numOfArticles]; for (int i = 0; i < numOfArticles; i++) { string[] sArray = articleBodies[i].Split(' '); numOfWords[i] = sArray.Length; foreach (string s2 in sArray) { if (!words.Contains(s2)) { words.Add(s2); //Console.WriteLine(s2); } } } //Stores the frequency of the occurences of each word int[,] frequencyArray = new int[numOfArticles, words.Count]; Array.Clear(frequencyArray, 0, frequencyArray.Length); for (int i = 0; i < numOfArticles; i++) { string[] sArray = articleBodies[i].Split(' '); foreach (string s2 in sArray) { //Location of the word in the array of words int j = words.IndexOf(s2); frequencyArray[i, j]++; } } //Normalized Frequency Array double[,] nFrequencyArray = new double[numOfArticles, words.Count]; for (int i = 0; i < numOfArticles; i++) { for (int j = 0; j < words.Count; j++) { nFrequencyArray[i, j] = ((double)frequencyArray[i, j]) / ((double)numOfWords[i]); } } //Inverse Document Frequency double[] idf = new double[words.Count]; for (int i = 0; i < words.Count; i++) { //How many documents the current word appears in int appearances = 0; for (int j = 0; j < numOfArticles; j++) { if (frequencyArray[j, i] > 0) { appearances++; } } idf[i] = (double)(1 + Math.Log((double)numOfArticles / (double)appearances)); } //TF-IDF double[,] tfIDF = new double[numOfArticles, words.Count]; for (int i = 0; i < numOfArticles; i++) { for (int j = 0; j < words.Count; j++) { tfIDF[i, j] = idf[j] * nFrequencyArray[i, j]; //Console.WriteLine(tfIDF[i, j]); } } //The location in the title list of each chosen article. The first one is always 0. int[] selectedArticles = { -1, -1, -1, -1 }; //Calculate cosine similarity between all articles, to see which ones are the least similar //int leastSimilarArticle = 0; //double smallestAngle = 1; double[,] cosineArray = new double[numOfArticles, numOfArticles]; for (int k = 0; k < numOfArticles; k++) { for (int i = 0; i < numOfArticles; i++) { double dotProduct = 0; double length1 = 0; double length2 = 0; for (int j = 0; j < words.Count; j++) { dotProduct += (tfIDF[k, j] * tfIDF[i, j]); length1 += Math.Pow(tfIDF[k, j], 2); length2 += Math.Pow(tfIDF[i, j], 2); } double cosine = dotProduct / (Math.Pow(length1, .5) * Math.Pow(length2, .5)); cosineArray[k, i] = Math.Round(cosine, 6); } } //Print out cosine array System.IO.StreamWriter file = new System.IO.StreamWriter("C:\\Users\\Sumbhav\\Documents\\Thesis\\cosine.txt"); for (int i = 0; i < numOfArticles; i++) { for (int j = 0; j < numOfArticles; j++) { file.Write(cosineArray[j, i].ToString("0.000000") + "\t\t\t"); } file.WriteLine(); } file.WriteLine("\n"); //Get the average similarity of each article to the other articles double[] averageSimilarity = new double[numOfArticles]; Array.Clear(averageSimilarity, 0, averageSimilarity.Length); for (int i = 0; i < numOfArticles; i++) { for (int j = 0; j < numOfArticles; j++) { averageSimilarity[i] += cosineArray[j, i]; } averageSimilarity[i] /= numOfArticles; file.WriteLine(averageSimilarity[i]); } //Find the mean and standard deviation of the average similarities double mean = averageSimilarity.Average(); double stDev = StandardDeviation(averageSimilarity); //Current selected article int selectedArticle = -1; //Find and select the articles with a similarity score closest to -2, -1, 0, and 1 standard deviation(s) from the mean for (int i = -2; i <= 1; i++) { double compareValue = mean + i * stDev; //Distance between the compare value and the similarity score of the article being examined double distance = 1.0; for (int j = 0; j < numOfArticles; j++) { if (Math.Abs(averageSimilarity[j] - compareValue) < distance) { distance = Math.Abs(averageSimilarity[j] - compareValue); selectedArticle = j; } } //Add selected article to final selection of articles selectedArticles[i + 2] = selectedArticle; } file.WriteLine(); foreach (int i in selectedArticles) { file.WriteLine(resultTitles[i]); file.WriteLine(urls[i] + "\n"); } file.WriteLine("\n"); foreach (string s in resultTitles) { file.WriteLine(s); } file.Close(); //MySql connection set up MySqlConnectionStringBuilder conn_string = new MySqlConnectionStringBuilder(); conn_string.Server = "sethia.cosfzwdmywyo.us-west-2.rds.amazonaws.com"; conn_string.UserID = "cheetobuv10"; conn_string.Password = "******"; conn_string.Database = "NBAbase"; MySqlConnection l_DBConn = new MySqlConnection(); l_DBConn.ConnectionString = conn_string.ToString(); //Open connection to DB (already open based on where it's being called) //l_DBConn.Open(); //Add articles to database MySqlCommand command; for (int i = 0; i < selectedArticles.Length; i++) { command = l_DBConn.CreateCommand(); command.Connection = l_DBConn; command.CommandText = "replace into Articles SET playerID='" + playerID + "', articleNumber=" + (i + 1) + ", url='" + urls[selectedArticles[i]] + "', title='" + resultTitles[selectedArticles[i]] + "'"; command.ExecuteNonQuery(); } //Close connection to DB l_DBConn.Close(); Console.WriteLine("Database updated with articles"); Console.ReadLine(); }
public List<NewsResult> Query(string company) { string market = "en-us"; string options = null; string adult = null; double? lat = null; double? lon = null; string newLocation = null; string newsCategory = "rt_Business"; string sortBy = "Date"; Container = new BingSearchContainer(new Uri(rootUrl)); Container.Credentials = new NetworkCredential(accountKey, accountKey); var query = Container.News(company, options, market, adult, lat, lon, newLocation, newsCategory, sortBy); List<NewsResult> totalSearchList = new List<NewsResult>(); for (int i = 0; i < 4; i++) { int skip = totalSearchList.Count(); var offsetQuery = query.AddQueryOption("$skip", skip.ToString()); var result = offsetQuery.Execute().ToList(); totalSearchList.AddRange(result); } return totalSearchList; }