Skip to content

Commit

Permalink
ENH Consecutive siblings and co-parents.
Browse files Browse the repository at this point in the history
  • Loading branch information
andre-martins committed Mar 13, 2014
1 parent 3bb3ea1 commit 4e54b29
Show file tree
Hide file tree
Showing 8 changed files with 295 additions and 37 deletions.
1 change: 1 addition & 0 deletions src/semantic_parser/SemanticFeatureTemplates.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ struct SemanticFeatureTemplateParts {
ALLSIBL,
GRANDPAR,
COPAR,
CONSECUTIVECOPAR,
NEXTSIBL_M_S,
ALLSIBL_M_S,
GRANDPAR_G_M,
Expand Down
46 changes: 33 additions & 13 deletions src/semantic_parser/SemanticFeatures.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1053,20 +1053,17 @@ void SemanticFeatures::AddArbitraryLabeledSiblingFeatures(
second_argument, false);
}

#if 0
// Add features for consecutive siblings.
void SemanticFeatures::AddConsecutiveSiblingFeatures(
SemanticInstanceNumeric* sentence,
bool labeled,
int r,
int predicate,
int sense,
int first_argument,
int second_argument) {
AddSiblingFeatures(sentence, labeled, r, predicate, sense, first_argument,
AddSiblingFeatures(sentence, false, r, predicate, sense, first_argument,
second_argument, true);
}
#endif

// Add features for siblings.
void SemanticFeatures::AddSiblingFeatures(SemanticInstanceNumeric* sentence,
Expand Down Expand Up @@ -1248,7 +1245,7 @@ void SemanticFeatures::AddGrandparentFeatures(
int sense,
int argument) {
AddSecondOrderFeatures(sentence, r, grandparent_predicate, grandparent_sense,
predicate, sense, argument, false);
predicate, sense, argument, false, false);
}

// Add features for co-parents.
Expand All @@ -1261,7 +1258,20 @@ void SemanticFeatures::AddCoparentFeatures(
int second_sense,
int argument) {
AddSecondOrderFeatures(sentence, r, first_predicate, first_sense,
second_predicate, second_sense, argument, true);
second_predicate, second_sense, argument, true, false);
}

// Add features for co-parents.
void SemanticFeatures::AddConsecutiveCoparentFeatures(
SemanticInstanceNumeric* sentence,
int r,
int first_predicate,
int first_sense,
int second_predicate,
int second_sense,
int argument) {
AddSecondOrderFeatures(sentence, r, first_predicate, first_sense,
second_predicate, second_sense, argument, true, true);
}

