Blame view

morfeusz/MorphInterpretation.cpp 3.05 KB
Michał Lenart authored
1
2
3
4
5
6
7
8
/* 
 * File:   MorphInterpretation.cpp
 * Author: mlenart
 * 
 * Created on November 14, 2013, 11:47 AM
 */

#include <string>
Michał Lenart authored
9
#include <sstream>
Michał Lenart authored
10
11
#include "morfeusz2.h"
#include "Environment.hpp"
Michał Lenart authored
12
#include "const.hpp"
Michał Lenart authored
13
14
15

using namespace std;
Michał Lenart authored
16
17
namespace morfeusz {
Michał Lenart authored
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
    /**
     * used for ignotium and whitespace tags who don't have any qualifiers.
     */
    static vector<string> emptyQualifiers;

    MorphInterpretation::MorphInterpretation(
            int startNode,
            int endNode,
            const string& orth,
            const string& lemma,
            int tagnum,
            int namenum,
            const vector<string>* qualifiers,
            const Tagset<string>* tagset)
    : startNode(startNode),
    endNode(endNode),
    orth(orth),
    lemma(lemma),
    tagnum(tagnum),
    namenum(namenum),
    qualifiers(qualifiers),
    tagset(tagset) {
    }
Michał Lenart authored
41
Michał Lenart authored
42
43
44
45
46
47
48
49
50
    MorphInterpretation::MorphInterpretation()
    : startNode(),
    endNode(),
    orth(),
    lemma(),
    tagnum(),
    namenum(),
    qualifiers(&emptyQualifiers),
    tagset(NULL) {
Michał Lenart authored
51
Michał Lenart authored
52
    }
Michał Lenart authored
53
Michał Lenart authored
54
55
56
    MorphInterpretation MorphInterpretation::createIgn(int startNode, int endNode, const std::string& orth, const Tagset<string>& tagset) {
        MorphInterpretation mi(startNode, endNode, orth, orth, 0, 0, &emptyQualifiers, &tagset);
        return mi;
Michał Lenart authored
57
    }
Michał Lenart authored
58
59
60
61

    MorphInterpretation MorphInterpretation::createWhitespace(int startNode, int endNode, const std::string& orth, const Tagset<string>& tagset) {
        MorphInterpretation mi(startNode, endNode, orth, orth, 1, 0, &emptyQualifiers, &tagset);
        return mi;
Michał Lenart authored
62
    }
Michał Lenart authored
63
Michał Lenart authored
64
65
66
67
68
69
70
71
    static inline bool hasEnding(const string &fullString, const string &ending) {
        if (fullString.length() >= ending.length()) {
            return (0 == fullString.compare(fullString.length() - ending.length(), ending.length(), ending));
        }
        else {
            return false;
        }
    }
Michał Lenart authored
72
Michał Lenart authored
73
74
75
76
77
78
    bool MorphInterpretation::hasHomonym(const string& homonymId) const {
        size_t homonymSeparatorIdx = this->lemma.length() - homonymId.length() - 1;
        return homonymSeparatorIdx > 0
                && this->lemma[homonymSeparatorIdx] == HOMONYM_SEPARATOR
                && hasEnding(this->lemma, homonymId);
    }
Michał Lenart authored
79
Michał Lenart authored
80
81
82
83
84
85
86
    static inline string getQualifiersStr(const MorphInterpretation& mi) {
        string res;
        for (unsigned int i = 0; i < mi.getQualifiers().size(); i++) {
            res += mi.getQualifiers()[i];
            if (i + 1 < mi.getQualifiers().size()) {
                res += "|";
            }
Michał Lenart authored
87
        }
Michał Lenart authored
88
        return res;
Michał Lenart authored
89
90
    }
Michał Lenart authored
91
92
93
94
95
96
    std::string MorphInterpretation::toString(bool includeNodeNumbers) const {
        std::stringstream res;
        if (includeNodeNumbers) {
            res << startNode << "," << endNode << ",";
        }
        res << orth << ",";
Michał Lenart authored
97
Michał Lenart authored
98
99
        res << lemma;
        res << ",";
Michał Lenart authored
100
Michał Lenart authored
101
102
103
104
105
106
107
108
        res << getTag();
        if (!getName().empty()) {
            res << "," << getName();
        }
        if (!getQualifiers().empty()) {
            res << "," << getQualifiersStr(*this);
        }
        return res.str();
Michał Lenart authored
109
    }
Michał Lenart authored
110
111

}