// RandD for this applicaiton is in QIResearcher on ALABAMA using System; using System.Collections; using System.Collections.Generic; using System.Text; using System.Text.RegularExpressions; /// /// Weighted Results contains the results to the Query Interpreter analysis. /// public struct WeightedResults { public int Gender; public int SkuModel; public int Brand; public int Category; } /// /// Search Interpreter - Analyzes a user defined search query and makes a determination about what type of query the user is asking for. /// public static class SearchInterpreter { /// /// What type of query is the user asking? /// /// User defined search phrase /// WeightedResults public static WeightedResults Analyze(string keyphrase) { // what are we searching for? string query = keyphrase; // split the query into multiple requests so that each word can be measured string[] keywords = query.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); #region Genders ListEx genders = new ListEx(); genders.Add("men"); genders.Add("mens"); genders.Add("women"); genders.Add("womens"); genders.Add("kid"); genders.Add("kids"); #endregion #region Sku/model List ListEx numbers = new ListEx(); numbers.Add("0"); numbers.Add("1"); numbers.Add("2"); numbers.Add("3"); numbers.Add("4"); numbers.Add("5"); numbers.Add("6"); numbers.Add("7"); numbers.Add("8"); numbers.Add("9"); numbers.Sort(); #endregion #region Category List ListEx categories = new ListEx(); categories.Add("running"); categories.Add("walking"); categories.Add("cross"); categories.Add("crosstraining"); categories.Add("training"); categories.Add("basketball"); categories.Add("tennis"); categories.Add("golf"); categories.Add("sandals"); categories.Add("sandal"); categories.Add("racing"); categories.Add("work"); categories.Add("uniform"); categories.Add("yoga"); categories.Add("retro"); categories.Add("baseball"); categories.Add("cleats"); categories.Add("cleat"); categories.Add("spike"); categories.Add("spikes"); categories.Add("hiking"); categories.Add("soccer"); categories.Add("track"); categories.Add("volleyball"); categories.Add("cheer"); categories.Add("cheering"); categories.Add("footwear"); categories.Add("coaching"); categories.Add("umpire"); categories.Add("aerobic"); categories.Sort(); #endregion #region Brands ListEx brands = new ListEx(); brands.Add("balance"); brands.Add("dunaham"); brands.Add("nike"); brands.Add("reebok"); brands.Add("brooks"); brands.Add("adidas"); brands.Add("puma"); brands.Add("converse"); brands.Add("asics"); brands.Add("saucony"); brands.Add("keen"); brands.Add("salomon"); brands.Add("etonic"); brands.Add("mizuno"); brands.Add("izumi"); brands.Add("spira"); brands.Add("montrail"); brands.Add("north"); brands.Add("ecco"); brands.Add("avia"); brands.Add("keds"); brands.Add("sketchers"); brands.Add("vans"); brands.Add("kswiss"); brands.Add("k-swiss"); brands.Sort(); #endregion WeightedResults weightedresults = new WeightedResults(); #region AnalysisEngine // Genders int genderCount = 0; foreach (string gender in keywords) { int tempgenderCount = genders.BinarySearchCountAll(gender); genderCount += tempgenderCount; } weightedresults.Gender = genderCount; // Sku/Model int numberCount = 0; foreach (string number in keywords) { if (IsNumeric(number) == true || ContainsNumeric(number) == true) numberCount += 1; } weightedresults.SkuModel = numberCount; // Category int categoryCount = 0; foreach (string keyword in keywords) { int count = categories.BinarySearchCountAll(keyword); categoryCount += count; } weightedresults.Category = categoryCount; // Brand int brandCount = 0; foreach (string brand in keywords) { int bCount = brands.BinarySearchCountAll(brand); brandCount += bCount; } weightedresults.Brand = brandCount; #endregion return weightedresults; } // Anything that contains a number is likely an Sku or Model Number private static bool ContainsNumeric(string v) { // Remove any alphaa and then test the string for just numeric values. return Regex.IsMatch(Regex.Replace(v, "[^0-9]", ""), @"^-?\d+(\.\d+)?$"); } // Anything that is entirely numeric in all likelihood is a Sky or Model Number private static bool IsNumeric(string v) { return (System.Text.RegularExpressions.Regex.IsMatch(v, "^[-0-9]*.[.0-9].[0-9]*$")); } } public class ListEx : List { // Count the number of times an item appears in this sorted list public int BinarySearchCountAll(T searchValue) { int ret; // search for first time int center = this.BinarySearch(searchValue); // no results... if (center < 0) { ret = 0; } else { int left = center; while (left > 0 && this[left - 1].Equals(searchValue)) { left -= 1; } int right = center; while (right < (this.Count - 1) && this[right + 1].Equals(searchValue)) { right += 1; } ret = (right - left) + 1; } return ret; } }