// Add second-order features (grandparents or co-parents).
Expand All @@ -1273,13 +1283,20 @@ void SemanticFeatures::AddSecondOrderFeatures(
int second_predicate,
int second_sense,
int argument,
bool coparents) {
bool coparents,
bool consecutive) {
CHECK(!input_features_[r]);
BinaryFeatures *features = new BinaryFeatures;
input_features_[r] = features;

int sentence_length = sentence->size();

// Note: the first parent has p1 = -1.
bool first_parent = consecutive && (first_predicate < 0);
bool last_parent = consecutive &&
(second_predicate == sentence_length ||
second_predicate <= 0);

#if 0
if (FLAGS_use_pair_features_second_order) {
if (FLAGS_use_upper_dependencies) {
Expand Down Expand Up @@ -1384,20 +1401,23 @@ void SemanticFeatures::AddSecondOrderFeatures(
uint8_t flags = 0;

// Words/POS.
GWID = (*word_ids)[first_predicate];
HWID = (*word_ids)[second_predicate];
GWID = first_parent? TOKEN_START : (*word_ids)[first_predicate];
HWID = last_parent? TOKEN_STOP : (*word_ids)[second_predicate];
MWID = (*word_ids)[argument];
GPID = (*pos_ids)[first_predicate];
HPID = (*pos_ids)[second_predicate];
GPID = first_parent? TOKEN_START : (*pos_ids)[first_predicate];
HPID = last_parent? TOKEN_STOP : (*pos_ids)[second_predicate];
MPID = (*pos_ids)[argument];

if (coparents) {
flags = SemanticFeatureTemplateParts::COPAR;
if (consecutive) {
flags = SemanticFeatureTemplateParts::CONSECUTIVECOPAR;
} else {
flags = SemanticFeatureTemplateParts::COPAR;
}
} else {
flags = SemanticFeatureTemplateParts::GRANDPAR;
}


// Maximum is 255 feature templates.
CHECK_LT(SemanticFeatureTemplateGrandparent::COUNT, 256);

Expand Down
11 changes: 10 additions & 1 deletion src/semantic_parser/SemanticFeatures.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,23 @@ class SemanticFeatures: public Features {
int second_sense,
int argument);

void AddConsecutiveCoparentFeatures(SemanticInstanceNumeric* sentence,
int r,
int first_predicate,
int first_sense,
int second_predicate,
int second_sense,
int argument);

void AddSecondOrderFeatures(SemanticInstanceNumeric* sentence,
int r,
int first_predicate,
int first_sense,
int second_predicate,
int second_sense,
int argument,
bool coparents);
bool coparents,
bool consecutive);

#if 0
void AddGrandSiblingFeatures(SemanticInstanceNumeric* sentence,
Expand Down
15 changes: 10 additions & 5 deletions src/semantic_parser/SemanticOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ DEFINE_string(model_type, "standard",
"following pieces:"
"af enables arc-factored parts (required), "
"+as enables arbitrary sibling parts,"
"+cs enables consecutive sibling parts, "
"+gp enables grandparent parts,"
"+cp enables co-parent parts,"
"+ccp enables consecutive co-parent parts, "
"+gs enables grandsibling parts,"
"+ts enables trisibling parts,"
"+cs enables consecutive sibling parts, "
"+gs enables grand-sibling (third-order) parts,"
"+ts enables tri-sibling (third-order) parts."
"The following alias are predefined:"
Expand Down Expand Up @@ -256,9 +257,10 @@ void SemanticOptions::Initialize() {
pruner_max_arguments_ = FLAGS_pruner_max_arguments;

use_arbitrary_siblings_ = false;
use_consecutive_siblings_ = false;
use_grandparents_ = false;
use_coparents_ = false;
use_consecutive_siblings_ = false;
use_consecutive_coparents_ = false;
use_grandsiblings_ = false;
use_trisiblings_ = false;

Expand All @@ -281,15 +283,18 @@ void SemanticOptions::Initialize() {
} else if (enabled_parts[i] == "as") {
use_arbitrary_siblings_ = true;
LOG(INFO) << "Arbitrary sibling parts enabled.";
} else if (enabled_parts[i] == "cs") {
use_consecutive_siblings_ = true;
LOG(INFO) << "Consecutive sibling parts enabled.";
} else if (enabled_parts[i] == "gp") {
use_grandparents_ = true;
LOG(INFO) << "Grandparent parts enabled.";
} else if (enabled_parts[i] == "cp") {
use_coparents_ = true;
LOG(INFO) << "Co-parent parts enabled.";
} else if (enabled_parts[i] == "cs") {
use_consecutive_siblings_ = true;
LOG(INFO) << "Consecutive sibling parts enabled.";
} else if (enabled_parts[i] == "ccp") {
use_consecutive_coparents_ = true;
LOG(INFO) << "Consecutive co-parent parts enabled.";
} else if (enabled_parts[i] == "gs") {
use_grandsiblings_ = true;
LOG(INFO) << "Grandsibling parts enabled.";
Expand Down
6 changes: 4 additions & 2 deletions src/semantic_parser/SemanticOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ class SemanticOptions : public Options {
double GetPrunerMaxArguments() { return pruner_max_arguments_; }

bool use_arbitrary_siblings() { return use_arbitrary_siblings_; }
bool use_consecutive_siblings() { return use_consecutive_siblings_; }
bool use_grandparents() { return use_grandparents_; }
bool use_coparents() { return use_coparents_; }
bool use_consecutive_siblings() { return use_consecutive_siblings_; }
bool use_consecutive_coparents() { return use_consecutive_coparents_; }
bool use_grandsiblings() { return use_grandsiblings_; }
bool use_trisiblings() { return use_trisiblings_; }

Expand All @@ -86,9 +87,10 @@ class SemanticOptions : public Options {
double pruner_posterior_threshold_;
int pruner_max_arguments_;
bool use_arbitrary_siblings_;
bool use_consecutive_siblings_;
bool use_grandparents_;
bool use_coparents_;
bool use_consecutive_siblings_;
bool use_consecutive_coparents_;
bool use_grandsiblings_;
bool use_trisiblings_;
};
Expand Down
82 changes: 78 additions & 4 deletions src/semantic_parser/SemanticPart.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ enum {
SEMANTICPART_ARGUMENT,
SEMANTICPART_SIBLING,
SEMANTICPART_LABELEDSIBLING,
SEMANTICPART_CONSECUTIVESIBLING,
SEMANTICPART_GRANDPARENT,
SEMANTICPART_COPARENT,
SEMANTICPART_CONSECUTIVECOPARENT,
NUM_SEMANTICPARTS
};

Expand Down Expand Up @@ -152,6 +154,34 @@ class SemanticPartSibling : public Part {
int a2_; // Index of the second_argument.
};

class SemanticPartConsecutiveSibling : public Part {
public:
SemanticPartConsecutiveSibling() { p_ = s_ = a1_ = a2_ = -1; };
SemanticPartConsecutiveSibling(int predicate, int sense, int first_argument,
int second_argument) {
p_ = predicate;
s_ = sense;
a1_ = first_argument;
a2_ = second_argument;
}
virtual ~SemanticPartConsecutiveSibling() {};

public:
int type() { return SEMANTICPART_CONSECUTIVESIBLING; };

public:
int predicate() { return p_; };
int sense() { return s_; };
int first_argument() { return a1_; };
int second_argument() { return a2_; };

private:
int p_; // Index of the predicate.
int s_; // Index of the sense.
int a1_; // Index of the first argument (or -1 for a2_ being the first child).
int a2_; // Index of the second_argument (or -1 for a1_ being the last child).
};

class SemanticPartGrandparent : public Part {
public:
SemanticPartGrandparent() { g_ = t_ = p_ = s_ = a_ = -1; };
Expand Down Expand Up @@ -215,6 +245,38 @@ class SemanticPartCoparent : public Part {
int a_; // Index of the argument.
};

class SemanticPartConsecutiveCoparent : public Part {
public:
SemanticPartConsecutiveCoparent() { p1_ = s1_ = p2_ = s2_ = a_ = -1; };
SemanticPartConsecutiveCoparent(int first_predicate, int first_sense,
int second_predicate, int second_sense,
int argument) {
p1_ = first_predicate;
s1_ = first_sense;
p2_ = second_predicate;
s2_ = second_sense;
a_ = argument;
}
virtual ~SemanticPartConsecutiveCoparent() {};

public:
int type() { return SEMANTICPART_CONSECUTIVECOPARENT; };

public:
int first_predicate() { return p1_; };
int first_sense() { return s1_; };
int second_predicate() { return p2_; };
int second_sense() { return s2_; };
int argument() { return a_; };

private:
int p1_; // Index of the first predicate (or -1 for p2_ being the first).
int s1_; // Index of the first sense.
int p2_; // Index of the second predicate (or -1 for p1_ being the last).
int s2_; // Index of the second sense.
int a_; // Index of the argument.
};

class SemanticPartLabeledSibling : public Part {
public:
SemanticPartLabeledSibling() { p_ = s_ = a1_ = a2_ = r1_ = r2_ = -1; };
Expand Down Expand Up @@ -451,14 +513,20 @@ class SemanticParts : public Parts {
void SetOffsetSibling(int offset, int size) {
SetOffset(SEMANTICPART_SIBLING, offset, size);
};
void SetOffsetLabeledSibling(int offset, int size) {
SetOffset(SEMANTICPART_LABELEDSIBLING, offset, size);
};
void SetOffsetConsecutiveSibling(int offset, int size) {
SetOffset(SEMANTICPART_CONSECUTIVESIBLING, offset, size);
};
void SetOffsetGrandparent(int offset, int size) {
SetOffset(SEMANTICPART_GRANDPARENT, offset, size);
};
void SetOffsetCoparent(int offset, int size) {
SetOffset(SEMANTICPART_COPARENT, offset, size);
};
void SetOffsetLabeledSibling(int offset, int size) {
SetOffset(SEMANTICPART_LABELEDSIBLING, offset, size);
void SetOffsetConsecutiveCoparent(int offset, int size) {
SetOffset(SEMANTICPART_CONSECUTIVECOPARENT, offset, size);
};

void GetOffsetLabeledArc(int *offset, int *size) const {
Expand All @@ -476,14 +544,20 @@ class SemanticParts : public Parts {
void GetOffsetSibling(int *offset, int *size) const {
GetOffset(SEMANTICPART_SIBLING, offset, size);
};
void GetOffsetLabeledSibling(int *offset, int *size) const {
GetOffset(SEMANTICPART_LABELEDSIBLING, offset, size);
};
void GetOffsetConsecutiveSibling(int *offset, int *size) const {
GetOffset(SEMANTICPART_CONSECUTIVESIBLING, offset, size);
};
void GetOffsetGrandparent(int *offset, int *size) const {
GetOffset(SEMANTICPART_GRANDPARENT, offset, size);
};
void GetOffsetCoparent(int *offset, int *size) const {
GetOffset(SEMANTICPART_COPARENT, offset, size);
};
void GetOffsetLabeledSibling(int *offset, int *size) const {
GetOffset(SEMANTICPART_LABELEDSIBLING, offset, size);
void GetOffsetConsecutiveCoparent(int *offset, int *size) const {
GetOffset(SEMANTICPART_CONSECUTIVECOPARENT, offset, size);
};

private:
Expand Down
Loading

0 comments on commit 4e54b29

Please sign in to comment.