构建通用语法模式以识别介词关联名词的技术问询
Great question! Identifying which nouns (or even verbs) a preposition links to is a common NLP challenge, and you can build a generalizable system using part-of-speech (POS) tagging and dependency parsing. Let’s break this down into actionable steps that work for any sentence:
1. 先做词性标注与多词介词识别
首先要给句子里的每个词打上语法标签——比如动词(VB/VBD/VBG)、介词(IN)、名词(NN/NNS/NNP)等。这里有个关键细节:要把多词介词短语(比如in front of、to the right of)当成一个整体的介词单元,不能拆成单个词处理,它们本质上是一个完整的连接成分。
拿你的示例句子来说:
extinguish the fire in front of the table which is to the right of stool which is to the left of cone then go towards the chair
标注后会得到:
- 动词:
extinguish、go - 介词单元:
in front of、to the right of、to the left of、towards - 名词:
fire、table、stool、cone、chair
2. 用依存句法分析映射关联关系
依存句法分析会生成一棵语法树,展示每个词之间的依赖关系。对于介词,我们只需要关注两个核心关系:
- 支配词(Governor):介词短语所修饰的词(可以是名词或动词)
- 介词宾语(pobj):介词直接指向的名词
把这个逻辑套用到你的例子上:
in front of修饰fire(支配词),指向table(介词宾语)→ 关联对:(fire,table)to the right of通过代词which指代table(支配词),指向stool(介词宾语)→ 关联对:(table,stool)to the left of修饰stool(支配词),指向cone(介词宾语)→ 关联对:(stool,cone)towards修饰动词go(支配词),指向chair(介词宾语)→ 关联对:(go,chair)
3. 构建通用规则模式
你可以把上面的逻辑提炼成适用于所有句子的规则:
- 先识别所有介词单元(单词或多词)
- 对每个介词单元:
- 找到它的支配词:也就是依存树里的"父节点"(介词短语依附的核心词)
- 找到它的介词宾语:也就是依存树里标记为
pobj的子节点 - 处理代词指代:如果支配词是关系代词(比如
which),要把它替换成对应的先行词(比如which指代的table)
4. 实现思路的伪代码示例
这里提供一段简化的代码逻辑,帮你理解如何落地:
def extract_preposition_pairs(parsed_sentence): preposition_pairs = [] # 预定义多词介词列表(可根据需求扩展) multi_word_preps = {"in front of", "to the right of", "to the left of"} for token in parsed_sentence: # 检查当前词是否是多词介词的开头 prep_phrase = check_for_multiword_prep(token, multi_word_preps) if not prep_phrase and token.pos_ != "IN": continue # 获取支配词 governor = token.head # 解析关系代词的先行词 if governor.pos_ == "PRON" and governor.dep_ == "nsubj": governor = governor.head.head # 获取介词宾语 pobj = next(child for child in token.children if child.dep_ == "pobj") # 保存关联对 preposition_pairs.append( (governor.text, pobj.text, prep_phrase or token.text) ) return preposition_pairs
需要注意的边缘情况
- 多词介词的扩展:要维护一个更新的多词介词列表,或者用统计模型自动识别这类短语
- 代词消解:务必把关系代词(如
which、that)映射回它们指代的原名词 - 动词类支配词:介词不仅会依附名词,也会依附动词(比如
go towards chair),你的系统要兼容这两种情况
内容的提问来源于stack exchange,提问作者Aditya Vartak

