weka java api stringtovector 异常

所以我有这样一段使用Weka Java API的代码:

  String html = "blaaah";    Attribute input = new Attribute("html",(FastVector) null);    FastVector inputVec = new FastVector();    inputVec.addElement(input);    Instances htmlInst = new Instances("html",inputVec,1);    htmlInst.add(new Instance(1));      htmlInst.instance(0).setValue(0, html);    System.out.println(htmlInst);StringToWordVector filter = new StringToWordVector();filter.setInputFormat(htmlInst);Instances dataFiltered = Filter.useFilter(htmlInst, filter);

但是在执行filter.setInputFormat(htmlInst)这一行时,Java报错说该函数抛出了未处理的异常…

我哪里做错了?


回答:

当一个函数明确抛出异常时,必须发生以下两种情况之一:

  1. 调用函数必须在try-catch块中处理该异常
  2. 调用函数必须将异常抛给其调用者函数(因此您必须选择某个点实际使用try-catch块来处理该异常)

根据此处的文档:http://www.lri.fr/~pierres/donn%E9es/save/these/weka-3-4/doc/weka/filters/unsupervised/attribute/StringToWordVector.html#setInputFormat(weka.core.Instances) 该函数抛出一个普通的Exception。虽然描述不算详细,但仍然需要适当处理。

您可以这样做:

try {    StringToWordVector filter = new StringToWordVector();    filter.setInputFormat(htmlInst);    Instances dataFiltered = Filter.useFilter(htmlInst, filter);} catch (Exception e) {    System.err.println("格式化过程中捕获到异常: " + e.getMessage());    return;}

如果您希望另一个函数处理该异常,可以更改方法签名以明确抛出异常:

private Object formatMyString(String s) throws Exception {    ...}

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注