-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontainers.h
More file actions
177 lines (154 loc) · 5.37 KB
/
containers.h
File metadata and controls
177 lines (154 loc) · 5.37 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
// Copyright 2012 Florian Petran
// contains low-level containers for alignment elements
#ifndef CONTAINERS_H_
#define CONTAINERS_H_
#include<list>
#include<map>
#include<vector>
#include<iostream>
#include<string>
#include"text.h"
#include"dictionary.h"
#include"params.h"
namespace Align {
/// An aligned pair of WordTokens in e and f texts.
/** wraps a number of WordToken related functions
* such as the closeness checks, and the slots of the
* WordTokens. Also provides a convenience function
* to reverse the alignment direction.
**/
class Pair {
public:
bool targets_close(const Pair&) const;
bool both_close(const Pair&) const;
bool operator==(const Pair&) const;
inline bool operator!=(const Pair& that) const {
return !(*this == that);
};
const Pair& reverse();
const WordToken& source() const;
const WordToken& target() const;
int slot() const;
int target_slot() const;
Pair(const WordToken&, const WordToken&);
private:
WordToken _source, _target;
};
class Hypothesis;
/// An aligned sequence of pairs
class Sequence : std::list<Pair> {
friend class Hypothesis;
public:
Sequence() = delete;
/// add a pair to the sequence
void add(const Pair&);
/// add a pair if its target is close to our last target,
/// return whether it was added or not
bool add_if_close(const Pair& p);
/// merge another sequence to this
void merge(Sequence* s);
/// reverse e and f
const Sequence& reverse();
/// starting slot of the sequence
/// i.e. the position of its first target
int slot() const;
/// slot of the last pair in the sequence
/// i.e. the position of its last target
int back_slot() const;
int length() const;
/// set the confidence score of this Sequence
void set_score(const float&);
const float& get_score() const;
bool operator==(const Sequence&) const;
bool has_target(int target_position);
/// checks if a target index is already in
/// the sequence. POTENTIALLY EXPENSIVE
bool has_target(const Pair&);
const Pair& first_pair() const;
const Pair& last_pair() const;
typedef std::list<Pair>::const_iterator const_iterator;
inline Sequence::const_iterator cbegin() const {
return std::list<Pair>::cbegin();
}
inline Sequence::const_iterator cend() const {
return std::list<Pair>::cend();
}
typedef std::list<Pair>::iterator iterator;
inline Sequence::iterator begin() {
return std::list<Pair>::begin();
}
inline Sequence::iterator end() {
return std::list<Pair>::end();
}
operator std::vector<int>();
protected:
explicit Sequence(const Dictionary&);
/// construct an initial Sequence over two texts from one pair
Sequence(const Dictionary&, const Pair&);
Sequence(const Sequence&) = delete;
const Sequence& operator=(const Sequence&);
/// only hypothesis may call the dtor, because it
/// owns the pointers
~Sequence();
private:
const Dictionary* _dict;
float _score = 0.0;
int _slot = 0,
_back_slot = 0;
/// cache for _length
mutable int _length = 0;
};
class AlignMake;
/// an alignment hypothesis consisting of multiple sequences.
/** also the Sequence factory.
*
* what this class needs to do:
* - add and remove sequences
* - iterate over sequences
* - track/own sequence pointers on the heap stored for the WordToken
* - reverse e/f direction
* - merge with another hypothesis
*/
class Hypothesis : private std::list<Sequence*> {
friend class AlignMake;
public:
typedef std::list<Sequence*>::iterator iterator;
typedef std::list<Sequence*>::const_iterator const_iterator;
inline iterator begin() {
return std::list<Sequence*>::begin();
};
inline const_iterator cbegin() const {
return std::list<Sequence*>::cbegin();
};
inline iterator end() {
return std::list<Sequence*>::end();
};
inline const_iterator cend() const {
return std::list<Sequence*>::cend();
}
operator std::vector<std::vector<int>>();
Sequence* new_sequence(const Pair& p);
iterator remove_sequence(iterator pos);
iterator remove_sequence(Sequence* seq);
/// reverse the alignment direction of this
/// Hypothesis
const Hypothesis& reverse();
/// munch up another hypothesis, adding all sequence
/// to this and removing them from the other hypothesis
const Hypothesis& munch(Hypothesis* other);
protected:
explicit Hypothesis(const Dictionary& d);
~Hypothesis();
Hypothesis() = delete;
Hypothesis(const Hypothesis&) = delete;
const Hypothesis& operator=(const Hypothesis&) = delete;
inline void set_dict(const Dictionary& d) {
_dict = &d;
};
private:
const Dictionary* _dict;
};
} // namespace Align
std::ostream& operator<<(std::ostream& strm, const Align::Pair& pair);
std::ostream& operator<<(std::ostream& strm, const Align::Sequence& seq);
#endif // CONTAINERS_H_