1

我希望能够以不指定句子的 xml 格式逐句处理。我的输入如下所示:

<p xmlns="https://jats.nlm.nih.gov/ns/archiving/1.0/"> 
Recently, a first step in this direction has been taken
in the form of the framework called &#8220;dynamical fingerprints&#8221;,
which has been developed to relate the experimental and MSM-derived
kinetic information.<sup><xref ref-type="bibr" rid="ref56">56</xref></sup> Several research
groups are now focused on developing protocols to systematically cross-validate
the MSM predictions and obtain MSM parameters using an optimization
protocol that produces the best estimate of the few slowest dynamics
modes of the protein dynamics.<sup><xref ref-type="bibr" rid="ref57">57</xref></sup></p>

我希望我的输入看起来更像:

<p xmlns="https://jats.nlm.nih.gov/ns/archiving/1.0/">
<s>Recently, a first step in this direction has been taken
in the form of the framework called &#8220;dynamical fingerprints&#8221;,
which has been developed to relate the experimental and MSM-derived
kinetic information.<sup><xref ref-type="bibr" rid="ref56">56</xref></sup> </s><s>Several research
groups are now focused on developing protocols to systematically cross-validate
the MSM predictions and obtain MSM parameters using an optimization
protocol that produces the best estimate of the few slowest dynamics
modes of the protein dynamics.<sup><xref ref-type="bibr" rid="ref57">57</xref></sup></s></p>

这样我就可以像这样提取这些整体:

<s xmlns="https://jats.nlm.nih.gov/ns/archiving/1.0/">Recently, a first step in this direction has been taken
in the form of the framework called &#8220;dynamical fingerprints&#8221;,
which has been developed to relate the experimental and MSM-derived
kinetic information.<sup><xref ref-type="bibr" rid="ref56">56</xref></sup> </s>

<s xmlns="https://jats.nlm.nih.gov/ns/archiving/1.0/">Several research
groups are now focused on developing protocols to systematically cross-validate
the MSM predictions and obtain MSM parameters using an optimization
protocol that produces the best estimate of the few slowest dynamics
modes of the protein dynamics.<sup><xref ref-type="bibr" rid="ref57">57</xref></sup></s>

我的测试代码是:

from lxml import etree

if __name__=="__main__":

  xml1 = '''<p xmlns="https://jats.nlm.nih.gov/ns/archiving/1.0/"> 
Recently, a first step in this direction has been taken
in the form of the framework called &#8220;dynamical fingerprints&#8221;,
which has been developed to relate the experimental and MSM-derived
kinetic information.<sup><xref ref-type="bibr" rid="ref56">56</xref></sup> Several research
groups are now focused on developing protocols to systematically cross-validate
the MSM predictions and obtain MSM parameters using an optimization
protocol that produces the best estimate of the few slowest dynamics
modes of the protein dynamics.<sup><xref ref-type="bibr" rid="ref57">57</xref></sup></p>
'''


  print xml1

  root = etree.XML(xml1)
  sentences_info = []
  for sentence in root:
    # I want to do more fun stuff here with the result
    sentence_text = sentence.text
    ref_ids = []
    for reference in sentence.getchildren():
        if 'rid' in reference.attrib.keys():
            ref_id = reference.attrib['rid']
            ref_ids.append(ref_id)
    sent_par = {'reference_ids': ref_ids,'text': sentence_text}
    sentences_info.append(sent_par)
    print sent_par
4

2 回答 2

0

这是当您解析 XML 时,它仍然包含命名空间。基本上,您解析的每个 XML 都将包含以下元素:

<Element {https://jats.nlm.nih.gov/ns/archiving/1.0/}p at 0x108219048>

您可以使用以下函数从 XML 中删除命名空间:

from lxml import etree

def remove_namespace(tree):
    for node in tree.iter():
        try:
            has_namespace = node.tag.startswith('{')
        except AttributeError:
            continue  # node.tag is not a string (node is a comment or similar)
        if has_namespace:
            node.tag = node.tag.split('}', 1)[1]

然后解析 XML 并删除命名空间

tree = etree.fromstring(xml1)
remove_namespace(tree) # remove namespace
tree.findall('sup') # output as [<Element sup at 0x1081d73c8>, <Element sup at 0x1081d7648>]
于 2017-06-18T04:24:28.693 回答
0

将 BeautifulSoup 对象转换为字符串,然后使用正则表达式进行清理效果很好。例如:

from bs4 import BeautifulSoup

soup = BeautifulSoup(urlopen('yourlink.com'), 'lxml')

paragraphs = str(soup.findAll('p')) #turn the soup object into a string

sentences = paragraphs.split('<sup><xref ref-type="bibr" rid="ref56">56</xref></sup>') #creates a list of sentences

clean = []
for e in sentences:
    e = re.sub(r'(<.*?>)', '', e) #gets rid of the tags
    clean.append(e)

据我所知,xml中没有内置的处理句子的方法,它需要自己的临时解决方案。

于 2017-06-17T04:52:34.170 回答