partial void DeleteScore(Score instance);
partial void InsertScore(Score instance);
partial void UpdateScore(Score instance);
/* Calculate the cosine similarity measure of ID versus all other documents in the corpus */ private static void ScoreStory(int storyID) { // consider passing data context as an argument to the function using (var _db = new EngineDBDataContext()) { // delete all existing scores for documentID _db.Scores.DeleteAllOnSubmit(from score in _db.Scores where score.Story1ID == storyID || score.Story2ID == storyID select score); _db.SubmitChanges(); // grab the principle document's terms/tfidfs var terms = from term in _db.Terms where term.StoryID == storyID select new { Text = term.Text, tfidf = term.tfidf }; // magnitude of the document's vector var mag1 = Math.Sqrt(terms.Sum(t => t.tfidf)); // ids of documents to score against var ids = from term in _db.Terms where term.StoryID != storyID group term by term.StoryID into g select g.Key; // score against each document foreach (int id in ids) { var id1 = id; // grab the other document's terms/tfidfs var terms2 = from term in _db.Terms where term.StoryID == id1 select new { Text = term.Text, tfidf = term.tfidf }; var mag2 = Math.Sqrt(terms2.Sum(t => t.tfidf)); // calculate the dot product var dot_product = 0.0; foreach (var term in terms) { var term1 = term; var term2 = terms2.SingleOrDefault(t => t.Text.ToLower() == term1.Text.ToLower()); if (term2 != null) { dot_product += term1.tfidf * term2.tfidf; } } var euclidean_dist = mag1 * mag2; // linq may complain about overwriting existing keys... we'll see. if so, delete scores before InsertOnSubmit() var score = new Score { Story1ID = storyID, Story2ID = id, Value = dot_product / euclidean_dist }; if (double.IsNaN(score.Value)) { score.Value = 0; } _db.Scores.InsertOnSubmit(score); } _db.SubmitChanges(); // } }