SEMGREX规则优化求助:实现多AMOD成分匹配同一adj标签
Great question! The issue with your original rule is that ?>amod only matches zero or one amod dependency arc, which is why it’s only picking up one of your modifiers. To capture multiple amod modifiers (like both distinct and electromagnetic for field), we need to adjust the pattern to allow repeated amod arcs.
Modified SEMGREX Rule
Here’s the updated rule that will capture all direct amod modifiers of your target noun:
((( {pos:/NN.*/}=target ?>compound {pos:/NN.*/}=compl ) (amod {pos:/JJ.*|VB.*/}=adj)* ) ?>/nmod.*/ {pos:/NN.*/}=adjn ) >det {word:/a|an/}=art
Key Changes Explained
- Replaced
?>amod {pos:/JJ.*|VB.*/}=adjwith(amod {pos:/JJ.*|VB.*/}=adj)*:- The parentheses
()group the amod arc and its node pattern together, so the quantifier applies to the entire modifier pattern. - The
*quantifier means "zero or more instances"—this preserves the optional behavior of the original rule but allows multiple amod modifiers to be matched. - SEMGREX automatically collects all matching modifier nodes under the
adjlabel when the pattern repeats, so bothdistinctandelectromagneticwill be included here.
- The parentheses
Handling Indirect Modifiers (If Needed)
If your dependency parse has modifiers in a chain (e.g., distinct modifies electromagnetic, which in turn modifies field via a compound arc), you can expand the rule to capture those too by including compound arcs in the modifier pattern:
((( {pos:/NN.*/}=target ?>compound {pos:/NN.*/}=compl ) ((amod|compound) {pos:/JJ.*|NN.*|VB.*/}=adj)* ) ?>/nmod.*/ {pos:/NN.*/}=adjn ) >det {word:/a|an/}=art
- Added
|compoundto the arc pattern to include compound modifiers alongside amods. - Updated the POS regex to
/JJ.*|NN.*|VB.*/to cover nouns used as modifiers (in caseelectromagneticis tagged as NN instead of JJ).
Testing the Rule
For your sentence fragment a distinct electromagnetic field, this rule will:
- Set
targettofield - Capture both
distinctandelectromagneticunder theadjlabel - Keep matching the other components (
artasa, etc.) as before
内容的提问来源于stack exchange,提问作者Oleg

