Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boilerpipe - How do I output JSON?

I am using boilerpipe and it seems great, but I want to output JSON. I am using the Java version and testing in NetBeans as follows:

final URL url = new URL("http://mashable.com/2012/09/26/worlds-best-father-kickstarter-calendar");
System.out.println(ArticleExtractor.INSTANCE.getText(url));

Can anyone tell me how I go about this?

like image 439
Wadester Avatar asked Nov 03 '22 15:11

Wadester


1 Answers

Boilerpipe does not come with a JSON serializer.

You can, however, do this (assuming you already extracted all data):

public String articleTextToJson(String article, String title, String sourceUrl) {
    if (null == article) {
        return "{ \"error\" : { " +
               "       \"message\" : \"Article did not extract\", " +
               "       \"code\" : 1 " +
               "    }, " +
               "  \"status\" : \"error\" " +
               "}";
    }
    return "{ \"response\" : { " +
           "       \"title\" : \"" + title + "\" " +
           "       \"content\" : \"" + article + "\", " +
           "       \"source\" : \"" + sourceUrl + "\" " +
           "    }, " +
           "  \"status\" : \"success\" " +
           "}"
}

The tricky part will be of course getting the title...

Or better yet use some JSON serializer like JSONObject.

Hope that helps.

like image 70
Ofir Farchy Avatar answered Nov 11 '22 22:11

Ofir Farchy