LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
mwt_dict.cpp
Go to the documentation of this file.
1// Copyright 2021 CEA LIST
2// SPDX-FileCopyrightText: 2026 CEA LIST <gael.de-chalendar@cea.fr>
3//
4// SPDX-License-Identifier: MIT
5
6#include "mwt_dict.h"
7
8#include <fstream>
9#include <iostream>
10#include <map>
11#include <stdexcept>
12
13#include "line.h"
15
16using namespace std;
17
18namespace deeplima
19{
20namespace CoNLLU
21{
22
23size_t extract_mwt_dict(const vector<string>& conllu_files, ostream& out, uint32_t min_rule_support,
24 size_t* rules_written)
25{
26 // surface -> (expansion -> count)
27 map<string, map<vector<string>, size_t>> dict;
28
29 for (const auto& fn : conllu_files)
30 {
31 ifstream input(fn);
32 if (!input.is_open())
33 {
34 throw runtime_error("extract_mwt_dict: can't open \"" + fn + "\"");
35 }
36
37 string line;
38 while (getline(input, line))
39 {
40 if (line.empty() || line[0] == '#')
41 {
42 continue;
43 }
44
45 CoNLLULine l(line);
46 if (!l.is_token_line() || !l.idx().is_multiword())
47 {
48 continue;
49 }
50
51 const size_t n_sub = size_t(l.idx()._last - l.idx()._first + 1);
52 const string surface = l.form();
53
54 // Collect the forms of the (n_sub) real-word lines that compose this MWT.
55 // They immediately follow the range line in UD; bail out cleanly on any
56 // malformed span rather than over-consuming into the next sentence.
57 vector<string> expansion;
58 expansion.reserve(n_sub);
59 size_t collected = 0;
60 while (collected < n_sub && getline(input, line))
61 {
62 if (line.empty())
63 {
64 break; // sentence boundary reached too early: malformed span
65 }
66 if (line[0] == '#')
67 {
68 continue;
69 }
70 CoNLLULine w(line);
71 if (!w.is_token_line() || w.idx().is_empty())
72 {
73 continue;
74 }
75 if (w.idx().is_multiword())
76 {
77 break; // a nested range here means the corpus is malformed
78 }
79 expansion.push_back(w.form());
80 ++collected;
81 }
82
83 if (collected == n_sub && !surface.empty() && expansion.size() >= 2)
84 {
85 dict[surface][expansion]++;
86 }
87 }
88 }
89
90 size_t written = 0;
91 MwtLexicon lexicon;
92 for (const auto& [surface, expansions] : dict)
93 {
94 const vector<string>* best = nullptr;
95 size_t best_count = 0;
96 size_t total = 0;
97 for (const auto& [exp, count] : expansions)
98 {
99 total += count;
100 if (count > best_count)
101 {
102 best_count = count;
103 best = &exp;
104 }
105 }
106 if (nullptr == best)
107 {
108 continue;
109 }
110
111 if (expansions.size() > 1)
112 {
113 cerr << "extract_mwt_dict: ambiguous surface \"" << surface << "\" ("
114 << expansions.size() << " expansions); keeping the most frequent ("
115 << best_count << "/" << total << ")." << endl;
116 }
117
118 out << surface << "\t" << best_count;
119 for (const auto& w : *best)
120 {
121 out << "\t" << w;
122 }
123 out << "\n";
124 lexicon.add_entry(surface, *best);
125 ++written;
126 }
127
128 // Affix rules, the fallback for flagged surfaces the dictionary misses
129 const size_t n_rules = lexicon.learn_rules(min_rule_support);
130 lexicon.write_rules(out);
131 if (nullptr != rules_written)
132 {
133 *rules_written = n_rules;
134 }
135
136 return written;
137}
138
139} // namespace CoNLLU
140} // namespace deeplima
const std::string & form() const
Definition line.h:153
const idx_t & idx() const
Definition line.h:148
bool is_token_line() const
Definition line.h:128
Multiword-token (MWT) lexicon: how to expand a surface token (French "du") into its syntactic words (...
Definition mwt_lexicon.h:64
void write_rules(std::ostream &out) const
void add_entry(const std::string &surface, const std::vector< std::string > &words)
size_t learn_rules(uint32_t min_support)
size_t extract_mwt_dict(const vector< string > &conllu_files, ostream &out, uint32_t min_rule_support, size_t *rules_written)
Definition mwt_dict.cpp:23
Edit rules for lemmatizing words absent from the lemma dictionary.
STL namespace.
base_int_t _first
Definition line.h:30
bool is_multiword() const
Definition line.h:48
bool is_empty() const
Definition line.h:53
base_int_t _last
Definition line.h:31