/// <summary> /// Parses spell-checking results /// </summary> /// <param name="node"></param> /// <returns></returns> public SpellCheckResults ParseSpellChecking(XElement node) { var r = new SpellCheckResults(); var suggestionsNode = node.XPathSelectElement("lst[@name='suggestions']"); var collationNode = suggestionsNode.XPathSelectElement("str[@name='collation']"); if (collationNode != null) { r.Collation = collationNode.Value; } var spellChecks = suggestionsNode.Elements("lst"); foreach (var c in spellChecks) { var result = new SpellCheckResult(); result.Query = c.Attribute("name").Value; result.NumFound = Convert.ToInt32(c.XPathSelectElement("int[@name='numFound']").Value); result.EndOffset = Convert.ToInt32(c.XPathSelectElement("int[@name='endOffset']").Value); result.StartOffset = Convert.ToInt32(c.XPathSelectElement("int[@name='startOffset']").Value); var suggestions = new List <string>(); var suggestionNodes = c.XPathSelectElements("arr[@name='suggestion']/str"); foreach (var suggestionNode in suggestionNodes) { suggestions.Add(suggestionNode.Value); } result.Suggestions = suggestions; r.Add(result); } return(r); }
private static async Task RunSpellingAsync(List <string> allText, ISpellChecker spellChecker, IReporter reporter) { int total = allText.Count; int current = 0; int lastPercent = 0; foreach (var message in allText) { SpellCheckResult spellCheckResult = null; try { spellCheckResult = await spellChecker.CheckSpellingAsync(message); } catch (Exception e) { Console.WriteLine(e.Message); } if (spellCheckResult != null && spellCheckResult.suggestions != null && spellCheckResult.suggestions.Any()) { foreach (var spellSuggest in spellCheckResult.suggestions) { var reportText = ReportSpelling($"Word: '{spellSuggest.original}' - did you mean: '{spellSuggest.suggestion}'? Character number: {spellSuggest.wordLocation}", message); reporter.ReportMessage(reportText); Console.WriteLine(reportText); } } current++; var percent = (int)((double)current / total * 100.0); if (percent > lastPercent) { lastPercent = percent; Console.WriteLine($"{percent}% Spell Checked"); } } }
public ActionResult Index() { var searchText = Request.QueryString["text"] ?? string.Empty; if (string.IsNullOrEmpty(searchText)) { return(View()); } //var queryOptions = new QueryOptions //{ // SpellCheck = new SpellCheckingParameters() // { // Query = searchText // } //}; var queryOptions = new SpellCheckHandlerQueryOptions() { SpellCheck = new SpellCheckingParameters() { Query = searchText } }; var indexName = $"sitecore_{Sitecore.Context.Database.Name}_index"; var index = ContentSearchManager.GetIndex(indexName); SpellCheckResult spellCheckResult = new SpellCheckResult(); using (var context = index.CreateSearchContext()) { //var results = context.Query<SearchResultItem>($"_name:{searchText}", // queryOptions); var results = context.GetSpellCheck(new SolrQuery($"_name:{searchText}"), queryOptions); if (results?.SpellChecking == null || results.SpellChecking.Count < 1) { return(View()); } var suggestions = new List <string>(); foreach (var term in results.SpellChecking) { foreach (var suggestion in term.Suggestions) { suggestions.Add(suggestion); } } spellCheckResult.SearchText = searchText; spellCheckResult.Suggestions = suggestions; } return(View(spellCheckResult)); }
public static void WarmUp() { for (var i = 0; i < 10; i++) { CheckString("According to all known laws of aviation, there is no way a bee should be able to fly."); CheckString( "Accorfding toa alal knfown lawzs owf aviatiaon, theare izs nno wway aa baee shouald bae ablae."); var test = CheckString("Thiz! uwu iz sum? texxt tu vwarm upp da spiell chkear und makeed ita gud. :)"); var spellCheckResult = new SpellCheckResult(new Control(), 8, "bepis"); CheckBlocks(new Array <Control>()); } }
// Checks the TextEdits of the provided set of Dialogue Blocks for spelling errors. // Returns a Dictionary in the form {id : {index : word}} public static Array <SpellCheckResult> CheckBlocks(Array <Control> blocks) { var outputArr = new Array <SpellCheckResult>(); foreach (var block in blocks) { var textEdit = (TextEdit)block.Get("dialogue_line_edit"); var spellingErrors = CheckString(textEdit.Text); if (spellingErrors.Count > 0) { foreach (var indexWordPair in spellingErrors) { var spellCheckResult = new SpellCheckResult(block, indexWordPair.Key, indexWordPair.Value); outputArr.Add(spellCheckResult); } } } return(outputArr); }
/// <summary> /// Parses spell-checking results /// </summary> /// <param name="node"></param> /// <returns></returns> public SpellCheckResults ParseSpellChecking(XElement node) { var r = new SpellCheckResults(); var suggestionsNode = node.XPathSelectElement("lst[@name='suggestions']"); r.Collations = CollectionNodeChecker(node); var spellChecks = suggestionsNode.Elements("lst"); foreach (var c in spellChecks) { var result = new SpellCheckResult(); result.Query = c.Attribute("name") == null ? null : c.Attribute("name").Value; result.NumFound = Convert.ToInt32(c.XPathSelectElement("int[@name='numFound']").Value); result.EndOffset = Convert.ToInt32(c.XPathSelectElement("int[@name='endOffset']").Value); result.StartOffset = Convert.ToInt32(c.XPathSelectElement("int[@name='startOffset']").Value); var suggestionNodes = c.XPathSelectElements("arr[@name='suggestion']/str"); result.Suggestions = suggestionNodes.Select(suggestionNode => suggestionNode.Value).ToList(); r.Add(result); } return(r); }
// Checks the TextEdits of the provided set of Dialogue Blocks for spelling errors. // Returns a Dictionary in the form {id : {index : word}} public static Array <SpellCheckResult> CheckBlocks(Array <Control> blocks) { var outputArr = new Array <SpellCheckResult>(); foreach (var block in blocks) { var textEdit = (TextEdit)block.Get("dialogue_line_edit"); var spellingErrors = CheckString(textEdit.Text); if (spellingErrors.Count > 0) { foreach (var indexWordPair in spellingErrors) { var spellCheckResult = new SpellCheckResult(block, indexWordPair.Key, indexWordPair.Value); // Add if block id isn't ignored by project settings if (!_ignoredWordsProject.ContainsKey('"' + block.Name + '"')) { outputArr.Add(spellCheckResult); } } } } return(outputArr); }
/// <summary> /// Parses spell-checking results /// </summary> /// <param name="node">SpellCheck node</param> /// <returns>List of suggestions and collations</returns> public SpellCheckResults ParseSpellChecking(XElement node) { var r = new SpellCheckResults(); var suggestionsNode = node.XPathSelectElement("lst[@name='suggestions']"); var collationNode = suggestionsNode.XPathSelectElement("str[@name='collation']"); if (collationNode != null) { r.Collation = collationNode.Value; } IEnumerable <XElement> collationNodes; var collationsNode = node.XPathSelectElement("lst[@name='collations']"); if (collationsNode != null) { // Solr 5.0+ collationNodes = collationsNode.XPathSelectElements("lst[@name='collation']"); } else { // Solr 4.x and lower collationNodes = suggestionsNode.XPathSelectElements("lst[@name='collation']"); } foreach (var cn in collationNodes) { if (cn.XPathSelectElement("str[@name='collationQuery']") != null) { r.Collations.Add(cn.XPathSelectElement("str[@name='collationQuery']").Value); if (string.IsNullOrEmpty(r.Collation)) { r.Collation = cn.XPathSelectElement("str[@name='collationQuery']").Value; } } } var spellChecks = suggestionsNode.Elements("lst"); foreach (var c in spellChecks) { if (c.Attribute("name").Value != "collation" || c.XPathSelectElement("int[@name='numFound']") != null) { //Spelling suggestions are added, required to check if 'collation' is a search term or indicates collation node var result = new SpellCheckResult(); result.Query = c.Attribute("name").Value; result.NumFound = Convert.ToInt32(c.XPathSelectElement("int[@name='numFound']").Value); result.EndOffset = Convert.ToInt32(c.XPathSelectElement("int[@name='endOffset']").Value); result.StartOffset = Convert.ToInt32(c.XPathSelectElement("int[@name='startOffset']").Value); var suggestions = new List <string>(); var suggestionNodes = c.XPathSelectElements("arr[@name='suggestion']/str"); foreach (var suggestionNode in suggestionNodes) { suggestions.Add(suggestionNode.Value); } result.Suggestions = suggestions; r.Add(result); } } return(r); }
/// <summary> /// Parses spell-checking results /// </summary> /// <param name="node">SpellCheck node</param> /// <returns>List of suggestions and collations</returns> public SpellCheckResults ParseSpellChecking(XElement node) { var r = new SpellCheckResults(); var suggestionsNode = node.XPathSelectElement("lst[@name='suggestions']"); IEnumerable <XElement> collationNodes; var collationsNode = node.XPathSelectElement("lst[@name='collations']"); if (collationsNode != null) { // Solr 5.0+ collationNodes = collationsNode.XPathSelectElements("lst[@name='collation']") .Union(collationsNode.XPathSelectElements("str[@name='collation']")); } else { // Solr 4.x and lower collationNodes = suggestionsNode.XPathSelectElements("lst[@name='collation']") .Union(suggestionsNode.XPathSelectElements("str[@name='collation']")); } CollationResult tempCollation; foreach (var cn in collationNodes) { //If it does not contain collationQuery element, it is a suggestion if (cn.XPathSelectElement("str[@name='collationQuery']") != null) { tempCollation = new CollationResult(); tempCollation.CollationQuery = cn.XPathSelectElement("str[@name='collationQuery']").Value; if (cn.XPathSelectElement("long[@name='hits']") != null) { tempCollation.Hits = Convert.ToInt64(cn.XPathSelectElement("long[@name='hits']").Value); } else if (cn.XPathSelectElement("int[@name='hits']") != null) { tempCollation.Hits = Convert.ToInt32(cn.XPathSelectElement("int[@name='hits']").Value); } //Selects the mispellings and corrections var correctionNodes = cn.XPathSelectElements("lst[@name='misspellingsAndCorrections']"); foreach (var mc in correctionNodes.Elements()) { tempCollation.MisspellingsAndCorrections.Add(new KeyValuePair <string, string>(mc.Attribute("name").Value, mc.Value)); } r.Collations.Add(tempCollation); } else if (cn.Name.LocalName.Equals("str")) { tempCollation = new CollationResult(); tempCollation.CollationQuery = cn.Value; tempCollation.Hits = -1; r.Collations.Add(tempCollation); } } var spellChecks = suggestionsNode.Elements("lst"); foreach (var c in spellChecks) { if (c.Attribute("name").Value != "collation" || c.XPathSelectElement("int[@name='numFound']") != null) { //Spelling suggestions are added, required to check if 'collation' is a search term or indicates collation node var result = new SpellCheckResult(); result.Query = c.Attribute("name").Value; result.NumFound = Convert.ToInt32(c.XPathSelectElement("int[@name='numFound']").Value); result.EndOffset = Convert.ToInt32(c.XPathSelectElement("int[@name='endOffset']").Value); result.StartOffset = Convert.ToInt32(c.XPathSelectElement("int[@name='startOffset']").Value); var suggestions = new List <string>(); var suggestionArray = c.XPathSelectElements("arr[@name='suggestion']"); var suggestionNodes = suggestionArray.Descendants("str"); foreach (var suggestionNode in suggestionNodes) { suggestions.Add(suggestionNode.Value); } result.Suggestions = suggestions; r.Add(result); } } return(r); }
internal Suggestion(SpellCheckResult suggestion, string prefix) : this(suggestion.Query, suggestion.StartOffset, suggestion.EndOffset, suggestion.Suggestions.ToArray(), prefix) { }
internal Suggestion(SpellCheckResult suggestion) : this(suggestion.Query, suggestion.StartOffset, suggestion.EndOffset, suggestion.Suggestions.ToArray(), "") { }