总体思路是编写一个脚本,将种子列表中的单词与话语中的单词进行比较。因此,我试图
xml语料库的结构如下:
<section name="thisisaxml-file">
<topic>
<utterance name="John Doe" id="264">
foo bar?
</utterance>
<utterance name="Henry Parker" id="265">
foo foo bar. New York, wind.
</utterance>
</topic>
</section>
到目前为止,我是基于我出色的,自学的python技能:
import pandas as pd
import xml.etree.ElementTree as ET
import nltk
from nltk.tokenize import word_tokenize
#xml file data input
xml_data = 'sample.xml'
#create an ElementTree object
etree = ET.parse(xml_data)
doc_df = pd.DataFrame(list(iter_docs(etree.getroot())))
hedge = ['foo', 'wind', 'base']
df = pd.DataFrame({'utterance': doc_df['utterance']})
df['id'] = pd.DataFrame({'id': doc_df['id']})
df['name'] = pd.DataFrame({'name': doc_df['name']})
df['tokenized_sents'] = df.apply(lambda row: word_tokenize(row['utterance']), axis=1)
df['sents_length'] = df.apply(lambda row: len(row['tokenized_sents']), axis=1)
final = df[df.tokenized_sents.apply(lambda x: hedge in x)]
final.to_csv('out.csv', sep='\t', encoding='utf-8') #prints to file
我偶然发现了一些问题:
df['tokenized_sents'].lower()
无法正常工作,因为(我认为)pd.DataFrame(list(iter_docs(etree.getroot())))
中的列表格式df[df.tokenized_sents.apply(lambda x: hedge in x)]
可以工作,但是不返回任何东西,但是,如果您使用的是字符串,则可以工作。因此,一般而言,我想对照熊猫中的特定列检查单词列表。虽然我在该站点上看到过类似的主题,但到目前为止,所提到的解决方案都没有对我有用。
你对我有什么想法吗?
答案 0 :(得分:0)
变量df已经是一个带有字典的数据框。在数据帧中创建数据帧会破坏您的数据,或者至少我看到它破坏了我的某些数据。如果情况并非总是如此,那么我很乐意为您提供参考。 无论如何,不知道这是否可以解决您的问题,但可以肯定会清理您的代码。
hedge = ['foo', 'wind', 'base']
df = pd.DataFrame({
'utterance': doc_df['utterance'],
'id':doc_df['id'],
'name':doc_df['name']})
df['tokenized_sents'] = df.apply(lambda row:word_tokenize(row['utterance']),axis=1)
df['sents_length'] = df.apply(lambda row: len(row['tokenized_sents']), axis=1)