目的:
将类似下图的文本数据转换为arff文件。文本数据每一行是一个句子和该句子的类属性值(classValue),得到的arff文件中的一个实例(Instance)即为一个句子的单词向量和类属性值。
思路:
1.将原始文本数据中一些特殊符号去除,以免在构建单词向量时出现问题;
2.在去除特殊符号的文本数据上自动添加arff文件的表头,另存为arff文件;
3.在WEKA中读取该arff文件,再使用StringToWordVector过滤器将该arff文件中的每个样本(此时为String)转换为单词向量,将数据另存为新的arff文件,即为最终需要的arff文件。
具体过程:
1.原始文本数据存储在temp.txt,将temp.txt中的特殊符号全部用空格替换。我使用JAVA处理,主要处理了(, = \ ’ -%/()* +)这些特殊符号,注意在处理( ()* +)这写符号时,需要在其前面添加“\\”,否则会报错:java.util.regex.PatternSyntaxException: Unclosed group near index 1
。
2.随后在temp.txt文件中的前面添加arff文件的表头,如下:
@relation data
@attribute text string
@attribute class {0,1}
@data
Multi-layers kept management at a distance from its customers , O'Neal said . 1
Now organisations must change to stay competitive . 0
O'Neal says firms will place a greater emphasis on teams and team performance in giving raises . 1
If your team does well , you 'll do well . 1
If it does n't do well , do n't expect a raise . 1
Is this fair to an employee who can go the distance but is on a team that ca n't keep up ? 0
That 's an important question we used to ask a lot , said O'Neal . 1
另存为temp.arff文件。
3.在WEKA中读取temp.arff文件,选择filters->unsupervised->attribute->StringToWordVector过滤器,设置相应的参数。参数设置说明如下:
此时会生成很多属性,名称是对应的单词。需要注意的是,这样子可能会使得类属性不是原先名称为class的属性了,所以需要点击Edit,右击class属性,点击Attribute as class,即设置class属性为类属性。随后Save为最终的arff文件即可。
注:这样得到的arff文件的数据往往是以稀疏矩阵的方式存储的,因为文本数据转换过来的单词向量有着大量的0,采用原始的方式存储会占据大量内存。