public void AddVertexToCommunity(EEVertex eev, EvalVal eval, double pScore) { if (!eval.memberable) { return; } if (!MemberNodes.ContainsKey(eev.Entity)) { MemberNodes.Add(eev.Entity, eev.Neighbors.Count); _pScoreSum += pScore; } this._outlinks += (uint)eval.linksOutCommunity; this._volume += (uint)eev.Neighbors.Count; if (OutlinkedNodes.ContainsKey(eev.Entity)) { OutlinkedNodes.Remove(eev.Entity); } foreach (string nbor in eev.Neighbors.Keys) { string[] kws = eev.Neighbors[nbor].kw.Split(",".ToCharArray()); foreach (string kw in kws) { if (kw != "" && kw != " ") { if (!TrappedKeywords.ContainsKey(kw)) { TrappedKeywords.Add(kw, 0); } TrappedKeywords[kw]++; } } if (!isMember(nbor)) { if (!OutlinkedNodes.ContainsKey(nbor)) { OutlinkedNodes.Add(nbor, 1 /*eev.Neighbors[nbor].Weight*/); } } } }
public EvalVal EvalEEVertex(EEVertex eev) { EvalVal eval = new EvalVal(); eval.linksOutCommunity = 0; eval.linksToCommunity = 0; foreach (string nbor in eev.Neighbors.Keys) { if (MemberNodes.ContainsKey(nbor)) { eval.linksToCommunity++; } else { eval.linksOutCommunity++; } } eval.cond = ((double)this.OutlinksCount + eval.linksOutCommunity - eval.linksToCommunity) / (this.Volume + eev.Neighbors.Count); if (eval.cond <= this.Conductance) { eval.memberable = true; } else if (eval.cond - CommCondThres < this.Conductance) { eval.memberable = true; } else { eval.memberable = false; } return(eval); }
public void Communitize(string filepath, double comThres = 0.01, int MinNeighborForSeed = 2, string forcedSeed = "") { DateTime startTime = DateTime.Now; Dictionary <string, int> AllEntity = new Dictionary <string, int>(StringComparer.InvariantCultureIgnoreCase); foreach (string entity in EkwEgg.Keys) { if (EkwEgg[entity].Neighbors.Count >= MinNeighborForSeed) { if (!AllEntity.ContainsKey(entity)) { AllEntity.Add(entity, EkwEgg[entity].Neighbors.Count); } } } bool keepRunning = true; string seed = forcedSeed; Console.WriteLine("building communities"); Console.WriteLine("SignificantEntity.Count: " + AllEntity.Count); while (keepRunning) { if (AllEntity.Count < 1) { keepRunning = false; continue; } Community aCommunity = new Community(); aCommunity.CommCondThres = comThres; //seed it if (seed == "" || !AllEntity.ContainsKey(seed)) { seed = AllEntity.Keys.First(); } while (EkwEgg[seed].Neighbors.Count < MinNeighborForSeed) { AllEntity.Remove(seed); seed = AllEntity.Keys.First(); Console.Write("\r" + AllEntity.Count); } Console.WriteLine("\t EntityRemain : " + AllEntity.Count); bool keepBuildingThisComnty = true; string entity = seed; while (keepBuildingThisComnty) { EEVertex eev = EkwEgg[entity]; Community.EvalVal eval = aCommunity.EvalEEVertex(eev); if (eval.memberable) { aCommunity.AddVertexToCommunity(eev, eval, EkwDict[entity].PScore); AllEntity.Remove(entity); Console.Write("\r" + AllEntity.Count); } else if (aCommunity.OutlinkedNodes.Count < 1) { keepBuildingThisComnty = false; continue; } else { //this is not going to be a potential member aCommunity.OutlinkedNodes.Remove(entity); } if (aCommunity.OutlinkedNodes.Count > 0) { entity = aCommunity.OutlinkedNodes.Keys.First(); } else { keepBuildingThisComnty = false; continue; } } if (aCommunity.Size > 0) { aCommunity.Consolidate(); Communities.Add(aCommunity); } Console.Write("\tCommunity size:" + aCommunity.MemberNodes.Count); seed = entity; } Console.WriteLine("time to find communitieis: " + (TimeSpan)(DateTime.Now - startTime)); int largesComntySz = 0; StreamWriter fs = new StreamWriter(filepath); fs.WriteLine("<Communities>"); int i = 1; foreach (Community comnty in Communities) { //int kwThreshhold = comnty; fs.WriteLine(" <Community id='" + i++ + "' size='" + comnty.Size + "' conductance='" + comnty.Conductance + "' pScore='" + comnty.pScore + "'>"); fs.WriteLine(" <trapped-keywords count='" + comnty.TrappedKeywords.Count + "'>"); fs.Write(" "); foreach (string kw in comnty.TrappedKeywords.Keys) { fs.Write(System.Net.WebUtility.HtmlEncode(kw) + ":" + comnty.TrappedKeywords[kw] + ","); } fs.WriteLine("\n </trapped-keywords>"); foreach (string member in comnty.MemberNodes.Keys) { fs.WriteLine(" <e>" + System.Net.WebUtility.HtmlEncode(member) + "</e>"); } fs.WriteLine(" </Community>"); largesComntySz = (largesComntySz > comnty.Size) ? largesComntySz : comnty.Size; } fs.WriteLine("</Communities>"); fs.Close(); Console.WriteLine("\n\nCommunities.Count: " + Communities.Count); Console.WriteLine("Largest Communities Size: " + largesComntySz); XmlDocument xDoc = new XmlDocument(); xDoc.Load(filepath); xDoc.Normalize(); xDoc.Save(filepath); }