///////////////////////////////////////////////// // Bipartite graph int TBPGraph::AddNode(int NId, const bool& LeftNode) { if (NId == -1) { NId = MxNId; MxNId++; } else if (IsLNode(NId)) { IAssertR(LeftNode, TStr::Fmt("Node with id %s already exists on the 'left'.", NId)); return NId; } else if (IsRNode(NId)) { IAssertR(! LeftNode, TStr::Fmt("Node with id %s already exists on the 'right'.", NId)); return NId; } else { MxNId = TMath::Mx(NId+1, MxNId()); } if (LeftNode) { LeftH.AddDat(NId, TNode(NId)); } else { RightH.AddDat(NId, TNode(NId)); } return NId; }
int TNEGraph::AddEdge(const int& SrcNId, const int& DstNId, int EId) { if (EId == -1) { EId = MxEId; MxEId++; } else { MxEId = TMath::Mx(EId+1, MxEId()); } IAssertR(!IsEdge(EId), TStr::Fmt("EdgeId %d already exists", EId)); IAssertR(IsNode(SrcNId) && IsNode(DstNId), TStr::Fmt("%d or %d not a node.", SrcNId, DstNId).CStr()); EdgeH.AddDat(EId, TEdge(EId, SrcNId, DstNId)); GetNode(SrcNId).OutEIdV.AddSorted(EId); GetNode(DstNId).InEIdV.AddSorted(EId); return EId; }
int TNEANetMP::AddEdge(const int& SrcNId, const int& DstNId, int EId) { int i; if (EId == -1) { EId = MxEId; MxEId++; } else { MxEId = TMath::Mx(EId+1, MxEId()); } IAssertR(!IsEdge(EId), TStr::Fmt("EdgeId %d already exists", EId)); IAssertR(IsNode(SrcNId) && IsNode(DstNId), TStr::Fmt("%d or %d not a node.", SrcNId, DstNId).CStr()); EdgeH.AddDat(EId, TEdge(EId, SrcNId, DstNId)); GetNode(SrcNId).OutEIdV.AddSorted(EId); GetNode(DstNId).InEIdV.AddSorted(EId); // update attribute columns for (i = 0; i < VecOfIntVecsE.Len(); i++) { TVec<TInt>& IntVec = VecOfIntVecsE[i]; IntVec.Ins(EdgeH.GetKeyId(EId), TInt::Mn); } TVec<TStr> DefIntVec = TVec<TStr>(); IntDefaultsE.GetKeyV(DefIntVec); for (i = 0; i < DefIntVec.Len(); i++) { TStr attr = DefIntVec[i]; TVec<TInt>& IntVec = VecOfIntVecsE[KeyToIndexTypeE.GetDat(DefIntVec[i]).Val2]; IntVec[EdgeH.GetKeyId(EId)] = GetIntAttrDefaultE(attr); } for (i = 0; i < VecOfStrVecsE.Len(); i++) { TVec<TStr>& StrVec = VecOfStrVecsE[i]; StrVec.Ins(EdgeH.GetKeyId(EId), TStr::GetNullStr()); } TVec<TStr> DefStrVec = TVec<TStr>(); IntDefaultsE.GetKeyV(DefStrVec); for (i = 0; i < DefStrVec.Len(); i++) { TStr attr = DefStrVec[i]; TVec<TStr>& StrVec = VecOfStrVecsE[KeyToIndexTypeE.GetDat(DefStrVec[i]).Val2]; StrVec[EdgeH.GetKeyId(EId)] = GetStrAttrDefaultE(attr); } for (i = 0; i < VecOfFltVecsE.Len(); i++) { TVec<TFlt>& FltVec = VecOfFltVecsE[i]; FltVec.Ins(EdgeH.GetKeyId(EId), TFlt::Mn); } TVec<TStr> DefFltVec = TVec<TStr>(); FltDefaultsE.GetKeyV(DefFltVec); for (i = 0; i < DefFltVec.Len(); i++) { TStr attr = DefFltVec[i]; TVec<TFlt>& FltVec = VecOfFltVecsE[KeyToIndexTypeE.GetDat(DefFltVec[i]).Val2]; FltVec[NodeH.GetKeyId(EId)] = GetFltAttrDefaultE(attr); } return EId; }
int TBPGraph::AddEdge(const int& LeftNId, const int& RightNId) { const bool IsLL = IsLNode(LeftNId), IsLR = IsRNode(LeftNId); const bool IsRL = IsLNode(RightNId), IsRR = IsRNode(RightNId); IAssertR((IsLL||IsLR)&&(IsRL||IsRR), TStr::Fmt("%d or %d is not a node.", LeftNId, RightNId).CStr()); IAssertR(LeftNId!=RightNId, "No self-edges are allowed."); IAssertR((IsLL&&!IsLR&&!IsRL&&IsRR)||(!IsLL&&IsLR&&IsRL&&!IsRR), "One node should be on the 'left' and the other on the 'right'."); const int LNId = IsLL ? LeftNId : RightNId; // the real left node const int RNId = IsLL ? RightNId : LeftNId; // the real right node if (LeftH.GetDat(LNId).IsOutNId(RNId)) { return -2; } // edge already exists LeftH.GetDat(LNId).NIdV.AddSorted(RNId); RightH.GetDat(RNId).NIdV.AddSorted(LNId); return -1; // no edge id }
TBPGraph::TEdgeI TBPGraph::GetEI(const int& LeftNId, const int& RightNId) const { const bool IsLL = IsLNode(LeftNId), IsLR = IsRNode(LeftNId); const bool IsRL = IsLNode(RightNId), IsRR = IsRNode(RightNId); IAssertR((IsLL||IsLR)&&(IsRL||IsRR), TStr::Fmt("%d or %d is not a node.", LeftNId, RightNId).CStr()); IAssertR(LeftNId!=RightNId, "No self-edges are allowed."); IAssertR((IsLL&&!IsLR&&!IsRL&&IsRR)||(!IsLL&&IsLR&&IsRL&&!IsRR), "One node should be on the 'left' and the other on the 'right'."); const int LNId = IsLL ? LeftNId : RightNId; // the real left node const int RNId = IsLL ? RightNId : LeftNId; // the real right node const TNodeI SrcNI = GetNI(LNId); const int NodeN = SrcNI.LeftHI.GetDat().NIdV.SearchBin(RNId); IAssertR(NodeN != -1, "Right edge endpoint does not exists!"); return TEdgeI(SrcNI, EndNI(), NodeN); }
void TBPGraph::DelEdge(const int& LeftNId, const int& RightNId) { const bool IsLL = IsLNode(LeftNId), IsLR = IsRNode(LeftNId); const bool IsRL = IsLNode(RightNId), IsRR = IsRNode(RightNId); IAssertR((IsLL||IsLR)&&(IsRL||IsRR), TStr::Fmt("%d or %d is not a node.", LeftNId, RightNId).CStr()); IAssertR(LeftNId!=RightNId, "No self-edges are allowed."); IAssertR((IsLL&&!IsLR&&!IsRL&&IsRR)||(!IsLL&&IsLR&&IsRL&&!IsRR), "One node should be on the 'left' and the other on the 'right'."); const int LNId = IsLL ? LeftNId : RightNId; // the real left node const int RNId = IsLL ? RightNId : LeftNId; // the real right node { TIntV& NIdV = LeftH.GetDat(LNId).NIdV; const int n = NIdV.SearchBin(RNId); if (n != -1) { NIdV.Del(n); } } { TIntV& NIdV = RightH.GetDat(RNId).NIdV; const int n = NIdV.SearchBin(LNId); if (n != -1) { NIdV.Del(n); } } }
PAlignPair TAlignPair::LoadAcXml(const TStr& FNm, const int& MxSents) { printf("Loading %s ...\n", FNm.CStr()); // get the lanugagne names TStr BaseNm = FNm.GetFMid(); TStrV PartV; BaseNm.SplitOnAllCh('-', PartV); IAssertR(PartV.Len() == 3, "Bad file name: " + BaseNm); // prepare aligne pair PAlignPair AlignPair = TAlignPair::New(PartV[1], PartV[2]); // parse the XML PTransCorpus TransCorpus = TTransCorpus::LoadAC(FNm, MxSents * 4); // select subset of sentences which will go into aligned corpus const int AllSents = TransCorpus->GetSentences(); TIntV SentIdV(AllSents, 0); for (int SentId = 0; SentId < AllSents; SentId++) { SentIdV.Add(SentId); } if (MxSents != -1 && AllSents > MxSents) { TRnd Rnd(1); SentIdV.Shuffle(Rnd); SentIdV.Trunc(MxSents); } // add the sentences to the bow const int Sents = SentIdV.Len(); for (int SentIdN = 0; SentIdN < Sents; SentIdN++) { const int SentId = SentIdV[SentIdN]; const TStr& Sent1 = TransCorpus->GetOrgStr(SentId); const TStr& Sent2 = TransCorpus->GetRefTransStrV(SentId)[0]; AlignPair->AddSent(Sent1, Sent2); } // finish the alignment pair AlignPair->Def(); return AlignPair; }
TMongSrv::TMongSrv(const int& _PortN, const bool& FixedPortNP, const PNotify& _Notify, int Threads) : Notify(_Notify), PortN(_PortN), HomeNrFPath( TStr::GetNrFPath(TDir::GetCurDir())), Rnd(TTm::GetMSecsFromOsStart()) { ClientsLock = new TCriticalSection(); const char *options[] = { //"document_root", HomeNrFPath.GetCStr(), "listening_ports", TInt::GetStr(PortN).GetCStr(), "num_threads", TInt::GetStr(Threads).GetCStr(), //"enable_keep_alive", "yes", //"auth_domain", TStr("").GetCStr(), 0 }; Ctx = mg_start(&HandleRequest, NULL, options); IAssertR(Ctx != NULL, "Web-Server: Server failed to start!"); TChA MsgChA; MsgChA += "Web-Server: Started at port "; MsgChA += TInt::GetStr(PortN); MsgChA += "."; TNotify::OnNotify(Notify, ntInfo, MsgChA); PMongSrv Pt(this); Registry.AddDat(PortN, Pt); }
/// Generates a small-world graph using the Watts-Strogatz model. /// We assume a circle where each node creates links to NodeOutDeg other nodes. /// This way at the end each node is connected to 2*NodeOutDeg other nodes. /// See: Collective dynamics of 'small-world' networks. Watts and Strogatz. /// URL: http://research.yahoo.com/files/w_s_NATURE_0.pdf PUNGraph GenSmallWorld(const int& Nodes, const int& NodeOutDeg, const double& RewireProb, TRnd& Rnd) { THashSet<TIntPr> EdgeSet(Nodes*NodeOutDeg); IAssertR(Nodes > NodeOutDeg, TStr::Fmt("Insufficient nodes for out degree, %d!", NodeOutDeg)); for (int node = 0; node < Nodes; node++) { const int src = node; for (int edge = 1; edge <= NodeOutDeg; edge++) { int dst = (node+edge) % Nodes; // edge to next neighbor if (Rnd.GetUniDev() < RewireProb) { // random edge dst = Rnd.GetUniDevInt(Nodes); while (dst == src || EdgeSet.IsKey(TIntPr(src, dst))) { dst = Rnd.GetUniDevInt(Nodes); } } EdgeSet.AddKey(TIntPr(src, dst)); } } PUNGraph GraphPt = TUNGraph::New(); TUNGraph& Graph = *GraphPt; Graph.Reserve(Nodes, EdgeSet.Len()); int node; for (node = 0; node < Nodes; node++) { IAssert(Graph.AddNode(node) == node); } for (int edge = 0; edge < EdgeSet.Len(); edge++) { Graph.AddEdge(EdgeSet[edge].Val1, EdgeSet[edge].Val2); } Graph.Defrag(); return GraphPt; }
// Add an edge between SrcNId and DstNId to the graph. int TUNGraph::AddEdge(const int& SrcNId, const int& DstNId) { IAssertR(IsNode(SrcNId) && IsNode(DstNId), TStr::Fmt("%d or %d not a node.", SrcNId, DstNId).CStr()); if (IsEdge(SrcNId, DstNId)) { return -2; } // edge already exists GetNode(SrcNId).NIdV.AddSorted(DstNId); if (SrcNId!=DstNId) { // not a self edge GetNode(DstNId).NIdV.AddSorted(SrcNId); } return -1; // edge id }
int TNGraph::AddEdge(const int& SrcNId, const int& DstNId) { IAssertR(IsNode(SrcNId) && IsNode(DstNId), TStr::Fmt("%d or %d not a node.", SrcNId, DstNId).CStr()); //IAssert(! IsEdge(SrcNId, DstNId)); if (IsEdge(SrcNId, DstNId)) { return -2; } GetNode(SrcNId).OutNIdV.AddSorted(DstNId); GetNode(DstNId).InNIdV.AddSorted(SrcNId); return -1; // edge id }
bool TGStat::TCmpByVal::operator () (const TGStat& GS1, const TGStat& GS2) const { IAssertR(GS1.HasVal(ValCmp) && GS2.HasVal(ValCmp), TStr::Fmt("CmpVal: %d (%s)", int(ValCmp), TGStat::GetValStr(ValCmp).CStr()).CStr()); bool Res; if (ValCmp == gsvTime) { Res = GS1.Time < GS2.Time; } else { Res = GS1.GetVal(ValCmp) < GS2.GetVal(ValCmp); } if (SortAsc) { return Res; } else { return ! Res; } }
// add a node from a vector pool // (use TUNGraph::IsOk to check whether the graph is consistent) int TUNGraph::AddNode(const int& NId, const TVecPool<TInt>& Pool, const int& NIdVId) { IAssert(NId != -1); IAssertR(! IsNode(NId), TStr::Fmt("NodeId %d already exists", NId)); MxNId = TMath::Mx(NId+1, MxNId()); TNode& Node = NodeH.AddDat(NId); Node.Id = NId; Node.NIdV.GenExt(Pool.GetValVPt(NIdVId), Pool.GetVLen(NIdVId)); Node.NIdV.Sort(); return NId; }
// Add a node of ID NId to the graph. int TUNGraph::AddNode(int NId) { if (NId == -1) { NId = MxNId; MxNId++; } else { IAssertR(!IsNode(NId), TStr::Fmt("NodeId %d already exists", NId)); MxNId = TMath::Mx(NId+1, MxNId()); } NodeH.AddDat(NId, TNode(NId)); return NId; }
// Adds Len characters to pool. To append a null terminated string Len must be equal to strlen(s) + 1 int TBigStrPool::AddStr(const char *Str, uint Len) { IAssertR(Len > 0, "String too short (lenght includes the null character)"); //J: if (! Len) return -1; Assert(Str); Assert(Len > 0); if (Len == 1 && IdOffV.Len() > 0) { return 0; } // empty string if (BfL + Len > MxBfL) { Resize(BfL + Len); } memcpy(Bf + BfL, Str, Len); TSize Pos = BfL; BfL += Len; IdOffV.Add(Pos); return IdOffV.Len()-1; }
// add a node with a list of neighbors // (use TUNGraph::IsOk to check whether the graph is consistent) int TUNGraph::AddNode(const int& NId, const TIntV& NbhNIdV) { IAssert(NId != -1); IAssertR(! IsNode(NId), TStr::Fmt("NodeId %d already exists", NId)); MxNId = TMath::Mx(NId+1, MxNId()); TNode& Node = NodeH.AddDat(NId); Node.Id = NId; Node.NIdV = NbhNIdV; Node.NIdV.Sort(); return NId; }
void TMultimodalGraphImplB::DelEdge(const TPair<TInt,TInt>& SrcNId, const TPair<TInt,TInt>& DstNId) { IAssertR(IsNode(SrcNId) && IsNode(DstNId), TStr::Fmt("%d or %d not a node.", SrcNId.GetVal2(), DstNId.GetVal2()).CStr()); if (!IsEdge(SrcNId, DstNId)) { return; // Edge doesn't exist } NEdges--; TPair<TInt,TInt> ModeIdsKey = GetModeIdsKey(SrcNId.GetVal1(), DstNId.GetVal1()); Graphs.GetDat(ModeIdsKey).DelEdge(SrcNId.GetVal2(), DstNId.GetVal2()); }
void TUNGraph::DelEdge(const int& SrcNId, const int& DstNId) { IAssertR(IsNode(SrcNId) && IsNode(DstNId), TStr::Fmt("%d or %d not a node.", SrcNId, DstNId).CStr()); { TNode& N = GetNode(SrcNId); int n = N.NIdV.SearchBin(DstNId); if (n!= -1) { N.NIdV.Del(n); } } if (SrcNId != DstNId) { // not a self edge TNode& N = GetNode(DstNId); int n = N.NIdV.SearchBin(SrcNId); if (n!= -1) { N.NIdV.Del(n); } } }
PBPGraph GenRndBipart(const int& LeftNodes, const int& RightNodes, const int& Edges, TRnd& Rnd) { PBPGraph G = TBPGraph::New(); for (int i = 0; i < LeftNodes; i++) { G->AddNode(i, true); } for (int i = 0; i < RightNodes; i++) { G->AddNode(LeftNodes+i, false); } IAssertR(Edges <= LeftNodes*RightNodes, "Too many edges in the bipartite graph!"); for (int edges = 0; edges < Edges; ) { const int LNId = Rnd.GetUniDevInt(LeftNodes); const int RNId = LeftNodes + Rnd.GetUniDevInt(RightNodes); if (G->AddEdge(LNId, RNId) != -2) { edges++; } // is new edge } return G; }
/// Generates a random graph with exact degree sequence DegSeqV. /// The generated graph has no self loops. The graph generation process /// simulates the Configuration Model but if a duplicate edge occurs, we find a /// random edge, break it and reconnect it with the duplicate. PUNGraph GenDegSeq(const TIntV& DegSeqV, TRnd& Rnd) { const int Nodes = DegSeqV.Len(); PUNGraph GraphPt = TUNGraph::New(); TUNGraph& Graph = *GraphPt; Graph.Reserve(Nodes, -1); TIntH DegH(DegSeqV.Len(), true); IAssertR(DegSeqV.IsSorted(false), "DegSeqV must be sorted in descending order."); int DegSum=0, edge=0; for (int node = 0; node < Nodes; node++) { IAssert(Graph.AddNode(node) == node); DegH.AddDat(node, DegSeqV[node]); DegSum += DegSeqV[node]; } IAssert(DegSum % 2 == 0); while (! DegH.Empty()) { // pick random nodes and connect const int NId1 = DegH.GetKey(DegH.GetRndKeyId(TInt::Rnd, 0.5)); const int NId2 = DegH.GetKey(DegH.GetRndKeyId(TInt::Rnd, 0.5)); IAssert(DegH.IsKey(NId1) && DegH.IsKey(NId2)); if (NId1 == NId2) { if (DegH.GetDat(NId1) == 1) { continue; } // find rnd edge, break it, and connect the endpoints to the nodes const TIntPr Edge = TSnapDetail::GetRndEdgeNonAdjNode(GraphPt, NId1, -1); if (Edge.Val1==-1) { continue; } Graph.DelEdge(Edge.Val1, Edge.Val2); Graph.AddEdge(Edge.Val1, NId1); Graph.AddEdge(NId1, Edge.Val2); if (DegH.GetDat(NId1) == 2) { DegH.DelKey(NId1); } else { DegH.GetDat(NId1) -= 2; } } else { if (! Graph.IsEdge(NId1, NId2)) { Graph.AddEdge(NId1, NId2); } // good edge else { // find rnd edge, break and cross-connect const TIntPr Edge = TSnapDetail::GetRndEdgeNonAdjNode(GraphPt, NId1, NId2); if (Edge.Val1==-1) {continue; } Graph.DelEdge(Edge.Val1, Edge.Val2); Graph.AddEdge(NId1, Edge.Val1); Graph.AddEdge(NId2, Edge.Val2); } if (DegH.GetDat(NId1)==1) { DegH.DelKey(NId1); } else { DegH.GetDat(NId1) -= 1; } if (DegH.GetDat(NId2)==1) { DegH.DelKey(NId2); } else { DegH.GetDat(NId2) -= 1; } } if (++edge % 1000 == 0) { printf("\r %dk / %dk", edge/1000, DegSum/2000); } } return GraphPt; }
///////////////////////////////////////////////// // Big-String-Pool void TBigStrPool::Resize(TSize _MxBfL) { TSize newSize = MxBfL; while (newSize < _MxBfL) { if (newSize >= GrowBy && GrowBy > 0) newSize += GrowBy; else if (newSize > 0) newSize *= 2; else newSize = TInt::GetMn(GrowBy, 1024); } if (newSize > MxBfL) { Bf = (char *) realloc(Bf, newSize); IAssertR(Bf, TStr::Fmt("old Bf size: %u, new size: %u", MxBfL, newSize).CStr()); MxBfL = newSize; } IAssert(MxBfL >= _MxBfL); }
// Add a node of ID NId to the graph and create edges to all nodes in vector NbrNIdV. int TUNGraph::AddNode(const int& NId, const TIntV& NbrNIdV) { int NewNId; if (NId == -1) { NewNId = MxNId; MxNId++; } else { IAssertR(!IsNode(NId), TStr::Fmt("NodeId %d already exists", NId)); NewNId = NId; MxNId = TMath::Mx(NewNId+1, MxNId()); } TNode& Node = NodeH.AddDat(NewNId); Node.Id = NewNId; Node.NIdV = NbrNIdV; Node.NIdV.Sort(); return NewNId; }
// Delete node of ID NId from the bipartite graph. void TBPGraph::DelNode(const int& NId) { AssertR(IsNode(NId), TStr::Fmt("NodeId %d does not exist", NId)); THash<TInt, TNode>& SrcH = IsLNode(NId) ? LeftH : RightH; THash<TInt, TNode>& DstH = IsLNode(NId) ? RightH : LeftH; { TNode& Node = SrcH.GetDat(NId); for (int e = 0; e < Node.GetOutDeg(); e++) { const int nbr = Node.GetOutNId(e); IAssertR(nbr != NId, "Bipartite graph has a loop!"); TNode& N = DstH.GetDat(nbr); const int n = N.NIdV.SearchBin(NId); IAssert(n!= -1); N.NIdV.Del(n); } } SrcH.DelKey(NId); }
// Add a node of ID NId to the graph and create edges to all nodes in the vector NIdVId in the vector pool Pool). int TUNGraph::AddNode(const int& NId, const TVecPool<TInt>& Pool, const int& NIdVId) { int NewNId; if (NId == -1) { NewNId = MxNId; MxNId++; } else { IAssertR(!IsNode(NId), TStr::Fmt("NodeId %d already exists", NId)); NewNId = NId; MxNId = TMath::Mx(NewNId+1, MxNId()); } TNode& Node = NodeH.AddDat(NewNId); Node.Id = NewNId; Node.NIdV.GenExt(Pool.GetValVPt(NIdVId), Pool.GetVLen(NIdVId)); Node.NIdV.Sort(); NEdges += Node.GetDeg(); return NewNId; }
void TMultimodalGraphImplB::DelNode(const TPair<TInt,TInt>& NId) { IAssertR(IsNode(NId), TStr::Fmt("NodeId %d does not exist", NId.GetVal2())); int ModeId = NId.GetVal1(); // Remove NId from all relevant graphs for (TGraphs::TIter it = Graphs.BegI(); it < Graphs.EndI(); it++) { TPair<TInt, TInt> ModeIdPair = it.GetKey(); if (ModeIdPair.GetVal1() == ModeId || ModeIdPair.GetVal2() == ModeId) { TNGraph& Graph = it.GetDat(); if (Graph.IsNode(NId.GetVal2())) { Graph.DelNode(NId.GetVal2()); } } } // Remove NId from NodeToModeMapping as well NodeToModeMapping.DelKey(NId.GetVal2()); }
void TNGraph::DelEdge(const int& SrcNId, const int& DstNId, const bool& Dir) { IAssertR(IsNode(SrcNId) && IsNode(DstNId), TStr::Fmt("%d or %d not a node.", SrcNId, DstNId).CStr()); { TNode& N = GetNode(SrcNId); int n = N.OutNIdV.SearchBin(DstNId); if (n!= -1) { N.OutNIdV.Del(n); } } { TNode& N = GetNode(DstNId); int n = N.InNIdV.SearchBin(SrcNId); if (n!= -1) { N.InNIdV.Del(n); } } if (! Dir) { { TNode& N = GetNode(SrcNId); int n = N.InNIdV.SearchBin(DstNId); if (n!= -1) { N.InNIdV.Del(n); } } { TNode& N = GetNode(DstNId); int n = N.OutNIdV.SearchBin(SrcNId); if (n!= -1) { N.OutNIdV.Del(n); } } } }
int TNEANetMP::AddNode(int NId) { int i; if (NId == -1) { NId = MxNId; MxNId++; } else { IAssertR(!IsNode(NId), TStr::Fmt("NodeId %d already exists", NId)); MxNId = TMath::Mx(NId+1, MxNId()); } // update attribute columns NodeH.AddDat(NId, TNode(NId)); for (i = 0; i < VecOfIntVecsN.Len(); i++) { TVec<TInt>& IntVec = VecOfIntVecsN[i]; IntVec.Ins(NodeH.GetKeyId(NId), TInt::Mn); } TVec<TStr> DefIntVec = TVec<TStr>(); IntDefaultsN.GetKeyV(DefIntVec); for (i = 0; i < DefIntVec.Len(); i++) { TStr attr = DefIntVec[i]; TVec<TInt>& IntVec = VecOfIntVecsN[KeyToIndexTypeN.GetDat(DefIntVec[i]).Val2]; IntVec[NodeH.GetKeyId(NId)] = GetIntAttrDefaultN(attr); } for (i = 0; i < VecOfStrVecsN.Len(); i++) { TVec<TStr>& StrVec = VecOfStrVecsN[i]; StrVec.Ins(NodeH.GetKeyId(NId), TStr::GetNullStr()); } TVec<TStr> DefStrVec = TVec<TStr>(); IntDefaultsN.GetKeyV(DefStrVec); for (i = 0; i < DefStrVec.Len(); i++) { TStr attr = DefStrVec[i]; TVec<TStr>& StrVec = VecOfStrVecsN[KeyToIndexTypeN.GetDat(DefStrVec[i]).Val2]; StrVec[NodeH.GetKeyId(NId)] = GetStrAttrDefaultN(attr); } for (i = 0; i < VecOfFltVecsN.Len(); i++) { TVec<TFlt>& FltVec = VecOfFltVecsN[i]; FltVec.Ins(NodeH.GetKeyId(NId), TFlt::Mn); } TVec<TStr> DefFltVec = TVec<TStr>(); FltDefaultsN.GetKeyV(DefFltVec); for (i = 0; i < DefFltVec.Len(); i++) { TStr attr = DefFltVec[i]; TVec<TFlt>& FltVec = VecOfFltVecsN[KeyToIndexTypeN.GetDat(DefFltVec[i]).Val2]; FltVec[NodeH.GetKeyId(NId)] = GetFltAttrDefaultN(attr); } return NId; }
PTransCorpus TTransCorpus::LoadEP(const TStr& InOrgFPath, const TStr& InTransFPath) { // prepare prset structures PTransCorpus TransCorpus = TTransCorpus::New(); // iterate over all the files TStr NrmInTransFPath = TStr::GetNrAbsFPath(InTransFPath); TFFile OrgFNms(InOrgFPath, "txt", false); TStr OrgFNm; int SentId = 0; while (OrgFNms.Next(OrgFNm)) { // get name of the file with aligned sentences TStr TransFNm = NrmInTransFPath + OrgFNm.GetFBase(); IAssertR(TFile::Exists(TransFNm), TransFNm); // load file printf("Loading %s and %s ...\r", OrgFNm.CStr(), TransFNm.CStr()); TLnRet OrgLnRet(TFIn::New(OrgFNm)); TLnRet TransLnRet(TFIn::New(TransFNm)); TStr OrgLn, TransLn; int LnN = 1; bool EmptyLnP = false; while (OrgLnRet.NextLn(OrgLn)) { if (!TransLnRet.NextLn(TransLn)) { printf("\nEarly stop in line (%s:%s)[%d]\n", OrgLn.CStr(), TransLn.CStr(), LnN); break; // first file finished, let's stop } if (OrgLn.Empty() || TransFNm.Empty()) { // skip empty line and skip till EmptyLnP = true; } else if (OrgLn[0] == '<' || TransLn[0] == '<') { if (TransLn[0] != OrgLn[0]) { printf("\nError in line (%s:%s)[%d]\n", OrgLn.CStr(), TransLn.CStr(), LnN); break; // we stop, lines not aligned anymore ... } // reset the empty count EmptyLnP = false; // skip XML tags } else if (!EmptyLnP) { // aligned sentence! TransCorpus->AddSentenceNoTrans(SentId, OrgLn.ToTrunc(), TransLn.ToTrunc()); SentId++; } LnN++; } } printf("\nDone!\n"); // finish return TransCorpus; }
// Add a node of ID NId to the graph and create edges to all nodes in vector NbrNIdV. int TUNGraph::AddNode(const int& NId, const TIntV& NbrNIdV) { int NewNId; if (NId == -1) { NewNId = MxNId; MxNId++; } else { IAssertR(! IsNode(NId), TStr::Fmt("NodeId %d already exists", NId)); NewNId = NId; MxNId = TMath::Mx(NewNId+1, MxNId()); } TNode& Node = NodeH.AddDat(NewNId); Node.Id = NewNId; Node.NIdV = NbrNIdV; Node.NIdV.Sort(); NEdges += Node.GetDeg(); for (int i = 0; i < NbrNIdV.Len(); i++) { GetNode(NbrNIdV[i]).NIdV.AddSorted(NewNId); } return NewNId; }
int TMultimodalGraphImplB::AddEdge(const TPair<TInt,TInt>& SrcNId, const TPair<TInt,TInt>& DstNId) { IAssertR(IsNode(SrcNId) && IsNode(DstNId), TStr::Fmt("%d or %d not a node.", SrcNId.GetVal2(), DstNId.GetVal2()).CStr()); TPair<TInt,TInt> ModeIdsKey = GetModeIdsKey(SrcNId.GetVal1(), DstNId.GetVal1()); if (!Graphs.IsKey(ModeIdsKey)) { Graphs.AddDat(ModeIdsKey, TNGraph()); } if (!Graphs.GetDat(ModeIdsKey).IsNode(SrcNId.GetVal2())) { Graphs.GetDat(ModeIdsKey).AddNode(SrcNId.GetVal2()); } if (!Graphs.GetDat(ModeIdsKey).IsNode(DstNId.GetVal2())) { Graphs.GetDat(ModeIdsKey).AddNode(DstNId.GetVal2()); } if (Graphs.GetDat(ModeIdsKey).IsEdge(SrcNId.GetVal2(), DstNId.GetVal2())) { return -2; // Edge already exists } NEdges++; return Graphs.GetDat(ModeIdsKey).AddEdge(SrcNId.GetVal2(), DstNId.GetVal2()); }