Skip to content

Commit

Permalink
SWITCHPARENT (#160)
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-martian authored Jan 30, 2025
1 parent 88f34d5 commit 9f3d751
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Makefile
/src/cg-mwesplit
/src/cg-proc
/src/cg-relabel
/src/cg-annotate
/src/cg-merge-annotations
/src/CMakeFiles
/src/libcg3.1.dylib
/src/libcg3.dylib
Expand Down
2 changes: 1 addition & 1 deletion emacs/cg.el
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ Don't change without re-evaluating `cg-kw-re' (or all of cg.el).")
"MAP" "ADD"
"UNMAP"
"SELECT" "REMOVE"
"SETPARENT" "SETCHILD" "REMPARENT"
"SETPARENT" "SETCHILD" "REMPARENT" "SWITCHPARENT"
"ADDRELATION" "REMRELATION" "SETRELATION"
"ADDRELATIONS" "REMRELATIONS" "SETRELATIONS"
"SETVARIABLE" "REMVARIABLE"
Expand Down
40 changes: 39 additions & 1 deletion manual/dependencies.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
<section id="dep-remparent">
<title>REMPARENT</title>
<indexterm>
<primary>SETCHILD</primary>
<primary>REMPARENT</primary>
</indexterm>
<screen>
[wordform] REMPARENT &lt;target&gt; [contextual_tests] ;
Expand All @@ -75,6 +75,44 @@
</screen>
</section>

<section id="dep-switchparent">
<title>SWITCHPARENT</title>
<indexterm>
<primary>SWITCHPARENT</primary>
</indexterm>
<screen>
[wordform] SWITCHPARENT [WITHCHILD &lt;child_set&gt;] &lt;target&gt; [contextual_tests] ;
</screen>
<para>
Sets the grandparent of the target as its parent and sets the target as the parent of the previous parent. If <link linkend="rule-options-withchild">WTIHCHILD</link> is present, any siblings of the target which match the set will also become children of the target.
</para>
<screen>
"&lt;and&gt;"
"and" CCONJ #1->2
"&lt;in&gt;"
"in" ADP #2->0
"&lt;the&gt;"
"the" DET #3->4
"&lt;house&gt;"
"house" NOUN #4->2
"&lt;.&gt;"
"." PUNCT #5->2

SWITCHPARENT WITHCHILD (*) - (PUNCT) (NOUN) IF (p (ADP)) ;

"&lt;and&gt;"
"and" CCONJ #1->4
"&lt;in&gt;"
"in" ADP #2->4
"&lt;the&gt;"
"the" DET #3->4
"&lt;house&gt;"
"house" NOUN #4->0
"&lt;.&gt;"
"." PUNCT #5->2
</screen>
</section>

<section id="dep-input">
<title>Existing Trees in Input</title>
<para>
Expand Down
1 change: 1 addition & 0 deletions manual/rules.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
SETCHILD &lt;target&gt; [contextual_tests]
TO|FROM &lt;contextual_target&gt; [contextual_tests] ;
REMPARENT &lt;target&gt; [contextual_tests] ;
SWITCHPARENT [WITHCHILD &lt;child_set&gt;] &lt;target&gt; [contextual_tests] ;

Relation manipulation:
ADDRELATION &lt;name&gt; &lt;target&gt; [contextual_tests]
Expand Down
31 changes: 30 additions & 1 deletion src/GrammarApplicator_runRules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ bool GrammarApplicator::runSingleRule(SingleWindow& current, const Rule& rule, R
}

// If this is REMPARENT and there's no parent, skip it.
if (type == K_REMPARENT && cohort->dep_parent == DEP_NO_PARENT) {
if ((type == K_REMPARENT || type == K_SWITCHPARENT) && cohort->dep_parent == DEP_NO_PARENT) {
continue;
}

Expand Down Expand Up @@ -2632,6 +2632,35 @@ uint32_t GrammarApplicator::runRulesOnSingleWindow(SingleWindow& current, const
TRACE;
get_apply_to().cohort->dep_parent = DEP_NO_PARENT;
}
else if (rule->type == K_SWITCHPARENT) {
// this is a per-cohort rule
finish_reading_loop = false;
TRACE;

// collect cohorts
Cohort* child = get_apply_to().cohort;
Cohort* parent = current.parent->cohort_map[child->dep_parent];
auto grandparent_number = parent->dep_parent;
CohortSet siblings;
collect_subtree(siblings, parent, rule->childset1);

// clear dependencies
child->dep_parent = DEP_NO_PARENT;
parent->dep_parent = DEP_NO_PARENT;
for (auto s : siblings) {
s->dep_parent = DEP_NO_PARENT;
}

// reattach
auto it = current.parent->cohort_map.find(grandparent_number);
if (it != current.parent->cohort_map.end()) {
attachParentChild(*(it->second), *child);
}
attachParentChild(*child, *parent);
for (auto s : siblings) {
attachParentChild(*child, *s);
}
}
else if (rule->type == K_MOVE_AFTER || rule->type == K_MOVE_BEFORE || rule->type == K_SWITCH) {
// this is a per-cohort rule
finish_reading_loop = false;
Expand Down
2 changes: 2 additions & 0 deletions src/Strings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ enum KEYWORDS : uint32_t {
K_CMDARGS_OVERRIDE,
K_COPYCOHORT,
K_REMPARENT,
K_SWITCHPARENT,
KEYWORD_COUNT,
};

Expand Down Expand Up @@ -263,6 +264,7 @@ constexpr UStringView keywords[KEYWORD_COUNT] = {
u"CMDARGS-OVERRIDE",
u"COPYCOHORT",
u"REMPARENT",
u"SWITCHPARENT",
};

constexpr UStringView stringbits[] = {
Expand Down
4 changes: 4 additions & 0 deletions src/TextualParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1691,6 +1691,10 @@ bool TextualParser::maybeParseRule(UChar*& p) {
else if (IS_ICASE(p, "REMPARENT", "remparent")) {
parseRule(p, K_REMPARENT);
}
// SWITCHPARENT
else if (IS_ICASE(p, "SWITCHPARENT", "switchparent")) {
parseRule(p, K_SWITCHPARENT);
}
// RESTORE
else if (IS_ICASE(p, "RESTORE", "restore")) {
parseRule(p, K_RESTORE);
Expand Down
36 changes: 36 additions & 0 deletions test/T_SwitchParent/expected.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"<and>"
"and" CCONJ #1->4
"<in>"
"in" ADP #2->4
"<the>"
"the" DET #3->4
"<house>"
"house" NOUN #4->0
"<.>"
"." PUNCT #5->2

"<there>"
"there" PRON #1->0
"<and>"
"and" CCONJ #2->5
"<in>"
"in" ADP #3->5
"<the>"
"the" DET #4->5
"<house>"
"house" NOUN #5->1
"<.>"
"." PUNCT #6->3

"<there>"
"there" PRON #1->0
"<and>"
"and" CCONJ #2->5
"<in>"
"in" ADP #3->5
"<the>"
"the" DET #4->5
"<house>"
"house" NOUN #5->5
"<.>"
"." PUNCT #6->3
4 changes: 4 additions & 0 deletions test/T_SwitchParent/grammar.cg3
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CMDARGS += -D ;
DELIMITERS = "<.>" ;

SWITCHPARENT WITHCHILD (*) - (PUNCT) (NOUN) IF (p (ADP)) ;
36 changes: 36 additions & 0 deletions test/T_SwitchParent/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"<and>"
"and" CCONJ #1->2
"<in>"
"in" ADP #2->0
"<the>"
"the" DET #3->4
"<house>"
"house" NOUN #4->2
"<.>"
"." PUNCT #5->2

"<there>"
"there" PRON #1->0
"<and>"
"and" CCONJ #2->3
"<in>"
"in" ADP #3->1
"<the>"
"the" DET #4->5
"<house>"
"house" NOUN #5->3
"<.>"
"." PUNCT #6->3

"<there>"
"there" PRON #1->0
"<and>"
"and" CCONJ #2->3
"<in>"
"in" ADP #3->3
"<the>"
"the" DET #4->5
"<house>"
"house" NOUN #5->3
"<.>"
"." PUNCT #6->3

0 comments on commit 9f3d751

Please sign in to comment.