我不知道arff是什么(也不需要知道如何帮助您将文本转换为不同的格式),所以让我们从以下内容开始:
$ cat tst.awk
BEGIN{ FS="\t" }
NR==1 { printf "@relation '%s'\n", FILENAME }
{
row = $1
attr = $2
if (!seenRow[row]++) {
rows[++numRows] = row
}
if (!seenAttr[attr]++) {
printf "@attribute \"%s\" string\n", attr
attrs[++numAttrs] = attr
}
score[row,attr] = $3
}
END {
print "\n\n@data"
for (rowNr=1; rowNr<=numRows; rowNr++) {
row = rows[rowNr]
for (attrNr=1;attrNr<=numAttrs;attrNr++) {
attr = attrs[attrNr]
printf "%d,", score[row,attr]
}
print row
}
}
$
$ cat file
church place 3
church institution 6
man place 86
man food 63
woman book 37
$
$ awk -f tst.awk file
@relation 'file'
@attribute "place" string
@attribute "institution" string
@attribute "food" string
@attribute "book" string
@data
3,6,0,0,church
86,0,63,0,man
0,0,0,37,woman
现在,告诉我们这有什么问题,我们可以从那里开始。