FlexionGraph.cpp
5.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <string>
#include <cassert>
#include <climits>
#include <vector>
#include "utils.hpp"
#include "FlexionGraph.hpp"
using namespace std;
static inline void debugPath(const std::vector<InterpretedChunk>& path) {
DEBUG("PATH:");
for (const InterpretedChunk& chunk : path) {
std::string text(chunk.chunkStartPtr);
DEBUG(text);
DEBUG(to_string(chunk.lowercaseCodepoints.size()));
}
}
static void debugGraph(const vector< vector<FlexionGraph::Edge> >& graph) {
DEBUG("GRAPH:");
for (unsigned int i = 0; i < graph.size(); i++) {
for (const FlexionGraph::Edge& e : graph[i]) {
cerr << i << " -> " << e.nextNode << " type=" << e.chunk.interpsGroup.type << endl;
}
}
}
void FlexionGraph::addStartEdge(const Edge& e) {
if (this->graph.empty()) {
assert(this->node2ChunkStartPtr.empty());
this->graph.push_back(vector<Edge>());
this->node2ChunkStartPtr.push_back(e.chunk.chunkStartPtr);
}
assert(this->node2ChunkStartPtr[0] == e.chunk.chunkStartPtr);
this->graph[0].push_back(e);
}
void FlexionGraph::addMiddleEdge(unsigned int startNode, const Edge& e) {
assert(startNode < e.nextNode);
assert(startNode == this->graph.size());
if (startNode == this->graph.size()) {
this->graph.push_back(vector<Edge>());
this->node2ChunkStartPtr.push_back(e.chunk.chunkStartPtr);
}
this->graph[startNode].push_back(e);
}
void FlexionGraph::addPath(const std::vector<InterpretedChunk>& path) {
// debugPath(path);
// debugGraph(this->graph);
for (const InterpretedChunk& chunk : path) {
if (&chunk == &(path.front())
&& &chunk == &(path.back())) {
Edge e = {chunk, UINT_MAX};
this->addStartEdge(e);
} else if (&chunk == &(path.front())) {
Edge e = {chunk, this->graph.empty() ? 1 : (unsigned int) this->graph.size()};
this->addStartEdge(e);
} else if (&chunk == &(path.back())) {
Edge e = {chunk, UINT_MAX};
this->addMiddleEdge((unsigned int) this->graph.size(), e);
} else {
Edge e = {chunk, (int) this->graph.size() + 1};
this->addMiddleEdge((unsigned int) this->graph.size(), e);
}
}
}
bool FlexionGraph::canMergeNodes(unsigned int node1, unsigned int node2) {
return this->node2ChunkStartPtr[node1] == this->node2ChunkStartPtr[node2]
&& this->getPossiblePaths(node1) == this->getPossiblePaths(node2);
}
set<FlexionGraph::Path> FlexionGraph::getPossiblePaths(unsigned int node) {
if (node == UINT_MAX || node == this->graph.size() - 1) {
return set<FlexionGraph::Path>();
} else {
set<FlexionGraph::Path> res;
for (Edge& e : this->graph.at(node)) {
FlexionGraph::PathElement pathElem(e.chunk.chunkStartPtr, e.chunk.interpsGroup.type);
set<Path> nextPaths = e.nextNode == this->graph.size()
? set<Path>()
: this->getPossiblePaths(e.nextNode);
for (Path path : nextPaths) {
path.insert(pathElem);
}
res.insert(nextPaths.begin(), nextPaths.end());
}
return res;
}
}
static bool containsEqualEdge(const vector<FlexionGraph::Edge>& edges, const FlexionGraph::Edge& e) {
for (FlexionGraph::Edge e1 : edges) {
if (e1.chunk.chunkStartPtr == e.chunk.chunkStartPtr
&& e1.chunk.lowercaseCodepoints == e.chunk.lowercaseCodepoints
&& e1.chunk.interpsGroup.type == e.chunk.interpsGroup.type
&& e1.nextNode == e.nextNode) {
return true;
}
}
return false;
}
void FlexionGraph::redirectEdges(unsigned int fromNode, unsigned int toNode) {
for (vector<Edge>& edges : this->graph) {
auto edgesIt = edges.begin();
while (edgesIt != edges.end()) {
Edge& e = *edgesIt;
if (e.nextNode == fromNode) {
Edge newEdge = {e.chunk, toNode};
if (!containsEqualEdge(edges, newEdge)) {
e.nextNode = toNode;
}
edges.erase(edgesIt);
}
else {
++edgesIt;
}
}
}
}
void FlexionGraph::doRemoveNode(unsigned int node) {
DEBUG("REMOVE "+to_string(node));
for (unsigned int i = node + 1; i < this->graph.size(); i++) {
redirectEdges(i, i - 1);
this->graph[i - 1] = this->graph[i];
}
this->graph.pop_back();
}
void FlexionGraph::doMergeNodes(unsigned int node1, unsigned int node2) {
// DEBUG("MERGE "+to_string(node1) + " " + to_string(node2));
if (node1 > node2) {
doMergeNodes(node2, node1);
} else {
// node1 < node2
for (Edge& e : this->graph[node2]) {
if (!containsEqualEdge(graph[node1], e)) {
this->graph[node1].push_back(e);
}
}
// DEBUG("1");
// debugGraph(this->graph);
this->redirectEdges(node2, node1);
// DEBUG("2");
// debugGraph(this->graph);
this->doRemoveNode(node2);
// DEBUG("3");
// debugGraph(this->graph);
}
}
bool FlexionGraph::tryToMergeTwoNodes() {
for (unsigned int node1 = 0; node1 < this->graph.size(); node1++) {
for (unsigned int node2 = 0; node2 < node1; node2++) {
if (this->canMergeNodes(node1, node2)) {
this->doMergeNodes(node1, node2);
return true;
}
}
}
return false;
}
void FlexionGraph::minimizeGraph() {
if (this->graph.size() > 2) {
// debugGraph(this->graph);
while (this->tryToMergeTwoNodes()) {
// debugGraph(this->graph);
}
}
}
bool FlexionGraph::empty() const {
return this->graph.empty();
}
void FlexionGraph::repairLastNodeNumbers() {
for (vector<Edge>& edges : this->graph) {
for (Edge& e : edges) {
if (e.nextNode == UINT_MAX) {
e.nextNode = this->graph.size();
}
}
}
}