19 using System.Collections.Generic;
23 namespace Lucene.Net.Analysis.Hunspell {
31 private static Int32 RECURSION_CAP = 2;
40 if (dictionary == null)
throw new ArgumentNullException(
"dictionary");
41 _dictionary = dictionary;
49 public IEnumerable<HunspellStem>
Stem(String word) {
50 if (word == null)
throw new ArgumentNullException(
"word");
52 var stems =
new List<HunspellStem>();
53 if (_dictionary.LookupWord(word) != null)
56 stems.AddRange(Stem(word, null, 0));
66 if (word == null)
throw new ArgumentNullException(
"word");
68 var stems =
new List<HunspellStem>();
70 if (_dictionary.LookupWord(word) != null) {
75 var otherStems = Stem(word, null, 0);
76 foreach (var s
in otherStems) {
77 if (!terms.Contains(s.Stem)) {
93 private IEnumerable<HunspellStem> Stem(String word, Char[] flags, Int32 recursionDepth) {
94 if (word == null)
throw new ArgumentNullException(
"word");
96 var stems =
new List<HunspellStem>();
97 var chars = word.ToCharArray();
98 var length = word.Length;
100 for (var i = 0; i < length; i++) {
101 var suffixes = _dictionary.LookupSuffix(chars, i, length - i);
102 if (suffixes != null) {
103 foreach (var suffix
in suffixes) {
104 if (HasCrossCheckedFlag(suffix.Flag, flags)) {
105 var deAffixedLength = length - suffix.Append.Length;
108 var strippedWord =
new StringBuilder()
109 .Append(word, 0, deAffixedLength)
110 .Append(suffix.Strip)
113 var stemList = ApplyAffix(strippedWord, suffix, recursionDepth);
114 foreach (var stem
in stemList) {
115 stem.AddSuffix(suffix);
118 stems.AddRange(stemList);
124 for (var i = length - 1; i >= 0; i--) {
125 var prefixes = _dictionary.LookupPrefix(chars, 0, i);
126 if (prefixes != null) {
127 foreach (var prefix
in prefixes) {
128 if (HasCrossCheckedFlag(prefix.Flag, flags)) {
129 var deAffixedStart = prefix.Append.Length;
130 var deAffixedLength = length - deAffixedStart;
132 var strippedWord =
new StringBuilder()
133 .Append(prefix.Strip)
134 .Append(word, deAffixedStart, deAffixedLength)
137 var stemList = ApplyAffix(strippedWord, prefix, recursionDepth);
138 foreach (var stem
in stemList) {
139 stem.AddPrefix(prefix);
142 stems.AddRange(stemList);
159 if (strippedWord == null)
throw new ArgumentNullException(
"strippedWord");
160 if (affix == null)
throw new ArgumentNullException(
"affix");
163 return new List<HunspellStem>();
166 var words = _dictionary.LookupWord(strippedWord);
168 return new List<HunspellStem>();
171 var stems =
new List<HunspellStem>();
173 foreach (var hunspellWord
in words) {
174 if (hunspellWord.HasFlag(affix.Flag)) {
175 if (affix.IsCrossProduct && recursionDepth < RECURSION_CAP) {
176 var recursiveStems = Stem(strippedWord, affix.AppendFlags, ++recursionDepth);
177 if (recursiveStems.Any()) {
178 stems.AddRange(recursiveStems);
197 private static Boolean HasCrossCheckedFlag(Char flag, Char[] flags) {
198 return flags == null || Array.BinarySearch(flags, flag) >= 0;