How do we get run Stanford Classifier on an array of Strings?

北慕城南 提交于 2020-01-05 13:12:50

问题


I have got an array of strings

String strarr[] = {
        "What a wonderful day",  
        "beautiful beds",
        "food was awesome"
    };

I also have a trained dataset

Room    What a beautiful room
Room    Wonderful sea-view
Room    beds are comfortable
Room    bed-spreads are good
Food    The dinner was marvellous
Food    Tasty foods
Service people are rude
Service waitors were not on time
Service service was horrible

Pogrammatically I am unable to get the scores and labels of the strings I want to classify. If however, I am using a train dataset, with the two columns like in the test dataset, it works. My problem is, in reality, it is not possible to understand which label falls to each of the strings in my array.

How can get the classifier to run on the array, instead of creating a train dataset?

I got an error when trying to compute

ColumnDataClassifier cdc = new ColumnDataClassifier("examples/drogo.prop");
        Classifier<String, String> cl
            = cdc.makeClassifier(cdc.readTrainingExamples("examples/drogo.train"));

        for (String li : strarr){
            Datum<String, String> d = cdc.makeDatumFromLine(li);

            System.out.println(li + "  ==>  " + cl.classOf(d) + " (score: " + cl.scoresOf(d) + ")");
        }

Error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    at edu.stanford.nlp.classify.ColumnDataClassifier.makeDatum(ColumnDataClassifier.java:738)
    at edu.stanford.nlp.classify.ColumnDataClassifier.makeDatumFromStrings(ColumnDataClassifier.java:275)
    at edu.stanford.nlp.classify.ColumnDataClassifier.makeDatumFromLine(ColumnDataClassifier.java:245)
    at alchemypoc.DrogoClassifier.main(DrogoClassifier.java:55)
Java Result: 1

回答1:


Okay, so I did the following and it now seemed to work. Since it was a ColumnDataClassifier and it somehow expected columnar data, I added a tab before each sentence.

String strarr[] = {
            "\tWhat a wonderful day",
            "\tbeautiful beds",
            "\tfood was awesome"
        };

It now gives me the values.

What a wonderful day  ==>  Room (score: {Service=-0.6692784244930884, Room=1.4113604761865859, Food=-0.7420810715491954})
    beautiful beds  ==>  Room (score: {Service=-2.1042147142001038, Room=3.888249805012589, Food=-1.7840358277259})
    food was awesome  ==>  Food (score: {Service=-0.44203328206155995, Room=-0.9779506257026013, Food=1.4199861760769543})

If anyone, has a different answer or a more correct way to do this, please do post your answers.



来源:https://stackoverflow.com/questions/28601653/how-do-we-get-run-stanford-classifier-on-an-array-of-strings

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!