问题描述
有人知道如何编写自定义解析器以提取实体内部的命名实体吗?
例如,从下面的句子中,我想提取“沸腾”(为此我分配了名称“名称”),它将位于前缀实体中。
d = Sentence('Synthesis of 2,4,6-trinitrotoluene (3a).The procedure was followed
to yield a pale yellow solid (boiling point 240 °C)')
这是我编写解析器的尝试:
class BoilingPoint(BaseModel):
value = StringType()
units = StringType()
prefix = StringType()
name = StringType()
Compound.boiling_points = ListType(ModelType(BoilingPoint))`
prefix = (R(u'^b\.?p\.?$',re.I) | I(u'boiling')(u'name') + I(u'point')).add_action(join)(u'prefix')
units = (W(u'°') + Optional(R(u'^[CFK]\.?$')))(u'units').add_action(merge)
value = R(u'^\d+(\.\d+)?$')(u'value')
bp = (prefix + value + units)(u'bp')
class BpParser(BaseParser):
root = bp
def interpret(self,result,start,end):
compound = Compound(
boiling_points=[
BoilingPoint(
value=first(result.xpath('./value/text()')),units=first(result.xpath('./units/text()')),prefix = first(result.xpath('./prefix/text()')),name = first(result.xpath('./name/text()')),)
]
)
yield compound
Sentence.parsers = [BpParser()]
命令d.records.serialize()的预期输出:
[{'boiling_points': [{'value': '240','units': '°C','prefix': 'boiling point','name': 'boiling'}]}]
但是d.records.serialize()产生的是
[{'boiling_points': [{'value': '240','prefix': 'boiling point'}]}]
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)