private string Calc(List <string> lst) { var str = ""; foreach (var item in lst) { var dict2 = new Dictionary <int, float>(); for (int i = 0; i < CodeArray.Length; i++) { var match = new MatchsMaker(item, CodeArray[i]); dict2.Add(i, match.Score); } var idx = dict2.First(p => p.Value == dict2.Max(x => x.Value)).Key; switch (idx) { case 10: idx = 1; break; case 11: case 12: idx = 0; break; default: break; } str += idx; } return(str); }
public void Classify() { Dictionary <Product, ProductClass> setClass = new Dictionary <Product, ProductClass>(); ProductClassRepository productClassRepository = new ProductClassRepository(); ProductRepository productRepository = new ProductRepository(); List <ProductClass> productClasses = productClassRepository.GetAllProductClasses(); List <Product> products = productRepository.GetAllProducts(); foreach (var product in products) { float similarity = 0.0F; Dictionary <ProductClass, float> res = new Dictionary <ProductClass, float>(); foreach (var cl in productClasses) { string tagStr = ""; foreach (var tag in cl.ClassTags) { tagStr += tag.Tag.TagName + " "; } MatchsMaker match = new MatchsMaker(ReplaceBadStrings(product.Name), tagStr); res.Add(cl, match.Score); //if (similarity < match.Score) //{ // similarity = match.Score; // if (setClass.ContainsKey(product)) // { // setClass.Remove(product); // } // setClass.Add(product, cl); // string output = "Product: " + product.Name + " is " + cl.Name + "(" + tagStr + ") with " + similarity * 100.0 + "% confidence."; // Console.WriteLine(output); // if (!File.Exists(filePath)) // { // File.WriteAllText(filePath, output + Environment.NewLine); // } // else // { // File.AppendAllText(filePath, output + Environment.NewLine); // } //} } var result = res.ToList(); result.Sort((pair1, pair2) => pair2.Value.CompareTo(pair1.Value)); Console.WriteLine("Лучшие 3 совпадения для " + product.Name + ":"); if (!File.Exists(filePath)) { File.WriteAllText(filePath, "Лучшие 3 совпадения для " + product.Name + ":" + Environment.NewLine); } else { File.AppendAllText(filePath, "Лучшие 3 совпадения для " + product.Name + ":" + Environment.NewLine); } for (int i = 0; i < 3; i++) { string output = "Product: " + product.Name + " is " + result.ElementAt(i).Key.Name + " with " + result.ElementAt(i).Value * 100.0 + "% confidence."; Console.WriteLine(output); File.AppendAllText(filePath, output + Environment.NewLine); } } }
public static MatchsMaker getInstance(string ourString, string yourString) { if (matchsMaker == null) { matchsMaker = new MatchsMaker(ourString, yourString); } return(matchsMaker); }
public static MatchsMaker getInstance(string ourString, string yourString) { if(matchsMaker==null) { matchsMaker = new MatchsMaker(ourString, yourString); }else { matchsMaker._lString = ourString; matchsMaker._rString = yourString; matchsMaker.MyInit(); } return matchsMaker; }
private void CompareString(object str) { if (string.IsNullOrEmpty(txtSentence.Text) || str == null || string.IsNullOrEmpty(str.ToString())) { MessageBox.Show("Sentence not found,nothing to compare"); } else { //string result = ResultCompare.MakeResult(txtSentence.Text, str.ToString(), false); float percent = MatchsMaker.getInstance(txtSentence.Text, str.ToString()).Score * 100; string stringPercent = percent.ToString("0.##\\%"); string result = ResultCompare.MakeResultString(txtSentence.Text, str.ToString(), (int)percent); txtProcessing.BeginInvoke(new Action(() => txtResult.Text = result)); } }
// so danh chuoi public static string similmarString(string sourceString, string[] chosingStrings) { string bestSimilar = ""; double score = 0.0; foreach (string s in chosingStrings) { MatchsMaker match = new MatchsMaker(sourceString, s); if (match.Score > score) { bestSimilar = s; score = match.Score; } } return(bestSimilar); }
public void ClassifyV3() { Dictionary <Product, ProductClass> setClass = new Dictionary <Product, ProductClass>(); ProductClassRepository productClassRepository = new ProductClassRepository(); ProductRepository productRepository = new ProductRepository(); List <ProductClass> productClasses = productClassRepository.GetAllProductClasses(); List <Product> products = productRepository.GetAllProducts(); foreach (var product in products) { float similarity = 0.0F; Dictionary <ProductClass, float> res = new Dictionary <ProductClass, float>(); foreach (var cl in productClasses) { MatchsMaker match = new MatchsMaker(ReplaceBadStrings(product.Name), ReplaceBadStrings(cl.Name)); res.Add(cl, match.Score); } var result = res.ToList(); result.Sort((pair1, pair2) => pair2.Value.CompareTo(pair1.Value)); Console.WriteLine("Лучшие 3 совпадения для " + product.Name + ":"); if (!File.Exists(filePath)) { File.WriteAllText(filePath, "Лучшие 3 совпадения для " + product.Name + ":" + Environment.NewLine); } else { File.AppendAllText(filePath, "Лучшие 3 совпадения для " + product.Name + ":" + Environment.NewLine); } for (int i = 0; i < 3; i++) { string output = "Product: " + product.Name + " is " + result.ElementAt(i).Key.Name + " with " + result.ElementAt(i).Value * 100.0 + "% confidence."; Console.WriteLine(output); File.AppendAllText(filePath, output + Environment.NewLine); } } }
public Product CreateProduct(string name) { using (var cc = new CustomersContext()) { List <Product> products = cc.Products.ToList(); Dictionary <Product, float> scores = new Dictionary <Product, float>(); foreach (var pr in products) { MatchsMaker match = new MatchsMaker(pr.Name, name); if (pr.Name == name || match.Score >= 0.85) { scores.Add(pr, match.Score); } } Product product = null; if (scores.Count > 0) { var scoresList = scores.ToList(); scoresList.Sort((pair1, pair2) => pair2.Value.CompareTo(pair1.Value)); product = scoresList.ElementAt(0).Key; } List <ProductClass> productClasses = cc.ProductClasses.Include(pc => pc.ClassTags.Select(ct => ct.Tag)).ToList(); float maxSimilarity = 0.0F; ProductClass specialOne = new ProductClass(); foreach (var cl in productClasses) { string tagStr = ""; try { tagStr = string.Join(" ", cl.ClassTags.Select(ct => ct.Tag.TagName)); } catch (Exception e) { continue; } MatchsMaker match = new MatchsMaker(name, tagStr); if (maxSimilarity < match.Score) { maxSimilarity = match.Score; specialOne = cl; } } if (product == null) { if (specialOne != null) { product = cc.Products.Add(new Product() { Name = name, ClassId = specialOne.Id }); } else { product = cc.Products.Add(new Product() { Name = name, ClassId = NonClassifiedProductsClassId }); } } else { product.ClassId = specialOne != null ? specialOne.Id : NonClassifiedProductsClassId; } cc.SaveChanges(); return(product); } }
// HUINYA public void ClassifyV2() { TagRepository tagRepository = new TagRepository(); List <Tag> tags = tagRepository.GetAllTags(); ProductClassRepository productClassRepository = new ProductClassRepository(); ProductRepository productRepository = new ProductRepository(); List <Product> products = productRepository.GetAllProducts(); List <ProductClass> productClasses = new List <ProductClass>(); foreach (var product in products) { string[] terms = ReplaceBadStrings(product.Name).Split(' '); for (int i = 0; i < terms.Length; i++) { Tag tagMatch = tags.Find(t => t.TagName == terms[i]); if (tagMatch != null) { productClasses.AddRange(productClassRepository.GetProductClassesByTag(tagMatch)); } } Dictionary <ProductClass, float> res = new Dictionary <ProductClass, float>(); foreach (var productClass in productClasses) { string tagStr = ""; foreach (var tag in productClass.ClassTags) { tagStr += tag.Tag.TagName + " "; } MatchsMaker match = new MatchsMaker(ReplaceBadStrings(product.Name), tagStr); res.Add(productClass, match.Score); } var result = res.ToList(); result.Sort((pair1, pair2) => pair2.Value.CompareTo(pair1.Value)); Console.WriteLine("Лучшие 3 совпадения для " + product.Name + ":"); if (!File.Exists(filePath)) { File.WriteAllText(filePath, "Лучшие 5 совпадений для " + product.Name + ":" + Environment.NewLine); } else { File.AppendAllText(filePath, "Лучшие 5 совпадений для " + product.Name + ":" + Environment.NewLine); } if (result.Count < 5) { foreach (var item in result) { string output = "Product: " + product.Name + " is " + item.Key.Name + " with " + item.Value * 100.0 + "% confidence."; Console.WriteLine(output); File.AppendAllText(filePath, output + Environment.NewLine); } } else { for (int i = 0; i < 5; i++) { string output = "Product: " + product.Name + " is " + result.ElementAt(i).Key.Name + " with " + result.ElementAt(i).Value * 100.0 + "% confidence."; Console.WriteLine(output); File.AppendAllText(filePath, output + Environment.NewLine); } } } }