-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalignment_heuristic.cc
More file actions
149 lines (124 loc) · 4.38 KB
/
alignment_heuristic.cc
File metadata and controls
149 lines (124 loc) · 4.38 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
#include "alignment_heuristic.h"
#include <boost/functional/hash.hpp>
#include "aligned_tree.h"
#include "translation_table.h"
#include "util.h"
typedef boost::hash<pair<int, int>> PairHasher;
typedef pair<double, pair<int, size_t>> State;
AlignmentHeuristic::AlignmentHeuristic(
shared_ptr<TranslationTable> forward_table,
shared_ptr<TranslationTable> reverse_table,
const unordered_set<int>& blacklisted_tags,
size_t max_additional_links) :
forward_table(forward_table),
reverse_table(reverse_table),
blacklisted_tags(blacklisted_tags),
max_additional_links(max_additional_links) {}
Alignment AlignmentHeuristic::GetBaseAlignment(
const AlignedTree& tree,
const Alignment& intersect_alignment) const {
vector<int> source_tags;
for (auto leaf = tree.begin_leaf(); leaf != tree.end_leaf(); ++leaf) {
source_tags.push_back(leaf->GetTag());
}
Alignment base_alignment;
for (const auto& link: intersect_alignment) {
if (!blacklisted_tags.count(source_tags[link.first])) {
base_alignment.push_back(link);
}
}
return base_alignment;
}
Alignment AlignmentHeuristic::GetAdditionalLinks(
const AlignedTree& tree,
const String& target_string,
const Alignment& gdfa_alignment,
const Alignment& base_alignment) const {
vector<int> source_words;
for (auto leaf = tree.begin_leaf(); leaf != tree.end_leaf(); ++leaf) {
source_words.push_back(leaf->GetWord());
}
vector<int> target_words;
for (auto node: target_string) {
target_words.push_back(node.GetWord());
}
unordered_set<pair<int, int>, PairHasher> base_links;
for (const auto& link: base_alignment) {
base_links.insert(link);
}
vector<pair<double, pair<int, int>>> candidates;
for (const auto& link: gdfa_alignment) {
if (!base_links.count(link)) {
double forward_prob = forward_table->GetProbability(
source_words[link.first], target_words[link.second]);
double reverse_prob = forward_table->GetProbability(
target_words[link.second], source_words[link.first]);
candidates.push_back(make_pair(forward_prob + reverse_prob, link));
}
}
sort(candidates.begin(), candidates.end(),
greater<pair<double, pair<int, int>>>());
Alignment additional_links;
for (size_t i = 0; i < candidates.size(); ++i) {
additional_links.push_back(candidates[i].second);
}
return additional_links;
}
double AlignmentHeuristic::GetInteriorNodesRatio(
const AlignedTree& tree) const {
int total_rules = 0, interior_nodes = 0;
for (auto node: tree) {
if (node.IsSplitNode()) {
++total_rules;
} else {
++interior_nodes;
}
}
return (double) interior_nodes / total_rules;
}
Alignment AlignmentHeuristic::FindBestAlignment(
const AlignedTree& parse_tree,
const String& target_string,
const Alignment& gdfa_alignment,
const Alignment& intersect_alignment) const {
Alignment base_alignment = GetBaseAlignment(parse_tree, intersect_alignment);
Alignment additional_links = GetAdditionalLinks(
parse_tree, target_string, gdfa_alignment, base_alignment);
Alignment best_alignment = base_alignment;
AlignedTree tree = parse_tree;
ConstructGHKMDerivation(tree, target_string, base_alignment);
double best_ratio = GetInteriorNodesRatio(tree);
int num_states = 0;
priority_queue<State, vector<State>, greater<State>> states;
states.push(make_pair(best_ratio, make_pair(0, 0)));
while (states.size() > 0 && num_states < MAX_STATES) {
++num_states;
auto state = states.top();
states.pop();
double current_ratio = state.first;
int encoding = state.second.first;
size_t index = state.second.second;
if (index == additional_links.size()) {
continue;
}
states.push(make_pair(current_ratio, make_pair(encoding, index + 1)));
int new_encoding = encoding | (1 << index);
Alignment alignment = base_alignment;
for (size_t i = 0; i < index; ++i) {
if (encoding & (1 << i)) {
alignment.push_back(additional_links[i]);
}
}
AlignedTree tree = parse_tree;
ConstructGHKMDerivation(tree, target_string, alignment);
double new_ratio = GetInteriorNodesRatio(tree);
if (new_ratio <= current_ratio) {
states.push(make_pair(new_ratio, make_pair(new_encoding, index + 1)));
}
if (new_ratio < best_ratio) {
best_ratio = new_ratio;
best_alignment = alignment;
}
}
return best_alignment;
}