TEST(TStr, ChangeChAll) { TStr Str = "abac"; TStr Empty; int Changes = 0; Changes = Empty.ChangeChAll('a', 'c'); EXPECT_EQ(Empty, ""); EXPECT_EQ(Changes, 0); Changes = Str.ChangeChAll('a', 'c'); EXPECT_EQ(Str, "cbcc"); EXPECT_EQ(Changes, 2); }
// <last_name>_<first name innitial> TStr TStrUtil::GetStdName(TStr AuthorName) { TStr StdName; AuthorName.ToLc(); AuthorName.ChangeChAll('\n', ' '); AuthorName.ChangeChAll('.', ' '); // if there is a number in the name, remove it and everything after it int i, pos = 0; while (pos<AuthorName.Len() && (AuthorName[pos]!='#' && !TCh::IsNum(AuthorName[pos]))) { pos++; } if (pos < AuthorName.Len()) { AuthorName = AuthorName.GetSubStr(0, pos-1).ToTrunc(); } if (AuthorName.Empty()) { return TStr::GetNullStr(); } // replace everything after '(' int b = AuthorName.SearchCh('('); if (b != -1) { AuthorName = AuthorName.GetSubStr(0, b-1).ToTrunc(); } // skip if contains ')' if (AuthorName .SearchCh(')')!=-1) { return TStr::GetNullStr(); } // skip if it is not a name if (AuthorName .SearchStr("figures")!=-1 || AuthorName .SearchStr("macros")!=-1 || AuthorName .SearchStr("univ")!=-1 || AuthorName .SearchStr("institute")!=-1) { return TStr::GetNullStr(); } // remove all non-letters (latex tags, ...) TChA NewName; for (i = 0; i < AuthorName.Len(); i++) { const char Ch = AuthorName[i]; if (TCh::IsAlpha(Ch) || TCh::IsWs(Ch) || Ch=='-') { NewName += Ch; } } StdName = NewName; StdName.ToTrunc(); TStrV AuthNmV; StdName.SplitOnWs(AuthNmV); // too short -- not a name if (! AuthNmV.Empty() && AuthNmV.Last() == "jr") AuthNmV.DelLast(); if (AuthNmV.Len() < 2) return TStr::GetNullStr(); const TStr LastNm = AuthNmV.Last(); if (! TCh::IsAlpha(LastNm[0]) || LastNm.Len() == 1) return TStr::GetNullStr(); IAssert(isalpha(AuthNmV[0][0])); return TStr::Fmt("%s_%c", LastNm.CStr(), AuthNmV[0][0]); }
void TStrUtil::GetStdNameV(TStr AuthorNames, TStrV& StdNameV) { AuthorNames.ChangeChAll('\n', ' '); AuthorNames.ToLc(); // split into author names TStrV AuthV, TmpV, Tmp2V; // split on 'and' AuthorNames.SplitOnStr(" and ", TmpV); int i; for (i = 0; i < TmpV.Len(); i++) { TmpV[i].SplitOnAllCh(',', Tmp2V); AuthV.AddV(Tmp2V); } // split on '&' TmpV = AuthV; AuthV.Clr(); for (i = 0; i < TmpV.Len(); i++) { TmpV[i].SplitOnAllCh('&', Tmp2V); AuthV.AddV(Tmp2V); } // split on ',' TmpV = AuthV; AuthV.Clr(); for (i = 0; i < TmpV.Len(); i++) { TmpV[i].SplitOnAllCh(',', Tmp2V); AuthV.AddV(Tmp2V); } // split on ';' TmpV = AuthV; AuthV.Clr(); for (i = 0; i < TmpV.Len(); i++) { TmpV[i].SplitOnAllCh(';', Tmp2V); AuthV.AddV(Tmp2V); } // standardize names StdNameV.Clr(); //printf("\n*** %s\n", AuthorNames.CStr()); for (i = 0; i < AuthV.Len(); i++) { TStr StdName = GetStdName(AuthV[i]); if (! StdName.Empty()) { //printf("\t%s ==> %s\n", AuthV[i].CStr(), StdName.CStr()); StdNameV.Add(StdName); } } }
///////////////////////////////////////////////// // Google-Web-Context-Graph PGgCtxGraph TGgCtxGraph::GetCtxGraph(const TStr& FocusUrlStr){ // create context-graph PGgCtxGraph CtxGraph=TGgCtxGraph::New(); CtxGraph->Ok=false; // get focus-web-page CtxGraph->FocusUrlStr=FocusUrlStr; {bool Ok; TStr MsgStr; TWebFetchBlocking::GetWebPg( CtxGraph->FocusUrlStr, Ok, MsgStr, CtxGraph->FocusWebPg, TNotify::StdNotify); if (!Ok){return CtxGraph;}} // get 'In' set of links PRSet InRSet= TGg::WebSearch(TStr("link:")+CtxGraph->FocusUrlStr, -1, TNotify::StdNotify); for (int HitN=0; HitN<InRSet->GetHits(); HitN++){ TStr UrlStr; TStr TitleStr; TStr SrcNm; TStr CtxStr; InRSet->GetHit(HitN, UrlStr, TitleStr, SrcNm, CtxStr); TitleStr.ChangeChAll('\n', ' '); CtxGraph->InUrlCtxStrPrV.Add(TStrPr(UrlStr, TitleStr)); } // get Out set TStrKdV OutDescUrlStrKdV; CtxGraph->FocusWebPg->GetOutDescUrlStrKdV(OutDescUrlStrKdV); for (int UrlN=0; UrlN<OutDescUrlStrKdV.Len(); UrlN++){ OutDescUrlStrKdV[UrlN].Key.ChangeChAll('\n', ' '); CtxGraph->OutUrlCtxStrPrV.Add( TStrPr(OutDescUrlStrKdV[UrlN].Dat, OutDescUrlStrKdV[UrlN].Key)); } // set context-graph successful CtxGraph->Ok=true; // return context-graph return CtxGraph; }
void TSAppSrvFun::LogReqRes(const TStrKdV& FldNmValPrV, const PHttpResp& HttpResp) { if (LogRqFolder == "") return; try { TDir::GenDir(LogRqFolder); TStr TimeNow = TTm::GetCurLocTm().GetWebLogDateTimeStr(true); TimeNow.ChangeChAll(':', '.'); PSOut Output = TFOut::New(LogRqFolder + "/" + TimeNow + ".txt", false); Output->PutStr(FunNm.CStr()); Output->PutCh('\n'); for (int N=0; N < FldNmValPrV.Len(); N++) Output->PutStrFmt(" %s: %s\n", FldNmValPrV[N].Key.CStr(), FldNmValPrV[N].Dat.CStr()); Output->PutCh('\n'); Output->PutStr(HttpResp->GetBodyAsStr(), false); } catch (...) { /*const PNotify& Notify = RqEnv->GetWebSrv()->GetNotify(); Notify->OnStatus("Unable to log request for function '" + GetFunNm() + "'!");*/ } }
////////////////////////////////////// // File-Download-Function void TSASFunFile::LoadFunFileV(const TStr& FPath, TSAppSrvFunV& SrvFunV) { TFFile File(FPath, "", false); TStr FNm; while (File.Next(FNm)) { TStr FExt = FNm.GetFExt(); TStr FUrl = FNm.GetSubStr(FPath.Len()); FUrl.ChangeChAll('\\', '/'); printf("%s %s %s\n", FNm.CStr(), FExt.CStr(), FUrl.CStr()); if (FExt == ".htm") { SrvFunV.Add(TSASFunFile::New(FUrl, FNm, THttp::TextHtmlFldVal)); } else if (FExt == ".html") { SrvFunV.Add(TSASFunFile::New(FUrl, FNm, THttp::TextHtmlFldVal)); } else if (FExt == ".js") { SrvFunV.Add(TSASFunFile::New(FUrl, FNm, THttp::TextJavaScriptFldVal)); } else if (FExt == ".css") { SrvFunV.Add(TSASFunFile::New(FUrl, FNm, THttp::TextCssFldVal)); } else if (FExt == ".jpg") { SrvFunV.Add(TSASFunFile::New(FUrl, FNm, THttp::ImageJpgFldVal)); } else if (FExt == ".jpeg") { SrvFunV.Add(TSASFunFile::New(FUrl, FNm, THttp::ImageJpgFldVal)); } else if (FExt == ".gif") { SrvFunV.Add(TSASFunFile::New(FUrl, FNm, THttp::ImageGifFldVal)); } else { printf("Unknown MIME type for extension '%s' for file '%s'", FExt.CStr(), FNm.CStr()); SrvFunV.Add(TSASFunFile::New(FUrl, FNm, THttp::AppOctetFldVal)); } } }
TFltRect TVizMapContext::PaintPointNm(PGks Gks, PVizMapPoint Point, const int& X, const int& Y, const int& PointFontSize, const int& PointNmFontScale, const bool& SelPointP, const bool& IsCatP) { // get and clean point name TStr PointNm = Point->GetPointNm(); PointNm.ChangeChAll('_', ' '); if (PointNm.IsStrIn("[[")) { const int StartPos = PointNm.SearchStr("[["); PointNm = PointNm.Left(StartPos - 1); } // set font TGksColor FontColor = SelPointP ? ColorSelPointFont : ColorPointFont; const int FontSize = PointFontSize + TFlt::Round(Point->GetWgt()*PointNmFontScale); //TFSet FontStyle = IsCatP ? (TFSet() | gfsBold) : TFSet(); //Gks->SetFont(TGksFont::New("ARIAL", FontSize, FontColor, FontStyle)); Gks->SetFont(TGksFont::New("ARIAL", FontSize, FontColor)); // refit it for the screen TStr ScreenPointNm = Gks->BreakTxt(PointNm, " ", "", PointNmWidth, PointNmMxLines); // calculate string position on the screen const int HalfTxtWidth = Gks->GetTxtWidth(ScreenPointNm) / 2; const int HalfTxtHeight = Gks->GetTxtHeight(ScreenPointNm) / 2; // draw it! const int MnX = X - HalfTxtWidth; int CurrY = Y - HalfTxtHeight; TStrV LineV; ScreenPointNm.SplitOnAllCh('\n', LineV); for (int LineN = 0; LineN < LineV.Len(); LineN++) { const int HalfLineWidth = Gks->GetTxtWidth(LineV[LineN]) / 2; const int LineHeight = Gks->GetTxtHeight(LineV[LineN]); Gks->PutTxt(LineV[LineN], MnX + (HalfTxtWidth - HalfLineWidth), CurrY); CurrY += LineHeight-3; } // finish return TFltRect(X - HalfTxtWidth, Y - HalfTxtHeight, X + HalfTxtWidth, Y + HalfTxtHeight - LineV.Len()*3); }
PLwOnto TLwOnto::LoadAsfaVoc(const TStr& FPath){ // normalize path TStr NrFPath=TStr::GetNrFPath(FPath); // create ontology PLwOnto LwOnto=TLwOnto::New(); // create language object int EnLangId=LwOnto->GetLangBs()->AddLang("EN", "English"); // create term-types {PLwTermType D_TermType=TLwTermType::New(0, "Descriptor", EnLangId); PLwTermType ND_TermType=TLwTermType::New(1, "Non-descriptor", EnLangId); LwOnto->GetTermTypeBs()->AddTermType(D_TermType); LwOnto->GetTermTypeBs()->AddTermType(ND_TermType);} // create link-types {PLwLinkType BT_LinkType=TLwLinkType::New(0, "BT", EnLangId, "Broader-Term"); PLwLinkType NT_LinkType=TLwLinkType::New(1, "NT", EnLangId, "Narrower-Term"); PLwLinkType RT_LinkType=TLwLinkType::New(2, "RT", EnLangId, "Related-Term"); PLwLinkType UF_LinkType=TLwLinkType::New(3, "UF", EnLangId, "Used-For"); PLwLinkType USE_LinkType=TLwLinkType::New(4, "USE", EnLangId, "Used-By"); LwOnto->GetLinkTypeBs()->AddLinkType(BT_LinkType); LwOnto->GetLinkTypeBs()->AddLinkType(NT_LinkType); LwOnto->GetLinkTypeBs()->AddLinkType(RT_LinkType); LwOnto->GetLinkTypeBs()->AddLinkType(UF_LinkType); LwOnto->GetLinkTypeBs()->AddLinkType(USE_LinkType);} // load ontology file TStr AsfaOntoFNm=NrFPath+"asfa_xml_20060522.xml"; printf("Loading '%s' ...", AsfaOntoFNm.CStr()); PXmlDoc AsfaXmlDoc=TXmlDoc::LoadTxt(AsfaOntoFNm); IAssert(AsfaXmlDoc->IsOk()); TXmlTokV ConceptXmlTokV; AsfaXmlDoc->GetTagTokV("THESAURUS|CONCEPT", ConceptXmlTokV); printf(" Done.\n"); // create terms {printf("Creating terms ..."); for (int ConceptN=0; ConceptN<ConceptXmlTokV.Len(); ConceptN++){ PXmlTok ConceptXmlTok=ConceptXmlTokV[ConceptN]; // term-name TStr TermNm; if (ConceptXmlTok->IsSubTag("NON-DESCRIPTOR")){ TermNm=ConceptXmlTok->GetTagTokStr("NON-DESCRIPTOR");} else if (ConceptXmlTok->IsSubTag("DESCRIPTOR")){ TermNm=ConceptXmlTok->GetTagTokStr("DESCRIPTOR");} // term-type TStr TermTypeNm=ConceptXmlTok->GetTagTokStr("TYP"); int TermTypeId=LwOnto->GetTermTypeBs()->GetTermTypeId(TermTypeNm, EnLangId); // description TStr DescStr; if (ConceptXmlTok->IsSubTag("SN")){ DescStr=ConceptXmlTok->GetTagTokStr("SN"); DescStr.ChangeChAll('\r', ' '); DescStr.ChangeChAll('\n', ' '); DescStr.ChangeStrAll(" ", " "); DescStr.ToTrunc(); } // create term PLwTerm Term=TLwTerm::New(-1, TermNm, EnLangId, TermTypeId, DescStr); LwOnto->GetTermBs()->AddTermGetTermId(Term); } printf(" Done. (%d)\n", LwOnto->GetTermBs()->GetTerms());} // create links {printf("Creating links ..."); for (int ConceptN=0; ConceptN<ConceptXmlTokV.Len(); ConceptN++){ PXmlTok ConceptXmlTok=ConceptXmlTokV[ConceptN]; // source-term-name TStr TermNm1; if (ConceptXmlTok->IsSubTag("NON-DESCRIPTOR")){ TermNm1=ConceptXmlTok->GetTagTokStr("NON-DESCRIPTOR");} else if (ConceptXmlTok->IsSubTag("DESCRIPTOR")){ TermNm1=ConceptXmlTok->GetTagTokStr("DESCRIPTOR");} int TermId1=LwOnto->GetTermBs()->GetTermId(TermNm1, EnLangId); // links for (int SubTokN=0; SubTokN<ConceptXmlTok->GetSubToks(); SubTokN++){ PXmlTok SubTok=ConceptXmlTok->GetSubTok(SubTokN); if (SubTok->IsTag()){ TStr LinkTypeNm=SubTok->GetTagNm(); if (LwOnto->GetLinkTypeBs()->IsLinkType(LinkTypeNm, EnLangId)){ // destination-term-name TStr TermNm2=ConceptXmlTok->GetTagTokStr(LinkTypeNm); int TermId2=LwOnto->GetTermBs()->GetTermId(TermNm2, EnLangId); int LinkTypeId=LwOnto->GetLinkTypeBs()->GetLinkTypeId(LinkTypeNm, EnLangId); LwOnto->GetLinkBs()->AddLink(TermId1, LinkTypeId, TermId2); } } } } printf(" Done. (%d)\n", LwOnto->GetLinkBs()->GetLinks());} // return ontology return LwOnto; }
/// save graph into a gexf file which Gephi can read void TAGMUtil::SaveGephi(const TStr& OutFNm, const PUNGraph& G, const TVec<TIntV>& CmtyVVAtr, const double MaxSz, const double MinSz, const TIntStrH& NIDNameH, const THash<TInt, TIntTr>& NIDColorH ) { THash<TInt,TIntV> NIDComVHAtr; TAGMUtil::GetNodeMembership(NIDComVHAtr, CmtyVVAtr); FILE* F = fopen(OutFNm.CStr(), "wt"); fprintf(F, "<?xml version='1.0' encoding='UTF-8'?>\n"); fprintf(F, "<gexf xmlns='http://www.gexf.net/1.2draft' xmlns:viz='http://www.gexf.net/1.1draft/viz' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://www.gexf.net/1.2draft http://www.gexf.net/1.2draft/gexf.xsd' version='1.2'>\n"); fprintf(F, "\t<graph mode='static' defaultedgetype='undirected'>\n"); if (CmtyVVAtr.Len() > 0) { fprintf(F, "\t<attributes class='node'>\n"); for (int c = 0; c < CmtyVVAtr.Len(); c++) { fprintf(F, "\t\t<attribute id='%d' title='c%d' type='boolean'>", c, c); fprintf(F, "\t\t<default>false</default>\n"); fprintf(F, "\t\t</attribute>\n"); } fprintf(F, "\t</attributes>\n"); } fprintf(F, "\t\t<nodes>\n"); for (TUNGraph::TNodeI NI = G->BegNI(); NI < G->EndNI(); NI++) { int NID = NI.GetId(); TStr Label = NIDNameH.IsKey(NID)? NIDNameH.GetDat(NID): ""; Label.ChangeChAll('<', ' '); Label.ChangeChAll('>', ' '); Label.ChangeChAll('&', ' '); Label.ChangeChAll('\'', ' '); TIntTr Color = NIDColorH.IsKey(NID)? NIDColorH.GetDat(NID) : TIntTr(120, 120, 120); double Size = MinSz; double SizeStep = (MaxSz - MinSz) / (double) CmtyVVAtr.Len(); if (NIDComVHAtr.IsKey(NID)) { Size = MinSz + SizeStep * (double) NIDComVHAtr.GetDat(NID).Len(); } double Alpha = 1.0; fprintf(F, "\t\t\t<node id='%d' label='%s'>\n", NID, Label.CStr()); fprintf(F, "\t\t\t\t<viz:color r='%d' g='%d' b='%d' a='%.1f'/>\n", Color.Val1.Val, Color.Val2.Val, Color.Val3.Val, Alpha); fprintf(F, "\t\t\t\t<viz:size value='%.3f'/>\n", Size); //specify attributes if (NIDComVHAtr.IsKey(NID)) { fprintf(F, "\t\t\t\t<attvalues>\n"); for (int c = 0; c < NIDComVHAtr.GetDat(NID).Len(); c++) { int CID = NIDComVHAtr.GetDat(NID)[c]; fprintf(F, "\t\t\t\t\t<attvalue for='%d' value='true'/>\n", CID); } fprintf(F, "\t\t\t\t</attvalues>\n"); } fprintf(F, "\t\t\t</node>\n"); } fprintf(F, "\t\t</nodes>\n"); //plot edges int EID = 0; fprintf(F, "\t\t<edges>\n"); for (TUNGraph::TNodeI NI = G->BegNI(); NI < G->EndNI(); NI++) { for (int e = 0; e < NI.GetOutDeg(); e++) { if (NI.GetId() > NI.GetOutNId(e)) { continue; } fprintf(F, "\t\t\t<edge id='%d' source='%d' target='%d'/>\n", EID++, NI.GetId(), NI.GetOutNId(e)); } } fprintf(F, "\t\t</edges>\n"); fprintf(F, "\t</graph>\n"); fprintf(F, "</gexf>\n"); fclose(F); }