Skip to content

Commit 1bde771

Browse files
shewerhchunhuiksqsf
authored
added new function of SpansReg (#367)
* added new function of SpansReg Signed-off-by: shewer <shewer@gmail.com> * added new function of SpansReg Signed-off-by: shewer <shewer@gmail.com> * Update src/types.cc Co-authored-by: ksqsf <login@ksqsf.moe> --------- Signed-off-by: shewer <shewer@gmail.com> Co-authored-by: hchunhui <hchunhui@mail.ustc.edu.cn> Co-authored-by: ksqsf <login@ksqsf.moe>
1 parent cec9ca7 commit 1bde771

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

src/types.cc

+69
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,16 @@ namespace SegmentReg {
116116
return r.substr(t.start, t.end - t.start);
117117
}
118118

119+
inline Spans spans(const T &seg) {
120+
Spans res;
121+
if (auto phrase = As<Phrase>(
122+
Candidate::GetGenuineCandidate(seg.GetSelectedCandidate()))) {
123+
res.AddSpans(phrase->spans());
124+
}
125+
res.AddSpan(seg.start, seg.end);
126+
return res;
127+
}
128+
119129
static const luaL_Reg funcs[] = {
120130
{ "Segment", WRAP(make) },
121131
{ NULL, NULL },
@@ -129,6 +139,7 @@ namespace SegmentReg {
129139
{ "get_candidate_at", WRAPMEM(T::GetCandidateAt) },
130140
{ "get_selected_candidate", WRAPMEM(T::GetSelectedCandidate) },
131141
{ "active_text", WRAP(active_text) },
142+
{ "spans", WRAP(spans) },
132143
{ NULL, NULL },
133144
};
134145

@@ -265,6 +276,15 @@ namespace CandidateReg {
265276
return false;
266277
};
267278

279+
Spans spans(const an<Candidate> &cand) {
280+
if (auto phrase = As<Phrase>(Candidate::GetGenuineCandidate(cand))) {
281+
return phrase->spans();
282+
}
283+
Spans spans;
284+
spans.AddSpan(cand->start(), cand->end());
285+
return spans;
286+
}
287+
268288
template<class OT>
269289
an<OT> candidate_to_(an<T> t) {
270290
return std::dynamic_pointer_cast<OT>(t);
@@ -286,6 +306,7 @@ namespace CandidateReg {
286306
{ "to_phrase", WRAP(candidate_to_<Phrase>)},
287307
{ "to_sentence", WRAP(candidate_to_<Sentence>)},
288308
{ "append", WRAP(append)},
309+
{ "spans", WRAP(spans)},
289310
{ NULL, NULL },
290311
};
291312

@@ -853,6 +874,18 @@ namespace CompositionReg {
853874
return t.empty();
854875
}
855876

877+
vector<Segment> get_segments(T &t) {
878+
return t;
879+
}
880+
881+
Spans spans(const T &t) {
882+
Spans spans;
883+
for (const auto &seg : t) {
884+
spans.AddSpans( SegmentReg::spans(seg) );
885+
}
886+
return spans;
887+
}
888+
856889
static const luaL_Reg funcs[] = {
857890
{ NULL, NULL },
858891
};
@@ -866,6 +899,7 @@ namespace CompositionReg {
866899
{ "has_finished_composition", WRAPMEM(T::HasFinishedComposition) },
867900
{ "get_prompt", WRAPMEM(T::GetPrompt) },
868901
{ "toSegmentation" , WRAP(toSegmentation) },
902+
{ "spans", WRAP(spans)},
869903
{ NULL, NULL },
870904
};
871905

@@ -2061,6 +2095,37 @@ namespace SpansReg {
20612095
return spans.Count(start, end);
20622096
}
20632097

2098+
vector<size_t> get_vertices(const T &spans) {
2099+
vector<size_t> res;
2100+
size_t end = spans.end();
2101+
for (size_t stop = spans.start(); ; stop = spans.NextStop(stop)) {
2102+
if (spans.HasVertex(stop)) {
2103+
res.push_back(stop);
2104+
}
2105+
if (stop == end) {
2106+
break;
2107+
}
2108+
}
2109+
return res;
2110+
}
2111+
2112+
int raw_set_vertices(lua_State *L) {
2113+
C_State C;
2114+
auto &spans = LuaType<Spans &>::todata(L, 1);
2115+
if (lua_istable(L, 2)) {
2116+
spans.Clear();
2117+
for (auto vertex : LuaType<vector<int>>::todata(L, 2, &C)) {
2118+
if (vertex >=0) {
2119+
spans.AddVertex(vertex);
2120+
}
2121+
}
2122+
}
2123+
else {
2124+
luaL_error(L, "bad argument #2 to set_vertices (table expected, got %s)" , lua_typename(L, 2));
2125+
}
2126+
return 0;
2127+
}
2128+
20642129
static const luaL_Reg funcs[] = {
20652130
{ "Spans", WRAP(make) },
20662131
{ NULL, NULL },
@@ -2069,21 +2134,25 @@ namespace SpansReg {
20692134
static const luaL_Reg methods[] = {
20702135
{ "add_span", WRAPMEM(T, AddSpan) },
20712136
{ "add_spans", WRAPMEM(T, AddSpans) },
2137+
{ "add_vertex", WRAPMEM(T, AddVertex)},
20722138
{ "previous_stop", WRAPMEM(T, PreviousStop) },
20732139
{ "next_stop", WRAPMEM(T, NextStop) },
20742140
{ "has_vertex", WRAPMEM(T, HasVertex) },
20752141
{ "count_between", WRAP(count_between) },
2142+
{ "clear", WRAPMEM(T, Clear) },
20762143
{ NULL, NULL },
20772144
};
20782145

20792146
static const luaL_Reg vars_get[] = {
20802147
{ "_start", WRAPMEM(T, start) },
20812148
{ "_end", WRAPMEM(T, end) },
20822149
{ "count", WRAP(count) },
2150+
{ "vertices", WRAP(get_vertices)},
20832151
{ NULL, NULL },
20842152
};
20852153

20862154
static const luaL_Reg vars_set[] = {
2155+
{ "vertices", (raw_set_vertices)},
20872156
{ NULL, NULL },
20882157
};
20892158
} // namespace SpansReg

0 commit comments

Comments
 (0)