public static void InsertToStockInfo(MongoConnection conn, StockCorrelation correlation) { ObjectId stockCorrelationId = new ObjectId(); stockCorrelationId = conn.InsertDocument("stock_info", correlation.GetBsonDocument()); if (stockCorrelationId == null || stockCorrelationId.Equals("")) { Console.WriteLine("Stock Correlation: " + "Insert Document returned empty? " + stockCorrelationId); } else { Console.WriteLine("Stock Correlation: " + "Successfully inserted stock correlation into stock_info at id: " + stockCorrelationId); } }
public static List <StockListing> RunCorrelation(MongoConnection conn, List <Entity> entities) { List <StockListing> nameMatch = new List <StockListing>(); List <StockListing> relatedMatch = new List <StockListing>(); foreach (Entity entity in entities) { //Check related entities List <StockListing> allMatches = CheckRelatedEntities(conn, entity.Name); foreach (StockListing match in allMatches) { var lookup = from stock in relatedMatch where stock.Symbol == match.Symbol select stock; if (lookup.Count() == 0) { relatedMatch.Add(match); } } //Check by organization name if (entity.Type == Entity.Types.Type.Organization) { nameMatch = GetStocks(conn, "SecurityName", entity.Name, false); if (nameMatch.Count > 0) { //Add/update entity in the related_entities table ProcessAllEntities(conn, entity, nameMatch, entities); } } } //Add all stocks to list List <StockListing> stocks = new List <StockListing>(nameMatch.Count + relatedMatch.Count); stocks.AddRange(nameMatch); stocks.AddRange(relatedMatch); return(stocks); }
public static void ProcessAllEntities(MongoConnection conn, Entity nameMatch, List <StockListing> stocks, List <Entity> entities) { try { foreach (StockListing stock in stocks) { foreach (Entity entity in entities) { if (entity.Name != nameMatch.Name) { List <BsonDocument> lookup = conn.GetFilterEq("related_entities", "keyword", entity.Name); if (lookup.Count > 0) { if (lookup.Count > 1) { Console.WriteLine($"Found multiple related_entities entries for '{entity.Name}', please check the database and combine duplicate entries into a single entry."); } BsonDocument relatedEntity = lookup[0]; BsonArray companies = relatedEntity["companies"].AsBsonArray; if (relatedEntity.ToString().Contains(stock.Symbol)) { foreach (BsonDocument company in companies) { if (company["stockSymbol"].ToString() == stock.Symbol) { int occurrences = company["occurrences"].ToInt32() + 1; // Update occurrences of the company var objectId = relatedEntity["_id"].AsObjectId; Dictionary <string, object> conditions = new Dictionary <string, object> { { "_id", objectId }, { "companies.stockSymbol", stock.Symbol } }; conn.UpdateDocument("related_entities", conditions, "companies.$.occurrences", occurrences); } } } else { var newCompany = BuildCompanyBsonDocument(stock.Symbol); // Add the company into companies of the entity var objectId = relatedEntity["_id"].AsObjectId; Dictionary <string, object> conditions = new Dictionary <string, object> { { "_id", objectId } }; conn.AddDocumentToArray("related_entities", conditions, "companies", newCompany); } } else { var newRelatedEntity = BuildRelatedEntityBsonDocument(entity.Name, stock.Symbol); conn.InsertDocument("related_entities", newRelatedEntity); } } } } } catch (Exception ex) { Console.WriteLine(ex.StackTrace); Console.WriteLine(ex.Message); } }
/// <summary> /// Insert entity sentiment result into DB /// </summary> /// <param name="entitySentimentBsonDocument">Entity sentiment result in BsonDocument</param> /// <param name="conn">MongoConnection</param> /// <returns>ObjectId of the inserted entity sentiment result</returns> public static ObjectId InsertSentimentResult(BsonDocument sentimentBsonDocument, MongoConnection conn) { try { //Insert to DB var sentimentDocId = conn.InsertDocument("sentiment_results", sentimentBsonDocument); return(sentimentDocId); } catch (Exception e) { throw new Exception("Exception at NLPMethods InsertEntitySentiment!: " + e.Message); } }