/// <summary> /// Creates a permutation of words using all components in this word structure. /// </summary> /// <returns></returns> private IList <Word> PermutateAllComponents() { var words = new List <Word>(); // if only one component exists, we'll just add it's characters to the word list. // otherwise, we'll loop through the components and permutate their characters. if (Components.Count == 1) { words.AddRange(Components.First().Words); } else { WordStructureComponent currentComponent = null; foreach (WordStructureComponent nextComponent in Components) { if (currentComponent == null) { currentComponent = nextComponent; continue; } else { var list = currentComponent.Permutate(nextComponent); currentComponent = new WordStructureComponent(list.ToArray()); words.AddRange(list); } } // remove words that have less characters than the number of components. words.RemoveAll(w => w.Sounds.Count < Components.Count); } return(words); }
/// <summary> /// Generates all subset <see cref="WordStructure"/> combinations of the <see cref="WordStructure"/> passed in. /// </summary> /// <param name="instr"></param> /// <param name="outstr"></param> private void GetCombinations(WordStructure instr, List <WordStructure> outstr) { var comps = instr.Components.ToList(); for (int i = 0; i < comps.Count(); i++) { WordStructureComponent wsc = instr.Components[i]; if (wsc.IsOptional) { comps.RemoveAt(i); var str = new WordStructure(comps.ToArray()); outstr.Add(str); GetCombinations(str, outstr); comps.Insert(i, wsc); } } }
/// <summary> /// Parses a string into a <see cref="WordStructure"/> object. /// </summary> /// <param name="str"></param> /// <returns></returns> public static WordStructure Parse(string str) { if (string.IsNullOrEmpty(str)) { throw new ArgumentNullException("str"); } WordStructure structure = new WordStructure(); bool isOptional = false; for (int i = 0; i < str.Length; i++) { var character = str.ElementAt(i); // check for special characters. switch (character) { case ParserSymbols.OPTIONAL_START: isOptional = true; continue; case ParserSymbols.OPTIONAL_END: isOptional = false; continue; } // build the component. WordStructureComponent component = new WordStructureComponent { IsOptional = isOptional, Symbol = character }; structure.Components.Add(component); } return(structure); }