我有一组标记,我试图从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以及通过降低血浆HIV-1 RNA水平来减少HIV-1感染的6-8共感染治疗。
以提取标签及其在渲染文本中的位置。目前,我正在使用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此代码无法找到第二个xref上的文本,因为它前面的文本是前一个xref标记的一部分。
有什么建议吗?
发布于 2012-07-11 18:47:33
没人回应所以我只好即兴发挥。这是我目前的方法:
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方法,它返回呈现的文本。我用它来确定段落中每个孩子的长度。然后我用这些长度来确定我要找的标签的位置。
希望这能帮到别人!
-Will
https://stackoverflow.com/questions/11421782
复制相似问题