// </DictLeaf> // building new tree with given words private DictLeaf BuildTree(List <string> words) { DictLeaf toRet = new DictLeaf(' '); foreach (string word in words) { toRet.InsertValue(' ', word); } return(toRet); }
// build big model by splitting data, building small ones and summing them up private DictLeaf BuildRootOfLargeTree(List <List <string> > wordsChunks) { int amount = wordsChunks.Count; List <DictLeaf> dictsSet = new List <DictLeaf>(); for (int i = 0; i < amount; i++) { dictsSet.Add(Task.Run(() => BuildTree(wordsChunks[i])).Result); } DictLeaf toRet = SummarizeLeafs(dictsSet); return(toRet); }//*/
// summarize lists of leafs private DictLeaf SummarizeLeafs(List <DictLeaf> leafs) { char currChar = leafs[0].GetRoot(); DictLeaf toRet = new DictLeaf(currChar); Dictionary <char, List <DictLeaf> > wtf = new Dictionary <char, List <DictLeaf> >(); foreach (var item in leafs) { if (item.GetRoot() != currChar) { throw new Exception(); // todo } toRet.Amount += item.Amount; foreach (var dictItem in item.Leafs) { if (wtf.TryGetValue(dictItem.Key, out var lol)) { wtf[dictItem.Key].Add(dictItem.Value); } else { wtf.Add(dictItem.Key, new List <DictLeaf>() { dictItem.Value }); } } } Dictionary <char, DictLeaf> toSetAsRoot = new Dictionary <char, DictLeaf>(); foreach (var item in wtf) { toSetAsRoot.Add(item.Key, SummarizeLeafs(item.Value)); } toRet.Leafs = toSetAsRoot; return(toRet); }
}//*/ // setting builded model to root public void BuildLargeTree(List <List <string> > wordsChunks) { Root = BuildRootOfLargeTree(wordsChunks); }