Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed Feb 23, 2014
1 parent 9646f9b commit 25a9e06
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 228 deletions.
224 changes: 0 additions & 224 deletions src/dep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,230 +176,6 @@ void DependencyParser::build(Tree *tree) const {
}
}

bool DependencyParser::estimate(const Tree *tree,
int src, int dst1, int dst2,
double *score) const {
DependencyParserData *data
= tree->allocator()->dependency_parser_data;
CHECK_DIE(data);

Hypothesis *hypo = data->hypothesis();
CHECK_DIE(hypo);

std::vector<int> *fp = &data->fp;
fp->clear();

CHECK_DIE(dst1 != dst2);

// distance features
const int dist1 = dst1 - src;
const int dist2 = dst2 - src;
if (dist1 == 1) {
ADD_FEATURE("DIST1:1");
} else if (dist1 >= 2 && dist1 <= 5) {
ADD_FEATURE("DIST1:2-5");
} else {
ADD_FEATURE("DIST1:6-");
}

if (dist2 == 1) {
ADD_FEATURE("DIST2:1");
} else if (dist2 >= 2 && dist2 <= 5) {
ADD_FEATURE("DIST2:2-5");
} else {
ADD_FEATURE("DIST2:6-");
}

{
ChunkInfo *chunk_info = &data->chunk_info[src];
if (chunk_info->src_static_feature.empty()) {
for (size_t i = 0; i < chunk_info->str_static_feature.size(); ++i) {
chunk_info->str_static_feature[i][0] = 'S';
ADD_FEATURE2(chunk_info->str_static_feature[i],
chunk_info->src_static_feature);
}
}
COPY_FEATURE(chunk_info->src_static_feature);
}

{
ChunkInfo *chunk_info = &data->chunk_info[dst1];
if (chunk_info->dst1_static_feature.empty()) {
for (size_t i = 0; i < chunk_info->str_static_feature.size(); ++i) {
std::string n = "D1";
n += chunk_info->str_static_feature[i];
ADD_FEATURE2(n, chunk_info->dst1_static_feature);
}
}
COPY_FEATURE(chunk_info->dst1_static_feature);
}

{
ChunkInfo *chunk_info = &data->chunk_info[dst2];
if (chunk_info->dst2_static_feature.empty()) {
for (size_t i = 0; i < chunk_info->str_static_feature.size(); ++i) {
std::string n = "D2";
n += chunk_info->str_static_feature[i];
ADD_FEATURE2(n, chunk_info->dst2_static_feature);
}
}
COPY_FEATURE(chunk_info->dst2_static_feature);
}

if (src > 0) {
ChunkInfo *chunk_info = &data->chunk_info[src - 1];
if (chunk_info->left_context_feature.empty()) {
for (size_t i = 0;
i < chunk_info->str_left_context_feature.size(); ++i) {
ADD_FEATURE2(chunk_info->str_left_context_feature[i],
chunk_info->left_context_feature);
}
}
COPY_FEATURE(chunk_info->left_context_feature);
}

if (dst1 < static_cast<int>(tree->chunk_size() - 1)) {
ChunkInfo *chunk_info = &data->chunk_info[dst1 + 1];
if (chunk_info->right1_context_feature.empty()) {
for (size_t i = 0;
i < chunk_info->str_right_context_feature.size(); ++i) {
std::string n = "R1";
n += chunk_info->str_right_context_feature[i];
ADD_FEATURE2(n, chunk_info->right1_context_feature);
}
}
COPY_FEATURE(chunk_info->right1_context_feature);
}

if (dst2 < static_cast<int>(tree->chunk_size() - 1)) {
ChunkInfo *chunk_info = &data->chunk_info[dst2 + 1];
if (chunk_info->right2_context_feature.empty()) {
for (size_t i = 0;
i < chunk_info->str_right_context_feature.size(); ++i) {
std::string n = "R2";
n += chunk_info->str_right_context_feature[i];
ADD_FEATURE2(n, chunk_info->right2_context_feature);
}
}
COPY_FEATURE(chunk_info->right2_context_feature);
}

for (size_t i = 0; i < hypo->children[src].size(); ++i) {
const int child = hypo->children[src][i];
ChunkInfo *chunk_info = &data->chunk_info[child];
if (chunk_info->src_child_feature.empty()) {
for (size_t j = 0; j < chunk_info->str_child_feature.size(); ++j) {
chunk_info->str_child_feature[j][0] = 'a';
ADD_FEATURE2(chunk_info->str_child_feature[j],
chunk_info->src_child_feature);
}
}
COPY_FEATURE(chunk_info->src_child_feature);
}

for (size_t i = 0; i < hypo->children[dst1].size(); ++i) {
const int child = hypo->children[dst1][i];
ChunkInfo *chunk_info = &data->chunk_info[child];
if (chunk_info->dst1_child_feature.empty()) {
for (size_t j = 0; j < chunk_info->str_child_feature.size(); ++j) {
std::string n = "A1";
n += chunk_info->str_child_feature[j];
ADD_FEATURE2(n, chunk_info->dst1_child_feature);
}
}
COPY_FEATURE(chunk_info->dst1_child_feature);
}

for (size_t i = 0; i < hypo->children[dst2].size(); ++i) {
const int child = hypo->children[dst2][i];
ChunkInfo *chunk_info = &data->chunk_info[child];
if (chunk_info->dst2_child_feature.empty()) {
for (size_t j = 0; j < chunk_info->str_child_feature.size(); ++j) {
std::string n = "A2";
n += chunk_info->str_child_feature[j];
ADD_FEATURE2(n, chunk_info->dst2_child_feature);
}
}
COPY_FEATURE(chunk_info->dst2_child_feature);
}

// gap features
{
int bracket_status = 0;
for (int k = src + 1; k <= dst1 - 1; ++k) {
ChunkInfo *chunk_info = &data->chunk_info[k];
for (size_t i = 0; i < chunk_info->str_gap_feature.size(); ++i) {
const char *gap_feature = chunk_info->str_gap_feature[i];
if (std::strcmp(gap_feature, "GOB:1") == 0) {
bracket_status |= 1;
} else if (std::strcmp(gap_feature, "GCB:1") == 0) {
bracket_status |= 2;
} else {
std::string n = "G1";
n += gap_feature;
ADD_FEATURE(n);
}
}
}

// bracket status
switch (bracket_status) {
case 0: ADD_FEATURE("GNB1:1"); break; // nothing
case 1: ADD_FEATURE("GOB1:1"); break; // open only
case 2: ADD_FEATURE("GCB1:1"); break; // close only
default: ADD_FEATURE("GBB1:1"); break; // both
}
}

{
int bracket_status = 0;
for (int k = src + 1; k <= dst2 - 1; ++k) {
ChunkInfo *chunk_info = &data->chunk_info[k];
for (size_t i = 0; i < chunk_info->str_gap_feature.size(); ++i) {
const char *gap_feature = chunk_info->str_gap_feature[i];
if (std::strcmp(gap_feature, "GOB:1") == 0) {
bracket_status |= 1;
} else if (std::strcmp(gap_feature, "GCB:1") == 0) {
bracket_status |= 2;
} else {
std::string n = "G2";
n += gap_feature;
ADD_FEATURE(n);
}
}
}

// bracket status
switch (bracket_status) {
case 0: ADD_FEATURE("GNB2:1"); break; // nothing
case 1: ADD_FEATURE("GOB2:1"); break; // open only
case 2: ADD_FEATURE("GCB2:1"); break; // close only
default: ADD_FEATURE("GBB2:1"); break; // both
}
}

std::sort(fp->begin(), fp->end());
fp->erase(std::unique(fp->begin(), fp->end()), fp->end());

if (action_mode() == PARSING_MODE) {
*score = svm_->classify(*fp);
return *score > 0;
} else {
const int h = tree->chunk(src)->link;
CHECK_DIE(h != -1 && h >= 0 && h < static_cast<int>(tree->size()));
CHECK_DIE(!fp->empty());
if (h == dst2) { // RIGHT
svm_->add(+1.0, *fp);
} else if (h == dst1) { // LEFT
svm_->add(-1.0, *fp);
} else {
CHECK_DIE(true) << "dst1 or dst2 must be a true head";
}
}

return false;
}

bool DependencyParser::estimate(const Tree *tree, int src, int dst,
double *score) const {
DependencyParserData *data
Expand Down
4 changes: 0 additions & 4 deletions src/dep.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,8 @@ class DependencyParser: public Analyzer {
bool estimate(const Tree *tree,
int src, int dst,
double *score) const;
bool estimate(const Tree *tree,
int src, int dst1, int dst2,
double *score) const;

scoped_ptr<SVMModelInterface> svm_;
// const char *feature_extractor_name_;
};
}
#endif

0 comments on commit 25a9e06

Please sign in to comment.