morfeusz2analyser.h
4.33 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
Copyright (C) 2014 Radosław Warzocha, 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.
*/
#ifndef LIBMACA_MORFEUSZ2ANALYSER_H
#define LIBMACA_MORFEUSZ2ANALYSER_H
#include <utility>
#include <boost/thread/mutex.hpp>
#include <libcorpus2/tagset.h>
#include <libcorpus2/token.h>
#include <morfeusz2.h>
#include <libmaca/conv/tagsetconverter.h>
#include "morphanalyser.h"
namespace Maca {
namespace details {
struct Morfeusz2Edge;
}
class Morfeusz2Analyser : public MorphAnalyser
{
public:
/**
* Constructor for a Morfeusz analyser with a given tagset and converter.
* The tagset should be the output tagset of the converter.
*/
Morfeusz2Analyser(const Corpus2::Tagset* tagset,
Conversion::TagsetConverter* conv);
/**
* Config node constructor. Recognized keys are:
* - converter - the converter to load (from standard paths)
* - ign_tag - the tag to use when Morfeusz returns no analysis,
* defaults to "ign"
* - warn_on_ign - warn when using the ign tag, false by default
* - warn_on_fold_failure - issue a warning when folding ambiguous paths
* is unsuccesful after conversion (off by def.)
*/
Morfeusz2Analyser(const Config::Node& cfg);
/// Cloning
Morfeusz2Analyser* clone() const;
/// Destructor
~Morfeusz2Analyser();
/// MorphAnalyser override
bool process_functional(const Toki::Token &t,
boost::function<void(Corpus2::Token *)> sink);
/// Class identifier
static const char* identifier;
/// Registered flag
static bool registered;
private:
static const morfeusz::Charset charset;
bool process_complex_analysis(const Toki::Token &t,
std::vector<details::Morfeusz2Edge>& pmorf,
boost::function<void(Corpus2::Token *)>sink);
typedef std::vector< std::vector<int> > adj_list;
typedef std::pair<adj_list, adj_list> adjacency_lists;
adjacency_lists build_adjacency_lists(const Toki::Token &t,
std::vector<details::Morfeusz2Edge>& pmorf);
/// convert gathered tokens and pass them to the sink
void flush_convert(std::vector<Corpus2::Token*>& vec,
boost::function<void(Corpus2::Token *)> sink);
/// convert gethered tokens (ambiguously segmented), try folding and
/// pass the resulting tokens to the sink
void flush_convert(std::vector< std::vector<Corpus2::Token*> >& vec,
boost::function<void(Corpus2::Token *)> sink);
/// helper to create a token from a Morfeusz interpretation struct
Corpus2::Token* make_token(const Toki::Token& t,
const details::Morfeusz2Edge& m) const;
/// helper to add lexemes from a Morfeusz interp struct into a token
void morfeusz_into_token(Corpus2::Token* tt,
const details::Morfeusz2Edge& m) const;
morfeusz::Morfeusz *morfeusz_instance;
boost::mutex morfeusz_mutex;
/// the tagset converter
Conversion::TagsetConverter* conv_;
Corpus2::Tag ign_tag_;
bool warn_on_ign_;
bool warn_on_fold_failure_;
};
/**
* Exception class for signalling Morfeusz-related analysis errors
*/
class Morfeusz2Error : public MacaError
{
public:
/// Constructor
Morfeusz2Error(const std::string& error, const std::string input,
const std::vector<details::Morfeusz2Edge>& interp);
/// Destructor
~Morfeusz2Error() throw();
/// Info accessor
std::string info() const;
/// The error info and Morfeusz input during the error, if available
std::string error, input;
/// The structure returned by Morfeusz during the error, if available
std::vector<details::Morfeusz2Edge> interp;
};
namespace details {
/// Helper struct for holding preprocessed Morfeusz results
struct Morfeusz2Edge
{
explicit Morfeusz2Edge(const morfeusz::MorphInterpretation interp,
const morfeusz::Morfeusz * morf);
int node_from, node_to;
UnicodeString orth;
UnicodeString lemma;
std::string tag_string;
Corpus2::Token* token;
};
} /* end ns details */
} /* end ns Maca */
#endif // LIBMACA_MORFEUSZ2ANALYSER_H