【问题标题】:trying to create a .arff file using python尝试使用 python 创建一个 .arff 文件
【发布时间】:2013-12-09 16:48:03
【问题描述】:

我想创建一个 .arff 文件,其中显示我的 python 代码中最有用的 10 个单词。格式应该是这样的。

@attribute pattern1 {yes,no}
@attribute pattern2 {yes,no} 
......
.......
@attribute emotion {angry,disgusted,fearful,happy,sad,surprised}

@data
yes, no, no,......, yes, happy
no, no, no,....., no, angry
yes, yes, no,......, yes, sad

每一行都应该包含一个由 10 个“真”或“假”值组成的列表,然后是一种情绪。

这是我到目前为止所写的,但它没有按要求显示。请帮帮我。

f = open("emotions.txt", "w")
f.write('''@RELATION Emotions\n
    @ATTRIBUTE word{yes,no}
    @ATTRIBUTE class {angry,sad,happy,surprised,fearful,disgusted}
    @DATA\n''')
for word in varall:
f.write("%s\n" %word)
f.close()

【问题讨论】:

  • 您的代码中如何定义varall
  • 有人可以帮我吗?

标签: python nlp arff


【解决方案1】:

您应该查看this library 它就是为这个问题而设计的,因为手工编码你的 arff 输出并不是一个好主意。

对于您的属性,您将执行以下操作:

arff_writer = arff.Writer(fileName, relation='Emotions',  header_names=['pattern1','pattern2', ... 'emotion')
arff_writer.pytypes[arff.nominal] = '{angry,disgusted,fearful,happy,sad,surprised}'
arff_writer.write([arff.nominal('emotion')])

对于您的数据:

data = [[1,2,'a'], [3, 4, 'john']]
arff.dump(open(fileName, 'w'), data, relation="whatever", header_names)

【讨论】:

    猜你喜欢
    • 2011-07-10
    • 1970-01-01
    • 2015-06-24
    • 2020-06-15
    • 1970-01-01
    • 1970-01-01
    • 2020-10-20
    • 2021-01-17
    • 1970-01-01
    相关资源
    最近更新 更多