Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added new function of SpansReg #367

Merged
merged 3 commits into from
Oct 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions src/types.cc
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,16 @@ namespace SegmentReg {
return r.substr(t.start, t.end - t.start);
}

inline Spans spans(const T &seg) {
Spans res;
if (auto phrase = As<Phrase>(
Candidate::GetGenuineCandidate(seg.GetSelectedCandidate()))) {
res.AddSpans(phrase->spans());
}
res.AddSpan(seg.start, seg.end);
return res;
}
Comment on lines +111 to +119
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


static const luaL_Reg funcs[] = {
{ "Segment", WRAP(make) },
{ NULL, NULL },
Expand All @@ -121,6 +131,7 @@ namespace SegmentReg {
{ "get_candidate_at", WRAPMEM(T::GetCandidateAt) },
{ "get_selected_candidate", WRAPMEM(T::GetSelectedCandidate) },
{ "active_text", WRAP(active_text) },
{ "spans", WRAP(spans) },
{ NULL, NULL },
};

Expand Down Expand Up @@ -257,6 +268,15 @@ namespace CandidateReg {
return false;
};

Spans spans(const an<Candidate> &cand) {
if (auto phrase = As<Phrase>(Candidate::GetGenuineCandidate(cand))) {
return phrase->spans();
}
Spans spans;
spans.AddSpan(cand->start(), cand->end());
return spans;
}

template<class OT>
an<OT> candidate_to_(an<T> t) {
return std::dynamic_pointer_cast<OT>(t);
Expand All @@ -278,6 +298,7 @@ namespace CandidateReg {
{ "to_phrase", WRAP(candidate_to_<Phrase>)},
{ "to_sentence", WRAP(candidate_to_<Sentence>)},
{ "append", WRAP(append)},
{ "spans", WRAP(spans)},
{ NULL, NULL },
};

Expand Down Expand Up @@ -845,6 +866,18 @@ namespace CompositionReg {
return t.empty();
}

vector<Segment> get_segments(T &t) {
return t;
}

Spans spans(const T &t) {
Spans spans;
for (const auto &seg : t) {
spans.AddSpans( SegmentReg::spans(seg) );
}
return spans;
}

static const luaL_Reg funcs[] = {
{ NULL, NULL },
};
Expand All @@ -858,6 +891,7 @@ namespace CompositionReg {
{ "has_finished_composition", WRAPMEM(T::HasFinishedComposition) },
{ "get_prompt", WRAPMEM(T::GetPrompt) },
{ "toSegmentation" , WRAP(toSegmentation) },
{ "spans", WRAP(spans)},
{ NULL, NULL },
};

Expand Down Expand Up @@ -2007,6 +2041,37 @@ namespace SpansReg {
return spans.Count(start, end);
}

vector<size_t> get_vertices(const T &spans) {
vector<size_t> res;
size_t end = spans.end();
for (size_t stop = spans.start(); ; stop = spans.NextStop(stop)) {
if (spans.HasVertex(stop)) {
res.push_back(stop);
}
if (stop == end) {
break;
}
}
return res;
}

int raw_set_vertices(lua_State *L) {
C_State C;
auto &spans = LuaType<Spans &>::todata(L, 1);
if (lua_istable(L, 2)) {
spans.Clear();
for (auto vertex : LuaType<vector<int>>::todata(L, 2, &C)) {
if (vertex >=0) {
spans.AddVertex(vertex);
}
}
}
else {
luaL_error(L, "bad argument #2 to set_vertices (table expected, got %s)" , lua_typename(L, 2));
}
return 0;
}
Comment on lines +2058 to +2073
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我对 Lua API 不熟,这部分 skip 了。


static const luaL_Reg funcs[] = {
{ "Spans", WRAP(make) },
{ NULL, NULL },
Expand All @@ -2015,21 +2080,25 @@ namespace SpansReg {
static const luaL_Reg methods[] = {
{ "add_span", WRAPMEM(T, AddSpan) },
{ "add_spans", WRAPMEM(T, AddSpans) },
{ "add_vertex", WRAPMEM(T, AddVertex)},
{ "previous_stop", WRAPMEM(T, PreviousStop) },
{ "next_stop", WRAPMEM(T, NextStop) },
{ "has_vertex", WRAPMEM(T, HasVertex) },
{ "count_between", WRAP(count_between) },
{ "clear", WRAPMEM(T, Clear) },
{ NULL, NULL },
};

static const luaL_Reg vars_get[] = {
{ "start", WRAPMEM(T, start) },
{ "end", WRAPMEM(T, end) },
{ "count", WRAP(count) },
{ "vertices", WRAP(get_vertices)},
{ NULL, NULL },
};

static const luaL_Reg vars_set[] = {
{ "vertices", (raw_set_vertices)},
{ NULL, NULL },
};
} // namespace SpansReg
Expand Down
Loading