2012-08-14 65 views
2
线

我得到一个错误的下面一行:在实例inst =新实例(1.0,new double [attrs.size()])上获取错误;

Instance inst =new Instance(1.0, new double[attrs.size()]); 

任何机构可以解释我是如何解决这个问题?

下面是代码:

import java.io.File; 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.HashSet; 
import java.util.List; 
import java.util.Map; 
import java.util.Set; 
import java.util.regex.Matcher; 
import java.util.regex.Pattern; 

import weka.core.Attribute; 
import weka.core.FastVector; 
import weka.core.Instance; 
import weka.core.Instances; 
import weka.core.converters.ArffSaver; 


public class ContextualFeatureExtractor implements FeatureExtractor{ 
    private static final int NUMBASEATTR = 1; 
    private static final int[] HOURCATEGORY = {3,3,3,3,0,0,0,0,0,0,1,1,1,1,1,1,2,2,2,2,2,2,3,3}; 
    private static final String[] TIMENAME = {"Morning","Afternoon","Evening","Night"}; 
    private static final int MINEMOTCOUNT = 100; 

    private FastVector attrs; 
    private Pattern emotpat; 
    private ArrayList<String> emotslist; 

    public void setupAttributes(List<Tweet> tweets) 
    { 
     attrs = new FastVector(); 
     // Determine attributes 

     FastVector hourvals = new FastVector(); 
     hourvals.addElement(TIMENAME[0]); hourvals.addElement(TIMENAME[1]); 
     hourvals.addElement(TIMENAME[2]); hourvals.addElement(TIMENAME[3]); 
     Attribute timeofday = new Attribute("TimeOfDay", hourvals); 
     attrs.addElement(timeofday); 

     // Find frequent emoticons 
     emotpat = Pattern.compile("\\p{Graph}*\\p{Punct}\\p{Graph}*"); 
     HashMap<String,Integer> emots = new HashMap<String,Integer>(100); 
     for(Tweet t: tweets) 
     { 
      Matcher emotmat = emotpat.matcher(t.text); 
      while (emotmat.find()) 
      { 
       String curemot = emotmat.group(); 
       if(curemot.length() > 1 && curemot.length() < 5) { 
        if(emots.containsKey(curemot)) { 
         int curcount = emots.get(curemot); 
         curcount++; 
         emots.put(curemot, curcount); 
        } 
        else 
         emots.put(curemot, 1); 
       } 
      } 
     } 
     Set<Map.Entry<String,Integer>> emotset = emots.entrySet(); 
     Set<String> emotrem = new HashSet<String>(100); 
     for(Map.Entry<String,Integer> emotmap : emotset) 
     { 
      if(emotmap.getValue() < MINEMOTCOUNT) 
       emotrem.add(emotmap.getKey()); 
     } 
     Set<String> goodemots = emots.keySet(); 
     goodemots.removeAll(emotrem); 
     emotslist = new ArrayList<String>(); 
     for(String emot : goodemots) 
     { 
      Attribute attr = new Attribute("confeature:" + emot); 
      attrs.addElement(attr); 
      emotslist.add(emot); 
     } 
    } 

    public Instances extractFeatures(List<Tweet> tweets) 
    { 
     if(attrs == null) 
      setupAttributes(tweets); 
     Instances feats = new Instances("Contextual Features", attrs, tweets.size()); 
     feats.setClassIndex(0); 
     // Record features 

     for(Tweet t: tweets) 
     { 

      Instance inst =new Instance(1.0, new double[attrs.size()]); 
      inst.setDataset(feats); 

      int hrcat = HOURCATEGORY[t.hour]; 
      inst.setValue(0, TIMENAME[hrcat]); 

      Matcher emotmat = emotpat.matcher(t.text); 
      while (emotmat.find()) 
      { 
       String curemot = emotmat.group(); 
       if(emotslist.contains(curemot)) 
       { 
        int attrind = emotslist.indexOf(curemot)+NUMBASEATTR; 
        //double val = inst.value(attrind); 
        //val += 1.0; 
        inst.setValue(attrind, 1.0); 
       } 

      }   

      feats.add(inst); 
     } 

     ArffSaver saver = new ArffSaver(); 
     saver.setInstances(feats); 
     try { 
      saver.setFile(new File("output/contextual.arff")); 
      saver.writeBatch(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     return feats; 
    } 

    public static void main(String[] args) { 
     ArrayList<Tweet> tweets = TweetFileParser.parseFile("data/train.40000.2009.05.25"); 
     ContextualFeatureExtractor cfe = new ContextualFeatureExtractor(); 
     Instances insts = cfe.extractFeatures(tweets); 
     System.out.println(insts.toSummaryString()); 
    } 
} 
+0

这将是非常有用的,如果你要包括错误输出,而不是只提 “一个错误”。在这个特殊情况下,如果你提到了一个'NoClassDefFoundError',这将立即表明错误是什么,而不需要Nandkumar的信心灵魂飞跃。 – 2012-08-14 11:29:13

+0

@ Andrzej Doyle无法实例化类型实例 – XYZ 2012-08-14 11:30:54

+0

这是* all *它说的吗?没有例外类名称?没有“由......引起”?没有堆栈跟踪?如果您将完整的输出编辑到您的问题中(可能替换源代码,这可能无关紧要),这将是理想的。 – 2012-08-14 11:37:09

回答

1

weka - LibSVM => libsvm.jar库不在类路径中。从here下载它。或者如果你有图书馆,然后将它添加到你的类路径。

3

Javadocs看来Instance是一个接口 - 这意味着你不能实例化它,但必须改为构造它的一个实现类。 (这看起来是BinarySparseInstance,DenseInstanceSparseInstance之一)。

但是我确实看到在其他一些版本中,Instance was a concrete class。如果您正在通过一个版本的库中的另一个版本运行示例,这可能会导致您遇到此错误。确保您使用的是您期望的版本,然后查看版本的文档以查看可以调用的内容。

参见:

+0

http://stackoverflow.com/questions/17352485/error-m2e-install-in-eclipse – XYZ 2013-08-10 09:38:53