6#ifndef DEEPLIMA_DUMPER_CONLLU_H
7#define DEEPLIMA_DUMPER_CONLLU_H
60 oss << token.
id <<
"-" << token.
mwt_last <<
"\t"
62 <<
"\t_\t_\t_\t_\t_\t_\t_\t_" << std::endl;
64 oss << token.
id <<
"\t"
66 << token.
lemma <<
"\t"
69 << token.
feats <<
"\t"
73 << token.
misc << std::endl << std::flush;
79std::ostream&
operator<< (std::ostream& out,
const std::vector<T>& v)
84 std::copy (v.begin(), v.end(), std::ostream_iterator<T>(out,
", "));
90bool dfs(
int v, std::vector<uint32_t>& heads, std::vector<int>& color,
91 uint32_t& cycle_start, uint32_t& cycle_end)
99 if (u == 0 || u >= heads.size())
106 if (
dfs(u, heads, color, cycle_start, cycle_end))
108 }
else if (color[u] == 1) {
121 uint32_t n = heads.size();
122 std::vector<int> color;
123 uint32_t cycle_start = std::numeric_limits<uint32_t>::max();
124 uint32_t cycle_end = 0;
127 for (uint32_t v = 1; v < n; v++)
129 if (v == root)
continue;
130 if (color[v] == 0 &&
dfs(v, heads, color, cycle_start, cycle_end))
134 if (cycle_start == std::numeric_limits<uint32_t>::max() || cycle_start == root)
143 if (cycle_end > 0 && cycle_end != root && cycle_end < n)
146 heads[cycle_end] = root;
168 virtual void operator()(
const std::vector<deeplima::segmentation::token_pos>& tokens, uint32_t len) = 0;
192 virtual void operator()(
const std::vector<deeplima::segmentation::token_pos>& tokens, uint32_t len)
195 for (
size_t i = 0; i < len; i++)
197 const char* ptoken = tokens[i].m_pch;
198 std::ostringstream s;
205 for (
size_t j = 0; j < tokens[i].m_len; j++)
207 if (*ptoken ==
'\r' || *ptoken ==
'\n' || *ptoken ==
'\t')
217 std::string str = s.str();
218 if (std::string::npos == str.find_first_not_of(
' '))
223 std::cout << str <<
" ";
229 std::cout << std::endl;
247 std::cout << std::endl;
251 virtual void operator()(
const std::vector<deeplima::segmentation::token_pos>& tokens, uint32_t len)
254 for (
size_t i = 0; i < len; i++)
256 const char* ptoken = tokens[i].m_pch;
257 std::ostringstream s;
263 for (
size_t j = 0; j < tokens[i].m_len; j++)
265 if (*ptoken ==
'\r' || *ptoken ==
'\n' || *ptoken ==
'\t')
275 std::string str = s.str();
276 if (std::string::npos == str.find_first_not_of(
' '))
284 if (tokens[i].m_mwt_len > 0 &&
nullptr != tokens[i].m_mwt_surface_pch)
287 const char* psurf = tokens[i].m_mwt_surface_pch;
288 for (
size_t j = 0; j < tokens[i].m_mwt_surface_len; j++)
290 surface += (*psurf ==
'\r' || *psurf ==
'\n' || *psurf ==
'\t') ?
' ' : *psurf;
295 << surface <<
"\t_\t_\t_\t_\t_\t_\t_\t_" << std::endl;
299 std::cout <<
"\t_\t_\t_\t_\t";
302 std::cout <<
"\t_\t_\t_" << std::endl;
311 std::cout << std::endl;
375 const std::vector<std::vector<std::string>> &
getMClasses()
const {
384 void set_classes(
size_t idx,
const std::string& class_name,
const std::vector<std::string>& data)
401 if (feat_name ==
"upos" || feat_name ==
"xpos" || feat_name ==
"eos")
413 std::string feat_str;
417 if (0 != iter.token_class(i))
419 if (!feat_str.empty())
426 feat_str +=
m_classes[i][iter.token_class(i)];
447 std::vector<uint32_t> heads(
m_tokens.size()+1);
449 for (
size_t i = 1; i < heads.size(); i++)
459 for (
size_t i = 1; i < heads.size(); i++)
470 std::cout << std::endl;
474 void operator()(I& iter, uint32_t begin, uint32_t end,
bool hasDeps =
false)
492 const char* ptoken = iter.form();
493 if (std::string(ptoken) ==
"<ROOT>")
499 std::ostringstream s;
507 if (*ptoken ==
'\r' || *ptoken ==
'\n' || *ptoken ==
'\t')
517 std::string str = s.str();
518 if (std::string::npos == str.find_first_not_of(
' '))
527 token.
lemma = iter.lemma();
531 if (iter.mwt_len() > 0)
567 if ((
m_root == begin) && (iter.head() == 0))
573 else if (iter.head() >= end)
589 else if ((
m_root != begin) && (iter.head() == 0))
606 token.
head = iter.head();
609 token.
deprel = iter.deprel();
void increment_token_counter()
uint64_t get_token_counter() const
virtual void operator()(const std::vector< deeplima::segmentation::token_pos > &tokens, uint32_t len)=0
virtual ~AbstractDumper()
void set_classes(size_t idx, const std::string &class_name, const std::vector< std::string > &data)
const std::vector< std::vector< std::string > > & getMClasses() const
void increment_token_counter()
std::string generate_feats(const I &iter)
std::vector< ConllToken > m_tokens
virtual ~AnalysisToConllU()
size_t m_first_feature_to_print
std::vector< std::string > m_class_names
std::vector< std::vector< std::string > > m_classes
void operator()(I &iter, uint32_t begin, uint32_t end, bool hasDeps=false)
virtual uint64_t get_token_counter() const
uint32_t m_next_token_idx
virtual uint64_t get_token_counter() const =0
virtual ~DumperBase()=default
virtual void operator()(const std::vector< deeplima::segmentation::token_pos > &tokens, uint32_t len)
uint32_t m_next_token_idx
virtual void operator()(const std::vector< deeplima::segmentation::token_pos > &tokens, uint32_t len)
bool dfs(int v, std::vector< uint32_t > &heads, std::vector< int > &color, uint32_t &cycle_start, uint32_t &cycle_end)
std::ostream & operator<<(std::ostream &oss, const ConllToken &token)
bool find_cycle(std::vector< uint32_t > &heads, uint32_t root)
ConllToken(const ConllToken &)=default
ConllToken & operator=(const ConllToken &)=default