int TStrUtil::CountWords(const TChA& ChA, const TStrHash<TInt>& StopWordH) { TChA Tmp; TVec<char *> WrdV; SplitWords(Tmp, WrdV); int SWordCnt = 0; for (int w = 0; w < WrdV.Len(); w++) { if (StopWordH.IsKey(WrdV[w])) { SWordCnt++; } } return WrdV.Len() - SWordCnt; }
// Loads a (directed, undirected or multi) graph from a text file InFNm with 1 node and all its edges in a single line. void IOConnListStr() { const int NNodes = 500; const int NEdges = 2000; const char *FName = "demo.graph.dat"; PUNGraph GOut, GIn; GOut = GenRndGnm<PUNGraph>(NNodes, NEdges); // Output nodes as random strings TIntStrH OutNIdStrH; TStrHash<TInt> OutStrNIdH; // Generate unique random strings for graph for (TUNGraph::TNodeI NI = GOut->BegNI(); NI < GOut->EndNI(); NI++) { TStr RandStr = ""; do { TInt RandLen = TInt::Rnd.GetUniDevInt(5, 10); for (int i = 0; i < RandLen; i++) { // TStr RandChar(TInt::Rnd.GetUniDevInt(33, 126)); TStr RandChar(TInt::Rnd.GetUniDevInt(97, 122)); RandStr += RandChar; } } while (OutStrNIdH.IsKey(RandStr) || RandStr[0] == '#'); OutNIdStrH.AddDat(NI.GetId(), RandStr); OutStrNIdH.AddDat(RandStr, NI.GetId()); } // Create graph file FILE *F = fopen(FName, "w"); for (TUNGraph::TNodeI NI = GOut->BegNI(); NI < GOut->EndNI(); NI++) { fprintf(F, "%s", OutNIdStrH[NI.GetId()].CStr()); for (int e = 0; e < NI.GetOutDeg(); e++) { fprintf(F, " %s", OutNIdStrH[NI.GetOutNId(e)].CStr()); } fprintf(F, "\n"); } fclose(F); TStrHash<TInt> InStrToNIdH; GIn = LoadConnListStr<PUNGraph>(FName, InStrToNIdH); PrintGStats("ConnListStr - Out", GOut); PrintGStats("ConnListStr - In", GIn); }
// Save and load directed, undirected and multi-graphs, where node names are strings void IOEdgeListStr() { const int NNodes = 1000; const int NEdges = 5000; const char *FName = "demo.graph.dat"; PNEGraph GOut, GIn; // Can also be PUNGraph or PNGraph GOut = GenRndGnm<PNEGraph>(NNodes, NEdges); // Output nodes as random strings TIntStrH OutNIdStrH; TStrHash<TInt> OutStrNIdH; // Generate unique random strings for graph TStr RandStr; for (TNEGraph::TNodeI NI = GOut->BegNI(); NI < GOut->EndNI(); NI++) { do { RandStr.Clr(); TInt RandLen = TInt::Rnd.GetUniDevInt(5, 30); for (int i = 0; i < RandLen; i++) { TStr RandChar(TInt::Rnd.GetUniDevInt(33, 126)); RandStr += RandChar; } } while (OutStrNIdH.IsKey(RandStr) || RandStr[0] == '#'); // Not unique or starts with comment OutNIdStrH.AddDat(NI.GetId(), RandStr); OutStrNIdH.AddDat(RandStr, NI.GetId()); } // Create graph file FILE *F = fopen(FName, "w"); for (TNEGraph::TEdgeI EI = GOut->BegEI(); EI < GOut->EndEI(); EI++) { TInt Src = EI.GetSrcNId(); TInt Dst = EI.GetDstNId(); fprintf(F, "%s %s\n", OutNIdStrH[Src].CStr(), OutNIdStrH[Dst].CStr()); } fclose(F); // Load edge list of strings TStrHash<TInt> InStrToNIdH; GIn = LoadEdgeListStr<PNEGraph>(FName, 0, 1, InStrToNIdH); PrintGStats<PNEGraph>("EdgeListStr - Out", GOut); PrintGStats<PNEGraph>("EdgeListStr - In", GIn); }