【问题标题】:nominal value not declared in header标头中未声明的标称值
【发布时间】:2017-02-01 23:03:10
【问题描述】:

我正在从 xslx 生成一个带有 groovy 的 arff 文件, 但是当我尝试在 weka 中打开这个文件时,我得到了这个错误:

文件“...”未被识别为“Arff 数据文件”文件。 原因: 标头中未声明的标称值,请阅读 Token[Ativo],第 16 行

我不明白为什么我会收到这个错误 有人可以帮我解决这个错误,并解释为什么会这样吗?

生成的文件

@relation kd-itempedido
@attribute tipopedido {Assistencia,Recompra,Venda,Troca}
@attribute aprovado {0.0,1.0}
@attribute fasepedido {Aprovado,Cancelado,EmAprovacao,Liberado,Novo}
@attribute statusinternopedido {NegociarPagamento,PedidosDeTeste,AguardandoOcorrencia,Nada,AguardandoBoletoDeposito,PedidoDuplicado,SuspeitaDeFraude}
@attribute canal {Marketplace,Desktop}
@attribute origem {LojasAmericanas,Optimise,MercadoLivre,Cityads,Zanox,Zoom,Rakuten,Lomadee,Facebook,Viptarget,Submarino,Criteo,Muccashop,Chaordic,Walmart,Googlead,Nada,Extra,Lojaskd,Shopback,Afilio,Shoptime,Nextperformance,CarrinhoAbandonado,Bing}
@attribute mercado {S,N}
@attribute cluster {EntregaImediata,Fiprec,Icconv,Esgotado}
@attribute statusitem {Ativo}
@attribute statusproduto {Inativo,Ativo,AtivoSemEstoque,ForaDeLinha}
@attribute polo {Polo1,Polo3,Polo2}
@data
Venda,0.0,Novo,Nada,Desktop,Googlead,S,Fiprec,Ativo,Ativo,Polo2
Venda,0.0,Novo,Nada,Desktop,Googlead,S,Fiprec,Ativo,Ativo,Polo2
Venda,0.0,Novo,Nada,Desktop,Googlead,S,Ativo,Inativo,Polo2
Venda,0.0,Novo,Nada,Desktop,Muccashop,N,Ativo,Ativo,Polo3

Groovy (VM -Dfile.encoding=ascii utf-8 utf8)

@Grapes([
        @Grab('org.apache.poi:poi:3.10.1'),
        @Grab('org.apache.poi:poi-ooxml:3.10.1')])
import org.apache.poi.xssf.usermodel.XSSFWorkbook
import java.text.Normalizer
import static org.apache.poi.ss.usermodel.Cell.*
import java.nio.file.Paths

def path = "/home/eric/Documents/development/ufpr/Solid Eric/ItemPedido1000.xlsx"
def relation = "kd-itempedido"
def columns = ["tipopedido", "aprovado", "fasepedido", "statusinternopedido", "canal", "origem", "mercado", "cluster", "statusitem","statusproduto", "polo"]
def arff = "ItemPedido.arff"
new XslxToArffParser(path, relation, columns, arff);

class Data{
    def rows = new ArrayList<List>();

    @Override
    String toString() {
        def s = ""
        for (r in rows){
            for(d in r){

                s+=d
                if(r.indexOf(d) < (r.size()-1))
                    s+=","
            }
            s+="\n"
        }
        return s
    }
}



class Atributo {
    def descricao;
    def possibilidades = new HashSet<Object>();
    def index;

    @Override
    String toString() {

        def builder = new StringBuilder()
        builder.append("@attribute ").append(descricao)
        builder.append(" {")
        for(def i = 0; i<possibilidades.size(); i++){
            builder.append(possibilidades[i])
            if((i+1) != possibilidades.size())
                builder.append(",")
        }
        builder.append("}").append("\n")
        return builder.toString();
    }
}

class XslxToArffParser {
    def attributes =[:];
    def data = new Data();
    def sheet = null;

    XslxToArffParser(path, relation, columns, arffPath){
        load(path)
        getAttributes(columns)
        collectData()
        saveArff(relation, arffPath)
    }

    def String parse(String s){
        s = Normalizer.normalize(s, Normalizer.Form.NFD)
        s = s.replaceAll("[\\p{InCombiningDiacriticalMarks}]", "")
        s = s.split(/[^\w]/).collect { it.toLowerCase().capitalize() }.join("")
        s = s.replaceAll(" ", "")
        s = s.replaceAll("[^A-Za-z0-9]", "")
        s = s.isEmpty() ? "Nada" : s
        return s
    }

    def load(path) {
        Paths.get(path).withInputStream { input ->
            def workbook = new XSSFWorkbook(input)
            sheet = workbook.getSheetAt(0)
        }
    }

    def getAttributes(columns){
        for (cell in sheet.getRow(0).cellIterator()) {
            def index = cell.columnIndex
            def description = parse(cell.stringCellValue).toLowerCase()
            if(columns.contains(description)){
                attributes << [(index):new Atributo(descricao: description, index: index)]
            }
        }
    }

    def collectData(){
        def headerFlag = true
        for (row in sheet.rowIterator()) {
            if (headerFlag) {
                headerFlag = false
                continue
            }
            def r = []
            for (cell in row.cellIterator()) {
                def index = cell.columnIndex;
                def value = cell.cellType == CELL_TYPE_STRING ? parse(cell.stringCellValue) : cell.numericCellValue

                def attr = attributes[index]
                if(attr != null){
                    attr.possibilidades.add(value)
                    r << value
                }
            }

            data.rows.add(r)
        }
    }

    def saveArff(relation, path){
        Paths.get(path).withWriter { writer ->

            writer.write "@relation " + relation
            writer.write "\n"
            for(a in attributes.values())
                writer.write a.toString()

            writer.write "@data"
            writer.write "\n"

            writer.write data.toString()
        }
    }
}

解决了。 "row.cellIterator()" 不会遍历空/空白单元格

【问题讨论】:

    标签: artificial-intelligence weka arff


    【解决方案1】:

    我使用 Weka 已经有一段时间了,但是查看您显示的文件和错误消息,我怀疑问题出在数据文件的最后两行。它们没有属性“集群”的值。

    在 S 或 N 之后(对于属性“mercado”),它们有“Ativo”。该“Ativo”值未定义为名义属性集群的可能值之一。该文件确实读取了“Ativo”(这就是错误消息显示“读取令牌 [Ativo]”的原因,但它希望读取集群属性的值,但它还没有期待 statusitem 属性的值。

    【讨论】:

      猜你喜欢
      • 2013-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-15
      • 2014-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多