<context>...</context>
タグ内のすべてのテキストを読み取るにはどうすればよいですか?そして、<head>...<\head>
タグ内の<context \>
タグはどうですか?
次のようなXMLファイルがあります。
<corpus lang="english">
<lexelt item="coach.n">
<instance id="1">
<context>I'll buy a train or <head>coach</head> ticket.</context>
</instance>
<instance id="2">
<context>A branch line train took us to Aubagne where a <head>coach</head> picked us up for the journey up to the camp.</context>
</instance>
</lexelt>
</corpus>
しかし、コードを実行して...内のXMLテキストを読み取ると、タグに到達するまでテキストを取得するだけです。
import xml.etree.ElementTree as et
inputfile = "./coach.data"
root = et.parse(open(inputfile)).getroot()
instances = []
for corpus in root:
for lexelt in corpus:
for instance in lexelt:
instances.append(instance.text)
j=1
for i in instances:
print "instance " + j
print "left: " + i
print "\n"
j+=1
今、私はちょうど左側を取得しています:
instance 1
left: I'll buy a train or
instance 2
left: A branch line train took us to Aubagne where a
出力には、コンテキストとヘッドの右側も必要です。次のようになります。
instance 1
left: I'll buy a train or
head: coach
right: ticket.
instance 2
left: A branch line train took us to Aubagne where a
head: coach
right: picked us up for the journey up to the camp.