【发布时间】:2013-02-10 02:03:55
【问题描述】:
我在将制表符分隔的文件输入到stanford classifier 时遇到问题。
虽然我能够成功浏览所有包含的斯坦福教程,包括新闻组教程,但当我尝试输入自己的训练和测试数据时,它无法正确加载。
起初我认为问题在于我使用 Excel 电子表格将数据保存到制表符分隔的文件中,这是某种编码问题。
但是当我执行以下操作时,我得到了完全相同的结果。首先,我将下面的演示数据逐字输入 gedit,确保在政治/体育类和随后的文本之间使用一个选项卡:
politics Obama today announced a new immigration policy.
sports The NBA all-star game was last weekend.
politics Both parties are eyeing the next midterm elections.
politics Congress votes tomorrow on electoral reforms.
sports The Lakers lost again last night, 102-100.
politics The Supreme Court will rule on gay marriage this spring.
sports The Red Sox report to spring training in two weeks.
sports Messi set a world record for goals in a calendar year in 2012.
politics The Senate will vote on a new budget proposal next week.
politics The President declared on Friday that he will veto any budget that doesn't include revenue increases.
我将其保存为myproject/demo-train.txt,并将类似的文件保存为myproject/demo-test.txt。
然后我运行了以下命令:
java -mx1800m -cp stanford-classifier.jar edu.stanford.nlp.classify.ColumnDataClassifier
-trainFile myproject/demo-train.txt -testFile myproject/demo-test.txt
好消息:这实际上运行时没有抛出任何错误。
坏消息:由于它不提取任何特征,因此它实际上无法估计真实模型,并且每个项目的概率默认为1/n,其中n 是类的数量。
然后我运行了相同的命令,但指定了两个基本选项:
java -mx1800m -cp stanford-classifier.jar edu.stanford.nlp.classify.ColumnDataClassifier
-trainFile myproject/demo-train.txt -testFile myproject/demo-test.txt -2.useSplitWords =2.splitWordsRegexp "\s+"
结果是:
Exception in thread "main" java.lang.RuntimeException: Training dataset could not be processed
at edu.stanford.nlp.classify.ColumnDataClassifier.readDataset(ColumnDataClassifier.java:402)
at edu.stanford.nlp.classify.ColumnDataClassifier.readTrainingExamples (ColumnDataClassifier.java:317)
at edu.stanford.nlp.classify.ColumnDataClassifier.trainClassifier(ColumnDataClassifier.java:1652)
at edu.stanford.nlp.classify.ColumnDataClassifier.main(ColumnDataClassifier.java:1628)
Caused by: java.lang.ArrayIndexOutOfBoundsException: 2
at edu.stanford.nlp.classify.ColumnDataClassifier.makeDatum(ColumnDataClassifier.java:670)
at edu.stanford.nlp.classify.ColumnDataClassifier.makeDatumFromLine(ColumnDataClassifier.java:267)
at edu.stanford.nlp.classify.ColumnDataClassifier.makeDatum(ColumnDataClassifier.java:396)
... 3 more
这些与我使用从 Excel 保存的真实数据时得到的结果完全相同。
更重要的是,我不知道如何理解ArrayIndexOutOfBoundsException。当我在 python 中使用readline 打印出我创建的演示文件和有效的教程文件的原始字符串时,格式似乎没有什么不同。所以我不知道为什么一组文件会引发此异常,而另一组则不会。
最后,还有一个怪癖。在某一时刻,我认为换行符可能是问题所在。所以我从演示文件中删除了所有换行符,同时保留了制表符并运行了相同的命令:
java -mx1800m -cp stanford-classifier.jar edu.stanford.nlp.classify.ColumnDataClassifier
-trainFile myproject/demo-train.txt -testFile myproject/demo-test.txt -2.useSplitWords =2.splitWordsRegexp "\s+"
令人惊讶的是,这次没有抛出 java 异常。但同样,它毫无价值:它将整个文件视为一个观察结果,因此无法正确拟合模型。
我现在已经在这上面花了 8 个小时,并且已经用尽了我能想到的一切。我是 Java 新手,但我认为这不应该成为问题 - 根据斯坦福大学的 API documentation for ColumnDataClassifier,只需要一个制表符分隔的文件。
任何帮助将不胜感激。
最后一点:我在 Windows 和 Ubuntu 上都使用相同的文件运行了这些相同的命令,结果都相同。
【问题讨论】:
-
我希望复制您的问题以尝试帮助您,但我很抱歉要求您提供更多信息,因为您已经提供了这么多信息。 :) 但是你在这里学习的教程在哪里?我想我需要 demo-train.txt 和 demo-test.txt 才能复制它。谢谢。
-
谢谢@dmn。您可以找到文件here,这是一个包含我的演示文件、我实际想要使用的文件以及有效的教程文件的 zip 文件。教程本身位于Stanford wiki here。非常感谢您观看。
标签: nlp stanford-nlp csv