/// <summary> /// Applies the affix rule to the given word, producing a list of stems if any are found. /// </summary> /// <param name="strippedWord">Word the affix has been removed and the strip added.</param> /// <param name="affix">HunspellAffix representing the affix rule itself.</param> /// <param name="recursionDepth">Level of recursion this stemming step is at.</param> /// <returns>List of stems for the word, or an empty list if none are found.</returns> public IEnumerable<HunspellStem> ApplyAffix(String strippedWord, HunspellAffix affix, Int32 recursionDepth) { if (strippedWord == null) throw new ArgumentNullException("strippedWord"); if (affix == null) throw new ArgumentNullException("affix"); if (!affix.CheckCondition(strippedWord)) { return new List<HunspellStem>(); } var words = _dictionary.LookupWord(strippedWord); if (words == null) { return new List<HunspellStem>(); } var stems = new List<HunspellStem>(); foreach (var hunspellWord in words) { if (hunspellWord.HasFlag(affix.Flag)) { if (affix.IsCrossProduct && recursionDepth < RECURSION_CAP) { var recursiveStems = Stem(strippedWord, affix.AppendFlags, ++recursionDepth); if (recursiveStems.Any()) { stems.AddRange(recursiveStems); } else { stems.Add(new HunspellStem(strippedWord)); } } else { stems.Add(new HunspellStem(strippedWord)); } } } return stems; }
/// <summary> /// Applies the affix rule to the given word, producing a list of stems if any are found. /// </summary> /// <param name="strippedWord">Word the affix has been removed and the strip added.</param> /// <param name="affix">HunspellAffix representing the affix rule itself.</param> /// <param name="recursionDepth">Level of recursion this stemming step is at.</param> /// <returns>List of stems for the word, or an empty list if none are found.</returns> public IEnumerable <HunspellStem> ApplyAffix(String strippedWord, HunspellAffix affix, Int32 recursionDepth) { if (strippedWord == null) { throw new ArgumentNullException("strippedWord"); } if (affix == null) { throw new ArgumentNullException("affix"); } if (!affix.CheckCondition(strippedWord)) { return(new List <HunspellStem>()); } var words = _dictionary.LookupWord(strippedWord); if (words == null) { return(new List <HunspellStem>()); } var stems = new List <HunspellStem>(); foreach (var hunspellWord in words) { if (hunspellWord.HasFlag(affix.Flag)) { if (affix.IsCrossProduct && recursionDepth < RECURSION_CAP) { var recursiveStems = Stem(strippedWord, affix.AppendFlags, ++recursionDepth); if (recursiveStems.Any()) { stems.AddRange(recursiveStems); } else { stems.Add(new HunspellStem(strippedWord)); } } else { stems.Add(new HunspellStem(strippedWord)); } } } return(stems); }
/// <summary> /// Adds a suffix to the list of suffixes used to generate this stem. Because it /// is assumed that suffixes are added depth first, the suffix is added to the end /// of the list. /// </summary> /// <param name="suffix">Suffix to add to the list of suffixes for this stem.</param> public void AddSuffix(HunspellAffix suffix) { _suffixes.Add(suffix); }
/// <summary> /// Adds a prefix to the list of prefixes used to generate this stem. Because it is /// assumed that prefixes are added depth first, the prefix is added to the front of /// the list. /// </summary> /// <param name="prefix">Prefix to add to the list of prefixes for this stem.</param> public void AddPrefix(HunspellAffix prefix) { _prefixes.Insert(0, prefix); }
/// <summary> /// Parses a specific affix rule putting the result into the provided affix map. /// </summary> /// <param name="affixes">Map where the result of the parsing will be put.</param> /// <param name="header">Header line of the affix rule.</param> /// <param name="reader">TextReader to read the content of the rule from.</param> /// <param name="conditionPattern">Pattern to be used to generate the condition regex pattern.</param> private void ParseAffix(Dictionary<String, List<HunspellAffix>> affixes, String header, TextReader reader, String conditionPattern) { if (affixes == null) throw new ArgumentNullException("affixes"); if (header == null) throw new ArgumentNullException("header"); if (reader == null) throw new ArgumentNullException("reader"); if (conditionPattern == null) throw new ArgumentNullException("conditionPattern"); var args = Regex.Split(header, "\\s+"); var crossProduct = args[2].Equals("Y"); var numLines = Int32.Parse(args[3]); for (var i = 0; i < numLines; i++) { var line = reader.ReadLine(); var ruleArgs = Regex.Split(line, "\\s+"); var affix = new HunspellAffix(); affix.Flag = _flagParsingStrategy.ParseFlag(ruleArgs[1]); affix.Strip = (ruleArgs[2] == "0") ? "" : ruleArgs[2]; var affixArg = ruleArgs[3]; var flagSep = affixArg.LastIndexOf('/'); if (flagSep != -1) { var cflag = affixArg.Substring(flagSep + 1); var appendFlags = _alias.Count > 0 ? _alias[int.Parse(cflag) - 1] : _flagParsingStrategy.ParseFlags(cflag); Array.Sort(appendFlags); affix.AppendFlags = appendFlags; affix.Append = affixArg.Substring(0, flagSep); } else { affix.Append = affixArg; } var condition = ruleArgs[4]; affix.SetCondition(condition, String.Format(conditionPattern, condition)); affix.IsCrossProduct = crossProduct; List<HunspellAffix> list; if (!affixes.TryGetValue(affix.Append, out list)) affixes.Add(affix.Append, list = new List<HunspellAffix>()); list.Add(affix); } }
/// <summary> /// Parses a specific affix rule putting the result into the provided affix map. /// </summary> /// <param name="affixes">Map where the result of the parsing will be put.</param> /// <param name="header">Header line of the affix rule.</param> /// <param name="reader">TextReader to read the content of the rule from.</param> /// <param name="conditionPattern">Pattern to be used to generate the condition regex pattern.</param> private void ParseAffix(Dictionary <String, List <HunspellAffix> > affixes, String header, TextReader reader, String conditionPattern) { if (affixes == null) { throw new ArgumentNullException("affixes"); } if (header == null) { throw new ArgumentNullException("header"); } if (reader == null) { throw new ArgumentNullException("reader"); } if (conditionPattern == null) { throw new ArgumentNullException("conditionPattern"); } var args = Regex.Split(header, "\\s+"); var crossProduct = args[2].Equals("Y"); var numLines = Int32.Parse(args[3]); var hasAliases = _aliases.Count > 0; for (var i = 0; i < numLines; i++) { var line = reader.ReadLine(); var ruleArgs = Regex.Split(line, "\\s+"); var affix = new HunspellAffix(); affix.Flag = _flagParsingStrategy.ParseFlag(ruleArgs[1]); affix.Strip = (ruleArgs[2] == "0") ? "" : ruleArgs[2]; var affixArg = ruleArgs[3]; var flagSep = affixArg.LastIndexOf('/'); if (flagSep != -1) { var cflag = affixArg.Substring(flagSep + 1); var appendFlags = hasAliases ? _aliases[cflag] : _flagParsingStrategy.ParseFlags(cflag); Array.Sort(appendFlags); affix.AppendFlags = appendFlags; affix.Append = affixArg.Substring(0, flagSep); } else { affix.Append = affixArg; } var condition = ruleArgs[4]; affix.SetCondition(condition, String.Format(conditionPattern, condition)); affix.IsCrossProduct = crossProduct; List <HunspellAffix> list; if (!affixes.TryGetValue(affix.Append, out list)) { affixes.Add(affix.Append, list = new List <HunspellAffix>()); } list.Add(affix); } }