public void GetDistance_Different() { string query = "<query><RecordSource ComparisonType=\"one of\">ELibrary</RecordSource><Title ComparisonType=\"FreeTextSearchWithStemming\">Защита информации</Title></query>"; SearchRequest request = new SearchRequest(query, "s1"); ExternalCatalogBook book = new ExternalCatalogBook("1", "Методы верификации"); Assert.IsTrue(NGramAnalyzer.GetDistance(request, book) > 0.4); }
public void GetDistance_Identical() { string query = "<query><RecordSource ComparisonType=\"one of\">ELibrary</RecordSource><Title ComparisonType=\"FreeTextSearchWithStemming\">Защита информации</Title></query>"; SearchRequest request = new SearchRequest(query, "s1"); ExternalCatalogBook book = new ExternalCatalogBook("1", "Защита информации"); Assert.AreEqual(NGramAnalyzer.GetDistance(request, book), 0); }
public static List <ExternalCatalogBook> GetBooks(int reportValue, ref BackgroundWorker worker, ref DoWorkEventArgs e) { index = new Dictionary <char, HashSet <ExternalCatalogBook> >(); List <ExternalCatalogBook> books = new List <ExternalCatalogBook>(); int count = 0; try { using (var db = new ExternalCatalogModel()) { var table = db.ExternalCatalog.AsNoTracking(); worker.ReportProgress(table.Count(), BackgroundWorkerState.ExternalCatalogInitialize); foreach (var row in table) { if (worker.CancellationPending) { e.Cancel = true; break; } if (!CatalogProxy.IsPresentInCatalog(row.Title)) { ExternalCatalogBook book = new ExternalCatalogBook(row.Id.ToString(), row.Title, row.Author, row.Keywords); books.Add(book); char t = book.Title[0]; char a = book.Author[0]; if (index.ContainsKey(t)) { index[t].Add(book); } else { index[t] = new HashSet <ExternalCatalogBook>(); index[t].Add(book); } if (index.ContainsKey(a)) { index[a].Add(book); } else { index[a] = new HashSet <ExternalCatalogBook>(); index[a].Add(book); } } count++; if (count % reportValue == 0) { worker.ReportProgress(count, BackgroundWorkerState.ExternalCatalogReportProgress); } } } } catch (Exception ignore) { } return(books); }
private void btnDelete_Click(object sender, EventArgs e) { if (gridResult.SelectedRows.Count > 0) { ExternalCatalogBook book = new ExternalCatalogBook(gridResult.SelectedRows[0].Cells[0].Value.ToString(), gridResult.SelectedRows[0].Cells[1].Value.ToString(), gridResult.SelectedRows[0].Cells[2].Value.ToString(), null); CustomKeyValuePair <ExternalCatalogBook, double> k = new CustomKeyValuePair <ExternalCatalogBook, double>(book, double.Parse(gridResult.SelectedRows[0].Cells[3].Value.ToString())); gridResult.Rows.RemoveAt(gridResult.SelectedRows[0].Index); btnDelete.Enabled = gridResult.SelectedRows.Count > 0; _generalSettings.Recommendations.Remove(k); _generalSettings.DeletedRecommendations.Add(k); } }
public static List <ExternalCatalogBook> GetBooks(string xmlPath, int reportValue, ref BackgroundWorker worker, ref DoWorkEventArgs e) { index = new Dictionary <char, HashSet <ExternalCatalogBook> >(); List <ExternalCatalogBook> books = new List <ExternalCatalogBook>(); XmlDocument xml = new XmlDocument(); xml.Load(xmlPath); XmlNode root = xml.LastChild; XmlNode shopNode = root.LastChild; XmlNode offersNode = shopNode.LastChild; int count = 0; if (offersNode.HasChildNodes) { worker.ReportProgress(offersNode.ChildNodes.Count, BackgroundWorkerState.ExternalCatalogInitialize); for (int i = 0; i < offersNode.ChildNodes.Count; i++) { if (worker.CancellationPending) { e.Cancel = true; break; } XmlNode offerNode = offersNode.ChildNodes[i]; string id = offerNode.Attributes["id"].Value; string title = null, author = null; foreach (XmlNode node in offerNode.ChildNodes) { if (node.Name.Equals("author")) { author = node.InnerText; } if (node.Name.Equals("name")) { title = node.InnerText; } if (title != null && author != null) { break; } } if (isDocumentValid(title, author) && !CatalogProxy.IsPresentInCatalog(title)) { ExternalCatalogBook book = new ExternalCatalogBook(id, title, author, null); books.Add(book); foreach (var word in GetWords(book.Title)) { char t = word[0]; if (index.ContainsKey(t)) { index[t].Add(book); } else { index[t] = new HashSet <ExternalCatalogBook>(); index[t].Add(book); } } foreach (var word in GetWords(book.Author)) { char t = word[0]; if (index.ContainsKey(t)) { index[t].Add(book); } else { index[t] = new HashSet <ExternalCatalogBook>(); index[t].Add(book); } } } if (count % reportValue == 0) { worker.ReportProgress(count, BackgroundWorkerState.ExternalCatalogReportProgress); } count++; if (count == 400) { break; } } } return(books); }
public static double GetDistance(SearchRequest request, ExternalCatalogBook book) { double distance = 0; List <string> requestTitle = new List <string>(); List <string> requestAuthor = new List <string>(); List <string> requestSubject = new List <string>(); List <string> requestKeywords = new List <string>(); AddWordsIfPresent(request, SearchFields.Title, ref requestTitle); AddWordsIfPresent(request, SearchFields.Responsible, ref requestAuthor); AddWordsIfPresent(request, SearchFields.SubjectText, ref requestSubject); AddWordsIfPresent(request, SearchFields.Keywords, ref requestKeywords); if (requestTitle.Count > 0 && requestSubject.Count > 0) { requestKeywords.AddRange(requestSubject); } else if (requestSubject.Count > 0) { requestTitle.AddRange(requestSubject); } requestSubject.Clear(); if (requestTitle.Count == 0) { requestTitle.AddRange(requestKeywords); requestKeywords.Clear(); } string title = book.Title; if (book.HasKeywords() && requestKeywords.Count > 0) { distance += Math.Pow(1.0 - GetSimilarityForFields(requestKeywords, book.Keywords), 2); } else if (requestKeywords.Count > 0) { requestTitle.AddRange(requestKeywords); requestKeywords.Clear(); } if (book.HasAuthor() && requestAuthor.Count > 0) { distance += Math.Pow(1.0 - GetSimilarityForFields(requestAuthor, book.Author), 2); } else if (requestAuthor.Count > 0) { requestTitle.AddRange(requestAuthor); requestAuthor.Clear(); } else if (book.HasAuthor()) { title += " " + book.Author; } if (requestTitle.Count > 0) { distance += Math.Pow(1.0 - GetSimilarityForFields(requestTitle, title), 2); } if (distance == 0) { return(0); } return(Math.Sqrt(distance)); }