我有一组标签,我试图从 XML 文本中提取并确定它们在“渲染”文本中的位置。
例如:
XML:
<p>The risk of sexual transmission of HIV-1 correlates strongly with plasma HIV-1 level.
<xref ref-type="bibr" rid="pone.0012598-Fideli1">[1]</xref>,
<xref ref-type="bibr" rid="pone.0012598-Quinn1">[2]</xref>This association has motivated proposed interventions (such as use of antiretroviral therapy (ART),
<xref ref-type="bibr" rid="pone.0012598-Cohen1">[3]</xref>,
<xref ref-type="bibr" rid="pone.0012598-Granich1">[4]</xref> therapeutic HIV-1 vaccines,<xref ref-type="bibr" rid="pone.0012598-Gurunathan1">[5]</xref> and treatment for co-infections<xref ref-type="bibr" rid="pone.0012598-Corey1">[6]</xref>–<xref ref-type="bibr" rid="pone.0012598-Walson1">[8]</xref> that reduce HIV-1 infectiousness by reducing levels of plasma HIV-1 RNA.
渲染:
HIV-1 的性传播风险与血浆 HIV-1 水平密切相关。[1]、[2] 这种关联促使提出了干预措施(例如使用抗逆转录病毒疗法 (ART)、[3]、[4] HIV-1 疫苗 [5] 和合并感染的治疗 [6]-[8] 通过降低血浆 HIV-1 RNA 的水平来降低 HIV-1 的传染性。
为了提取标签及其在渲染文本中的位置。目前我正在使用bs4
与此代码类似的代码(sent_tokenize
来自 NLTK 工具箱,并list
从输入文本创建一个句子):
for n, p in enumerate(article.find_all('p')):
rawtext = str(p) #returns the XML version of the text
readtext = p.text #returns the rendered version
sents = sent_tokenize(readtext) #splits sentences
for ref in p.find_all('xref'):
startloc = rawtext.find(str(ref))
prestart = max(0, startloc-20)
for s in sents:
if s.find(rawtext[prestart:startloc]) > -1:
print s, ref
break
此代码无法在第二个外部参照上找到 ,因为它之前的文本是前一个外部参照标记的一部分。
有什么建议么?