convertinganalyser.cpp
3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
Copyright (C) 2010 Tomasz Śniatowski, Adam Radziszewski
Part of the libmaca project
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at your option)
any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.
See the LICENSE.MACA, LICENSE.SFST, LICENSE.GUESSER, COPYING.LESSER and COPYING files for more details.
*/
#include <libmaca/morph/convertinganalyser.h>
#include <libmaca/util/settings.h>
#include <fstream>
namespace Maca {
const char* ConvertingAnalyser::identifier = "wrap_convert";
bool ConvertingAnalyser::registered =
MorphAnalyser::register_analyser<ConvertingAnalyser>();
ConvertingAnalyser::ConvertingAnalyser(MorphAnalyser *ma,
Conversion::TagsetConverter *conv)
: MorphAnalyser(&conv->tagset_to()), wrapped_(ma), converter_(conv)
{
Corpus2::require_matching_tagsets(converter_->tagset_from(), *wrapped_,
"Converting analyser creation (from)");
Corpus2::require_matching_tagsets(converter_->tagset_to(), *this,
"Converting analyser creation (to)");
}
ConvertingAnalyser::ConvertingAnalyser(const Config::Node &cfg)
: MorphAnalyser(cfg), wrapped_(NULL), converter_(NULL)
{
std::string wrapped_id = cfg.get("wrapped_class", "");
Config::Node cfg2 = cfg;
cfg2.put("tagset", cfg.get("wrapped_tagset", ""));
try {
wrapped_.reset(MorphAnalyser::create(wrapped_id, cfg2));
} catch (MorphAnalyserFactoryException&) {
if (cfg.get("plugin_autoload", true)) {
if (MorphAnalyser::load_plugin(wrapped_id, false)) {
try {
wrapped_.reset(MorphAnalyser::create(wrapped_id, cfg2));
} catch (MorphAnalyserFactoryException&) {
throw MacaError("Unknown analyser type: " + wrapped_id +
" (plugin found but create failed)");
}
} else {
throw MacaError("Unknown analyser type: " + wrapped_id +
" (plugin not found)");
}
} else {
throw MacaError("Unknown analyser type: " + wrapped_id);
}
}
std::string fn = cfg.get("wrapped_converter", "");
std::ifstream ifs;
Path::Instance().open_stream_or_throw(fn, ifs, "converter");
Config::Node conv_cfg = Config::from_stream(ifs);
converter_.reset(new Conversion::TagsetConverter(conv_cfg));
Corpus2::require_matching_tagsets(converter_->tagset_from(), *wrapped_,
"Converting analyser creation (from)");
Corpus2::require_matching_tagsets(converter_->tagset_to(), *this,
"Converting analyser creation (to)");
}
ConvertingAnalyser* ConvertingAnalyser::clone() const
{
std::auto_ptr<ConvertingAnalyser> copy;
copy.reset(new ConvertingAnalyser(wrapped_->clone(), converter_->clone()));
return copy.release();
}
bool ConvertingAnalyser::process_functional(const Toki::Token &t,
boost::function<void (Corpus2::Token *)> sink)
{
std::vector<Corpus2::Token*> tv = wrapped_->process(t);
if (tv.empty()) return false;
converter_->convert_simple(tv, sink);
return true;
}
} /* end ns Maca */