Я пытаюсь создать собственный синтаксический анализатор для извлечения точек кипения из следующих текстов, так что текст между "точкой кипения" и "of" является необязательным.
Paragraph(u'The boiling point limit of 2,4,6-trinitrotoluene is 240 °C') # <- text 1
Paragraph(u'The boiling point of 2,4,6-trinitrotoluene is 240 °C') # <- text 2
Я пытаюсь использовать следующий префикс
prefix = (I('boiling') + I('point') + Optional(R('^\S+$')).hide() + I('of') +\ R(r'\S+') +(I('is')|I('was')).hide() )(u'prefix').add_action(join)
Но это не работает для текста 2, когда нет текста между «точкой кипения» и «of».
Кто-нибудь знает, как это исправить в ChemDataExtractor ?
Полный код указан ниже.
from chemdataextractor.model import BaseModel, StringType, ListType, ModelType
class BoilingPoint(BaseModel):
prefix = StringType()
value = StringType()
units = StringType()
Compound.boiling_points = ListType(ModelType(BoilingPoint))
import re
from chemdataextractor.parse import R, I, W, Optional, merge
prefix = (I('boiling') + I('point') + Optional(R('^\S*$')).hide() + I('of') +\
R(r'\S+') +(I('is')|I('was')).hide() )(u'prefix').add_action(join)
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')
from chemdataextractor.parse.base import BaseParser
from chemdataextractor.utils import first
class BpParser(BaseParser):
root = bp
def interpret(self, result, start, end):
compound = Compound(
boiling_points=[
BoilingPoint(
prefix=first(result.xpath('./prefix/text()')),
value=first(result.xpath('./value/text()')),
units=first(result.xpath('./units/text()'))
)
]
)
yield compound
Paragraph.parsers = [BpParser()]
d = Document(
Heading(u'Synthesis of (3a)'),
# Paragraph(u'The boiling point limit of 2,4,6-trinitrotoluene is 240 °C') # <- text 1
Paragraph(u'The boiling point of 2,4,6-trinitrotoluene is 240 °C') # <- text 2
)
d.records.serialize()