/** * <summary>Replace words not in given dictionary. * Deletes unknown words from children nodes and adds them to {@link NGramNode#unknown} unknown node as children recursively.</summary> * * <param name="dictionary">dictionary of known words.</param> */ public void ReplaceUnknownWords(HashSet <TSymbol> dictionary) { if (_children != null) { var childList = new List <NGramNode <TSymbol> >(); foreach (var s in _children.Keys) { if (!dictionary.Contains(s)) { childList.Add(_children[s]); } } if (childList.Count > 0) { _unknown = new NGramNode <TSymbol>(default(TSymbol)); _unknown._children = new Dictionary <TSymbol, NGramNode <TSymbol> >(); var sum = 0; foreach (var child in childList) { if (child._children != null) { foreach (var(key, value) in child._children) { _unknown._children.Add(key, value); } } sum += child._count; _children.Remove(child._symbol); } _unknown._count = sum; _unknown.ReplaceUnknownWords(dictionary); } foreach (var child in _children.Values) { child.ReplaceUnknownWords(dictionary); } } }
/** * <summary>Replaces words not in {@link HashSet} given dictionary.</summary> * * <param name="dictionary">dictionary of known words.</param> */ public void ReplaceUnknownWords(HashSet <TSymbol> dictionary) { rootNode.ReplaceUnknownWords(dictionary); }