LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
LangCodeHelpers.h
Go to the documentation of this file.
1// Copyright 2002-2021 CEA LIST
2// SPDX-FileCopyrightText: 2022 CEA LIST <gael.de-chalendar@cea.fr>
3//
4// SPDX-License-Identifier: MIT
5
6#ifndef LIMA_TENSORFLOWUNITS_LANGCODEHELPERS_H
7#define LIMA_TENSORFLOWUNITS_LANGCODEHELPERS_H
8
9namespace Lima
10{
11namespace LinguisticProcessing
12{
13
14// lang_str must indicate annotation model (only ud is supported now)
15// udlang must indicate the particular language code (afr, fra, eng, ...)
16// This function parses several different representations of the data mentioned above:
17// ud-fra, fra
18
19bool fix_lang_codes(QString &lang_str, std::string &udlang)
20{
21 // Registered UD sub-media (e.g. "ud-eng"): the deeplima models live under the
22 // "ud" annotation dir and the treebank is selected by an explicit udlang
23 // (e.g. "eng-UD_English-EWT"). Normalize lang_str to "ud" and keep the
24 // treebank code, stripping a leading "ud-" if the caller included it.
25 if (lang_str.startsWith("ud-") && udlang.size() > 0)
26 {
27 lang_str = "ud";
28 if (udlang.find("ud-") == 0)
29 udlang = udlang.substr(3);
30 return true;
31 }
32
33 if (lang_str != QString("ud") || udlang.find("ud-") == 0)
34 {
35 if (udlang.size() == 0 && lang_str.size() > 0 && lang_str != QString("ud"))
36 {
37 // This block helps to use tokenizer in non-UD pipelines.
38 if (lang_str.startsWith("ud-"))
39 {
40 udlang = lang_str.right(lang_str.size() - 3).toStdString();
41 }
42 else
43 {
44 udlang = lang_str.toStdString();
45 }
46 lang_str = "ud";
47 }
48 else if (udlang.size() >= 4 && udlang.find(lang_str.toStdString()) == 0 && udlang[lang_str.size()] == '-')
49 {
50 udlang = udlang.substr(3);
51 }
52 else
53 {
54 // parse lang codes like 'eng.ud'
55 if (udlang.size() == 0 && lang_str.size() >= 4 && lang_str.indexOf(".ud") == lang_str.size() - 3)
56 {
57 udlang = lang_str.left(3).toStdString();
58 lang_str = "ud";
59 }
60 else
61 {
62 return false;
63 }
64 }
65 }
66 return true;
67}
68
69} // namespace LinguisticProcessing
70} // namespace Lima
71
72#endif
bool fix_lang_codes(QString &lang_str, std::string &udlang)
NAUTITIA.