Repository navigation
Expand file tree
/
Copy pathtext.h
More file actions
190 lines (167 loc) · 5.46 KB
/
Copy pathtext.h
File metadata and controls
190 lines (167 loc) · 5.46 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright 2012 Florian Petran
// representations for texts, and word types and tokens
#ifndef TEXT_H_
#define TEXT_H_
#include<string>
#include<fstream>
#include<list>
#include<vector>
#include<map>
#include"string_impl.h"
#include"params.h"
namespace Align {
class Sequence;
class Text;
/// Base class for WordToken and WordType.
/** Takes care of the storage and access
* of the Text ptr via the derived class.
**/
class Word {
public:
const Text& get_text() const;
virtual ~Word() = default;
Word() = delete;
protected:
explicit Word(const Text*);
const Text* _text;
};
class WordType;
/// A specific word at a specific position in the Text.
/** Provides a check for closeness with another WordToken,
* and a the methods to remove them from a Sequence, or
* add them to a Sequence.
*
* Also provides access to
* - the WordType, if needed.
* - the string realization of the WordToken.
* - All Sequence objects this WordToken is part of.
*
* Since Sequence list, and string realization are both
* pointers, only Text class is allowed to construct
* WordToken objects, to ensure consistency and prevent
* leaks.
**/
class WordToken : public Word {
friend class Text;
public:
bool operator==(const WordToken&) const;
bool close_to(const WordToken& other) const;
void remove_from(const Sequence* seq) const;
inline const string_impl& get_str() const {
return *_string_realization;
}
operator string_impl() {
return *_string_realization;
}
inline int position() const {
return _position;
}
inline bool operator!=(const WordToken& other) const {
return !(*this == other);
}
inline bool operator<(const WordToken& other) const {
return this->_position < other._position;
}
inline const WordType& get_type() const {
return *_type;
}
inline void add_to_sequence(Sequence* seq) const {
_sequences->push_back(seq);
}
inline std::list<Sequence*>* get_sequences() const {
return _sequences;
}
protected:
WordToken(const Text* txt,
std::list<Sequence*>* seqs,
const string_impl* str,
int pos,
const WordType* type);
WordToken();
private:
int _position = 0;
const WordType* _type;
const string_impl* _string_realization;
mutable std::list<Sequence*> *_sequences;
};
/// A set of WordToken in a particular text.
/** Note that the WordToken objects do not necessarily have the
* same string realization, therefore we can't access that here
* directly.
*
* Provide access to
* - frequency of its tokens (by amount of tokens associated with it).
* - all WordToken objects associated with this.
**/
class WordType : public Word {
friend class Text;
public:
inline const std::list<WordToken>& get_tokens() const {
return _tokens;
}
inline int frequency() const {
return _frequency;
}
inline bool operator<(const WordType& other) const {
// this may be a problem. what if a type doesn't have a
// token yet? XXX
// the reason for the comparison operator is inserting
// in maps as keys, which currently only happens in
// dictionaries, so for now, it should be okay. needs
// revising though.
return this->_tokens.front() < other._tokens.front();
}
/// test if ANY of the associated WordToken string
/// representations match
bool operator==(const WordType&) const;
protected:
explicit WordType(const Text*);
const WordType& add_token(const WordToken&);
private:
std::list<WordToken> _tokens;
int _frequency;
};
/// Represents a Text as container.
/** Provide sequential and random access to WordToken and WordType
* objects.
* Also owns the pointers to the string realizations of WordToken.
* For this reason, the dtor can only be called by the Dictionary
* objects associated with this Text.
**/
class Text : private std::vector<WordToken> {
friend class Dictionary;
friend class DictionaryFactory;
public:
Text() = delete;
Text(const Text&) = delete;
const Text& operator=(const Text&) = delete;
const WordToken& operator[](int pos) const;
const WordToken& at(int pos) const;
inline int length() const {
return _length;
}
inline const std::string& filename() const {
return _fname;
}
/// access is provided via a const_iterator, since
/// we don't expect to change the text at any point.
typedef std::vector<WordToken>::const_iterator iterator;
inline Text::iterator begin() const {
return std::vector<WordToken>::cbegin();
}
inline Text::iterator end() const {
return std::vector<WordToken>::cend();
}
protected:
explicit Text(const std::string&);
~Text();
std::map<string_impl, WordType*> _types;
private:
void open(const std::string&);
std::string _fname;
/// these are copied between tokens, but owned by Text
std::map<string_impl, string_impl*> string_ptrs;
int _length;
};
} // namespace Align
#endif // TEXT_H_