在处理搜索时考虑XSLT(操作 XML 文档的专用语言),它会根据匹配的值重写原始 xml。
下面的 XSLT 嵌入在 Python 中,以使用finalPatterns
列表动态删除不匹配的元素。从那里,Python 可以转换(使用lxml
模块)原始文档,然后将此类输出用于您的最终使用需求。
Python脚本
import lxml.etree as ET
finalPatterns=['went \n to \n','created\n the\n', 'founded\n a\n', 'went\n yesterday\n to\n', 'a\n visit\n', 'founded\n in\n']
# BUILDING XSLT FILTER STRING
contains = ''
for p in finalPatterns:
contains += "("
for i in p.split('\n '):
contains += "contains(., '{}') and \n".format(i.replace('\n', '').strip(' '))
contains += ")"
contains = contains.replace(' and \n)', ') or ')
xslstr = '''<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output version="1.0" encoding="UTF-8" indent="yes" />
<xsl:strip-space elements="*"/>
<!-- Identity Transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- Rewrites Matching Phrase elements -->
<xsl:template match="PHRASE">
<xsl:copy>
<wholetext>
<xsl:call-template name="join">
<xsl:with-param name="valueList" select="*"/>
<xsl:with-param name="separator" select="' '"/>
</xsl:call-template>
</wholetext>
<xsl:choose>
<xsl:when test="contains(., 'went') = True and contains(., 'to') = True">
<match>went to</match>
</xsl:when>
<xsl:when test="contains(., 'founded') = True and contains(., 'in') = True">
<match>founded in</match>
</xsl:when>
<xsl:when test="contains(., 'created') = True and contains(., 'the') = True">
<match>created the</match>
</xsl:when>
<xsl:otherwise test="contains(., 'a') = True and contains(., 'visit') = True">
<match>a visit</match>
</xsl:otherwise>
</xsl:choose>
<person><xsl:value-of select="en[@x='PERS']"/></person>
<organization><xsl:value-of select="en[@x='ORG']"/></organization>
<location><xsl:value-of select="en[@x='LOC']"/></location>
</xsl:copy>
</xsl:template>
<!-- Rewrites Unmatched Phrase elements -->
<xsl:template match="PHRASE[not({0})]"/>
<!-- Join Text values -->
<xsl:template name="join">
<xsl:param name="valueList" select="''"/>
<xsl:param name="separator" select="','"/>
<xsl:for-each select="$valueList">
<xsl:choose>
<xsl:when test="position() = 1">
<xsl:value-of select="."/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat($separator, .) "/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
</xsl:transform>'''.format(contains[:-4])
dom = ET.parse(os.path.join(cd, 'SearchWords.xml'))
xslt = ET.fromstring(xslstr)
transform = ET.XSLT(xslt)
newdom = transform(dom)
tree_out = ET.tostring(newdom, encoding='UTF-8', pretty_print=True)
print(tree_out.decode("utf-8"))
for phrase in newdom.findall('PHRASE'):
print("Text: {} \n ORG is: {}, PERS is: {} /".format(phrase.find('wholetext').text,
phrase.find('organization').text,
phrase.find('person').text))
输出
下面包括用于演示的转换后的 xml。该tree_out
字符串可以在外部保存为新的 xml 文件。
<TEXT>
<PHRASE>
<wholetext>went Mark to United Nations for a visit</wholetext>
<person>Mark</person>
<organization>United Nations</organization>
<location/>
</PHRASE>
<PHRASE>
<wholetext>in 1987 Nick founded XYZ company</wholetext>
<person>Nick</person>
<organization>XYZ company</organization>
<location/>
</PHRASE>
<PHRASE>
<wholetext>Google's Frank went yesterday to San Fransisco</wholetext>
<person>Frank</person>
<organization>Google's</organization>
<location>San Fransisco</location>
</PHRASE>
</TEXT>
Text: went Mark to United Nations for a visit
ORG is: United Nations, PERS is: Mark /
Text: in 1987 Nick founded XYZ company
ORG is: XYZ company, PERS is: Nick /
Text: Google's Frank went yesterday to San Fransisco
ORG is: Google's, PERS is: Frank /
列表理解
请参阅列表理解尝试使用xpath
. 但是,挑战在于您finalPatterns
在一致的比赛中不匹配。例如 text 可以went \n to
与 like 之间的单词一起使用went \n Mark \n to
。如果列表的每个元素仅包含一个关键字,则可以使用以下方法。否则考虑使用正则表达式进行模式识别。
dom = ET.parse(os.path.join(cd, 'Input.xml'))
phraselist = dom.xpath('//PHRASE')
for phrase in phraselist:
if any(word in p for p in phrase.xpath('./*/text()') for word in finalPatterns):
print(' '.join(phrase.xpath('./*/text()')))
print('ORG is: {0}, PERS is: {1}'.format(phrase.xpath("./en[@x='ORG']")[0].text, \
phrase.xpath("./en[@x='PERS']")[0].text))