如何使用AddValues向实例添加新的名义值?

时间:2016-01-11 13:44:22

标签: java weka

我真的不得不咀嚼WEKA API。我想做的就是为我的属性引入新的名义值。

我有句子1

等数据
@relation i-do-not-care

@attribute sentence_id {1090587:0}
@attribute word {here,girlfriend,bday,23rd,go,for,my,just}
@attribute relations {nmod:poss-,amod+;nsubj-,nsubj+;nmod+;advmod+,amod-,nmod-;case+;nmod:poss+,advmod-,case-}
@attribute target {0}

@data
1090587:0,go,nsubj+;nmod+;advmod+,0
1090587:0,my,nmod:poss-,0

句子2

@relation i-do-not-care

@attribute sentence_id {1090587:1}
@attribute word {be,idea,house,weehawken,offer,view,top,along,of,have,if,you,a,in,the,ever,to,river,chart}
@attribute relations {aux-,cop-,nmod-;case+,nsubj+;acl:relcl-;xcomp+,advmod-,det-,nsubj-,advcl+;nsubj+;dobj+,case-,acl:relcl+;det+;nmod+;dobj-,mark+;nsubj+;det+;nmod+;cop+;aux+;case+;advcl-;advmod+,compound+;nsubj-;det+,mark+;xcomp-,mark-,compound-,det+;nmod+;nmod-;case+}
@attribute target {0,1}

@data
1090587:1,a,det-,0
1090587:1,have,advcl+;nsubj+;dobj+,0

我试图完成的是将它们添加到空的Instance数据集中:

@relation features

@attribute sentence_id {}
@attribute word {}
@attribute relations {}
@attribute target {0,1}

@data

这就是我尝试这样做的方式:

public Instances add(Instances instances, Instances newInstances) {

    AddValues addValues = new AddValues();

    try {   
        // This should just copy 'sentence_id' values   
        // but I am not sure about that.            
        addValues.setAttributeIndex("1");
        addValues.setInputFormat(newInstances);

        instances = Filter.useFilter(instances, addValues); 

        return instances;               
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

但它只是覆盖instances而不是添加值。

1 个答案:

答案 0 :(得分:0)

您使用相同的代码两次。

instances = Filter.useFilter(instances, addValues); 

return instances = Filter.useFilter(instances, addValues); 

试试这个:

instances = Filter.useFilter(instances, addValues); 

return instances;

或者下一个级别:

return Filter.useFilter(instances, addValues