/// <summary>
        /// Checks the spelling of the specified text
        /// </summary>
        /// <param name="affFile">The affFile path</param>
        /// <param name="dicFile">The dictionary file path</param>
        /// <param name="text">The text to spell check</param>
        /// <returns></returns>
        public static string CheckSpelling(string affFile, string dicFile, string text)
        {
            bool success = true;
            List<string> misspelledWords = new List<string>();
            List<List<string>> data = new List<List<string>>();

            try
            {
                using (Hunspell hunspell = new Hunspell(affFile, dicFile))
                {
                    var words = text.Split((char[])null);

                    //check the spelling of each word
                    foreach (var word in words)
                    {
                        if (!hunspell.Spell(word))
                            misspelledWords.Add(word);
                    }

                    data.Add(misspelledWords);
                }
            }
            catch (Exception exception)
            {
                //need to add logging
                success = false;
            }

            var results = new SpellCheckResults
            {
                Outcome = success ? "success" : "error",
                Data = success ? data : null
            };

            return JsonConvert.SerializeObject(results);
        }
Exemple #2
0
        /// <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);
        }
Exemple #3
0
        /// <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);
        }
 internal SuggestionCollection(SpellCheckResults results)
     : this(results, "")
 {
 }