文章目录
1.准备数据
1.1 准备训练数据
用的是原始的xml数据,选择v2版本,根据这篇文章,进行格式转化,转换成csv文件
import xml.etree.cElementTree as ET
path = 'Restaurants_Train_v2.xml'
tree = ET.parse(path)
root = tree.getroot()
# category级别
data = []
for sentence in root.findall('sentence'):
text = sentence.find('text').text
aspectCategories = sentence.find('aspectCategories')
for aspectCategory in aspectCategories.findall('aspectCategory'):
category = aspectCategory.get('category')
polarity = aspectCategory.get('polarity')
data.append((text, category, polarity))
import pandas as pd
df=pd.DataFrame(data,columns=['text','category','polarity'])
df.to_csv('restaurant_train_category.csv',index=False)
df.head()
# aspect级别
data=[]
for sentence in root.findall('.//aspectTerms/..'):
text = sentence.find('text').text
aspectTerms=sentence.find('aspectTerms')
for aspectTerm in aspectTerms.findall('aspectTerm'):
term = aspectTerm.get('term')
polarity = aspectTerm.get('polarity')
data.append((text, term, polarity))
df = pd.DataFrame(data,columns=['text', 'term', 'polarity'])
df = df[df['polarity'].isin(['positive', 'negative', 'neutral'])]
df['polarity'] = df['polarity'].map(
{
'positive': 1, 'neutral': 0, 'negative': -1})
df.to_csv('restaurant_train_aspectterm.csv',index=0)
df
1.2 准备测试数据
数据来源:下载 Restaurants_Test_Gold.xml.seg 以及Laptops_Test_Gold.xml.seg 都是txt格式的文件
里面的数据是换行分隔的

data1 = pd.read_table('Restaurants_Test.txt',header=None)
#每隔三行存储一个属性
list1 = [

该博客详细介绍了如何处理Semeval 2014餐厅评价数据集,从数据准备到模型训练,再到模型应用。首先,博主讲述了如何将XML数据转化为CSV格式,接着介绍了如何准备测试数据。在模型训练部分,涵盖了数据预处理、模型构建、验证集划分、数据迭代器创建,以及模型保存和评估。最后,博主展示了如何加载训练好的模型进行实际应用。
最低0.47元/天 解锁文章
4471

被折叠的 条评论
为什么被折叠?



