1

我有一组标签,我试图从 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>–&lt;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

此代码无法在第二个外部参照上找到 ,因为它之前的文本是前一个外部参照标记的一部分。

有什么建议么?

4

1 回答 1

1

好吧,没有人回应,所以我不得不即兴发挥。这是我目前的方法:

lens = [len(tag.string) for tag in p.contents]
clens = [sum(lens[:ind]) for ind in xrange(1,len(lens))]
locs = [spot for tag, spot in zip(p.contents, clens) if isinstance(tag, Tag) and tag.name == 'xref']

基本思想是使用string返回渲染文本的方法。我用它来确定段落的每个子项的长度。然后我使用这些长度来确定我正在寻找的标签的位置。

希望对其他人有所帮助!

-将要

于 2012-07-11T18:47:33.997 回答