From 4bb32d22a698a26e8791d22cb8d444791069753e Mon Sep 17 00:00:00 2001 From: Joshua Holmgren Date: Tue, 15 Jul 2014 15:34:30 -0600 Subject: [PATCH 1/3] changed return value of AlchemyAPI class memeber functions to String type so that JSON can be output. --- .gitignore | 3 +- src/com/alchemyapi/api/AlchemyAPI.java | 263 +++++++++++------- src/com/alchemyapi/api/AlchemyAPI_Params.java | 3 +- src/com/alchemyapi/test/AuthorTest.java | 6 +- src/com/alchemyapi/test/CategoryTest.java | 10 +- src/com/alchemyapi/test/CombinedTest.java | 8 +- src/com/alchemyapi/test/ConceptTest.java | 8 +- .../alchemyapi/test/ConstraintQueryTest.java | 6 +- src/com/alchemyapi/test/EntityTest.java | 17 +- src/com/alchemyapi/test/FeedLinksTest.java | 6 +- src/com/alchemyapi/test/ImageTest.java | 8 +- src/com/alchemyapi/test/KeywordTest.java | 8 +- src/com/alchemyapi/test/LanguageTest.java | 8 +- src/com/alchemyapi/test/MicroformatTest.java | 6 +- src/com/alchemyapi/test/ParameterTest.java | 8 +- src/com/alchemyapi/test/RelationsTest.java | 12 +- src/com/alchemyapi/test/SentimentTest.java | 18 +- src/com/alchemyapi/test/TaxonomyTest.java | 8 +- src/com/alchemyapi/test/TextExtractTest.java | 14 +- 19 files changed, 249 insertions(+), 171 deletions(-) diff --git a/.gitignore b/.gitignore index 3497551..c170c9f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ bin -dist \ No newline at end of file +dist +testdir/api_key.txt \ No newline at end of file diff --git a/src/com/alchemyapi/api/AlchemyAPI.java b/src/com/alchemyapi/api/AlchemyAPI.java index 876138a..cdc9dc1 100644 --- a/src/com/alchemyapi/api/AlchemyAPI.java +++ b/src/com/alchemyapi/api/AlchemyAPI.java @@ -16,6 +16,8 @@ import javax.xml.parsers.ParserConfigurationException; import javax.xml.xpath.*; +//import com.google.gson.*; + import org.w3c.dom.Document; import org.w3c.dom.NodeList; @@ -80,14 +82,14 @@ public void SetAPIHost(String apiHost) { _requestUri = "http://" + apiHost + ".alchemyapi.com/calls/"; } - public Document URLGetAuthor(String url) + public String URLGetAuthor(String url) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { return URLGetAuthor(url, new AlchemyAPI_Params()); } - public Document URLGetAuthor(String url, AlchemyAPI_Params params) + public String URLGetAuthor(String url, AlchemyAPI_Params params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -98,14 +100,14 @@ public Document URLGetAuthor(String url, AlchemyAPI_Params params) return GET("URLGetAuthor", "url", params); } - public Document HTMLGetAuthor(String html, String url) + public String HTMLGetAuthor(String html, String url) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { return HTMLGetAuthor(html, url, new AlchemyAPI_Params()); } - public Document HTMLGetAuthor(String html, String url, + public String HTMLGetAuthor(String html, String url, AlchemyAPI_Params params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException @@ -118,14 +120,14 @@ public Document HTMLGetAuthor(String html, String url, return POST("HTMLGetAuthor", "html", params); } - public Document URLGetRankedNamedEntities(String url) + public String URLGetRankedNamedEntities(String url) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { return URLGetRankedNamedEntities(url, new AlchemyAPI_NamedEntityParams()); } - public Document URLGetRankedNamedEntities(String url, AlchemyAPI_NamedEntityParams params) + public String URLGetRankedNamedEntities(String url, AlchemyAPI_NamedEntityParams params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -136,7 +138,7 @@ public Document URLGetRankedNamedEntities(String url, AlchemyAPI_NamedEntityPara return GET("URLGetRankedNamedEntities", "url", params); } - public Document HTMLGetRankedNamedEntities(String html, String url) + public String HTMLGetRankedNamedEntities(String html, String url) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -144,7 +146,7 @@ public Document HTMLGetRankedNamedEntities(String html, String url) } - public Document HTMLGetRankedNamedEntities(String html, String url, AlchemyAPI_NamedEntityParams params) + public String HTMLGetRankedNamedEntities(String html, String url, AlchemyAPI_NamedEntityParams params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -156,14 +158,14 @@ public Document HTMLGetRankedNamedEntities(String html, String url, AlchemyAPI_N return POST("HTMLGetRankedNamedEntities", "html", params); } - public Document TextGetRankedNamedEntities(String text) + public String TextGetRankedNamedEntities(String text) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { return TextGetRankedNamedEntities(text, new AlchemyAPI_NamedEntityParams()); } - public Document TextGetRankedNamedEntities(String text, AlchemyAPI_NamedEntityParams params) + public String TextGetRankedNamedEntities(String text, AlchemyAPI_NamedEntityParams params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -176,14 +178,14 @@ public Document TextGetRankedNamedEntities(String text, AlchemyAPI_NamedEntityPa - public Document URLGetRankedConcepts(String url) + public String URLGetRankedConcepts(String url) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { return URLGetRankedConcepts(url, new AlchemyAPI_ConceptParams()); } - public Document URLGetRankedConcepts(String url, AlchemyAPI_ConceptParams params) + public String URLGetRankedConcepts(String url, AlchemyAPI_ConceptParams params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -195,14 +197,14 @@ public Document URLGetRankedConcepts(String url, AlchemyAPI_ConceptParams params } - public Document HTMLGetRankedConcepts(String html, String url) + public String HTMLGetRankedConcepts(String html, String url) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { return HTMLGetRankedConcepts(html, url, new AlchemyAPI_ConceptParams()); } - public Document HTMLGetRankedConcepts(String html, String url, AlchemyAPI_ConceptParams params) + public String HTMLGetRankedConcepts(String html, String url, AlchemyAPI_ConceptParams params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -214,12 +216,12 @@ public Document HTMLGetRankedConcepts(String html, String url, AlchemyAPI_Concep return POST("HTMLGetRankedConcepts", "html", params); } - public Document TextGetRankedConcepts(String text) throws IOException, SAXException, + public String TextGetRankedConcepts(String text) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { return TextGetRankedConcepts(text, new AlchemyAPI_ConceptParams()); } - public Document TextGetRankedConcepts(String text, AlchemyAPI_ConceptParams params) throws IOException, SAXException, + public String TextGetRankedConcepts(String text, AlchemyAPI_ConceptParams params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { CheckText(text); @@ -231,14 +233,14 @@ public Document TextGetRankedConcepts(String text, AlchemyAPI_ConceptParams para - public Document URLGetRankedKeywords(String url) + public String URLGetRankedKeywords(String url) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { return URLGetRankedKeywords(url, new AlchemyAPI_KeywordParams()); } - public Document URLGetRankedKeywords(String url, AlchemyAPI_KeywordParams params) + public String URLGetRankedKeywords(String url, AlchemyAPI_KeywordParams params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -250,14 +252,14 @@ public Document URLGetRankedKeywords(String url, AlchemyAPI_KeywordParams params } - public Document HTMLGetRankedKeywords(String html, String url) + public String HTMLGetRankedKeywords(String html, String url) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { return HTMLGetRankedKeywords(html, url, new AlchemyAPI_KeywordParams()); } - public Document HTMLGetRankedKeywords(String html, String url, AlchemyAPI_KeywordParams params) + public String HTMLGetRankedKeywords(String html, String url, AlchemyAPI_KeywordParams params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -269,12 +271,12 @@ public Document HTMLGetRankedKeywords(String html, String url, AlchemyAPI_Keywor return POST("HTMLGetRankedKeywords", "html", params); } - public Document TextGetRankedKeywords(String text) throws IOException, SAXException, + public String TextGetRankedKeywords(String text) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { return TextGetRankedKeywords(text, new AlchemyAPI_KeywordParams()); } - public Document TextGetRankedKeywords(String text, AlchemyAPI_KeywordParams params) throws IOException, SAXException, + public String TextGetRankedKeywords(String text, AlchemyAPI_KeywordParams params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { CheckText(text); @@ -284,14 +286,14 @@ public Document TextGetRankedKeywords(String text, AlchemyAPI_KeywordParams para return POST("TextGetRankedKeywords", "text", params); } - public Document URLGetLanguage(String url) + public String URLGetLanguage(String url) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { return URLGetLanguage(url, new AlchemyAPI_LanguageParams()); } - public Document URLGetLanguage(String url, AlchemyAPI_LanguageParams params) + public String URLGetLanguage(String url, AlchemyAPI_LanguageParams params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -302,14 +304,14 @@ public Document URLGetLanguage(String url, AlchemyAPI_LanguageParams params) return GET("URLGetLanguage", "url", params); } - public Document HTMLGetLanguage(String html, String url) + public String HTMLGetLanguage(String html, String url) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { return HTMLGetLanguage(html, url, new AlchemyAPI_LanguageParams()); } - public Document HTMLGetLanguage(String html, String url, AlchemyAPI_LanguageParams params) + public String HTMLGetLanguage(String html, String url, AlchemyAPI_LanguageParams params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -321,14 +323,14 @@ public Document HTMLGetLanguage(String html, String url, AlchemyAPI_LanguagePara return POST("HTMLGetLanguage", "html", params); } - public Document TextGetLanguage(String text) + public String TextGetLanguage(String text) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { return TextGetLanguage(text, new AlchemyAPI_LanguageParams()); } - public Document TextGetLanguage(String text, AlchemyAPI_LanguageParams params) + public String TextGetLanguage(String text, AlchemyAPI_LanguageParams params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -339,14 +341,14 @@ public Document TextGetLanguage(String text, AlchemyAPI_LanguageParams params) return POST("TextGetLanguage", "text", params); } - public Document URLGetCategory(String url) + public String URLGetCategory(String url) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { return URLGetCategory(url, new AlchemyAPI_CategoryParams()); } - public Document URLGetCategory(String url, AlchemyAPI_CategoryParams params) + public String URLGetCategory(String url, AlchemyAPI_CategoryParams params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -357,14 +359,14 @@ public Document URLGetCategory(String url, AlchemyAPI_CategoryParams params) return GET("URLGetCategory", "url", params); } - public Document HTMLGetCategory(String html, String url) + public String HTMLGetCategory(String html, String url) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { return HTMLGetCategory(html, url, new AlchemyAPI_CategoryParams()); } - public Document HTMLGetCategory(String html, String url, AlchemyAPI_CategoryParams params) + public String HTMLGetCategory(String html, String url, AlchemyAPI_CategoryParams params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -376,14 +378,14 @@ public Document HTMLGetCategory(String html, String url, AlchemyAPI_CategoryPara return POST("HTMLGetCategory", "html", params); } - public Document TextGetCategory(String text) + public String TextGetCategory(String text) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { return TextGetCategory(text, new AlchemyAPI_TextParams()); } - public Document TextGetCategory(String text, AlchemyAPI_TextParams params) + public String TextGetCategory(String text, AlchemyAPI_TextParams params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -394,14 +396,14 @@ public Document TextGetCategory(String text, AlchemyAPI_TextParams params) return POST("TextGetCategory", "text", params); } - public Document URLGetText(String url) + public String URLGetText(String url) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { return URLGetText(url, new AlchemyAPI_TextParams()); } - public Document URLGetText(String url, AlchemyAPI_TextParams params) + public String URLGetText(String url, AlchemyAPI_TextParams params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -412,14 +414,14 @@ public Document URLGetText(String url, AlchemyAPI_TextParams params) return GET("URLGetText", "url", params); } - public Document HTMLGetText(String html, String url) + public String HTMLGetText(String html, String url) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { return HTMLGetText(html, url, new AlchemyAPI_TextParams()); } - public Document HTMLGetText(String html, String url, AlchemyAPI_TextParams params) + public String HTMLGetText(String html, String url, AlchemyAPI_TextParams params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -431,14 +433,14 @@ public Document HTMLGetText(String html, String url, AlchemyAPI_TextParams param return POST("HTMLGetText", "html", params); } - public Document URLGetRawText(String url) + public String URLGetRawText(String url) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { return URLGetRawText(url, new AlchemyAPI_Params()); } - public Document URLGetRawText(String url, AlchemyAPI_Params params) + public String URLGetRawText(String url, AlchemyAPI_Params params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -449,14 +451,14 @@ public Document URLGetRawText(String url, AlchemyAPI_Params params) return GET("URLGetRawText", "url", params); } - public Document HTMLGetRawText(String html, String url) + public String HTMLGetRawText(String html, String url) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { return HTMLGetRawText(html, url, new AlchemyAPI_Params()); } - public Document HTMLGetRawText(String html, String url, AlchemyAPI_Params params) + public String HTMLGetRawText(String html, String url, AlchemyAPI_Params params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -468,14 +470,14 @@ public Document HTMLGetRawText(String html, String url, AlchemyAPI_Params params return POST("HTMLGetRawText", "html", params); } - public Document URLGetTitle(String url) + public String URLGetTitle(String url) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { return URLGetTitle(url, new AlchemyAPI_Params()); } - public Document URLGetTitle(String url, AlchemyAPI_Params params) + public String URLGetTitle(String url, AlchemyAPI_Params params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -486,14 +488,14 @@ public Document URLGetTitle(String url, AlchemyAPI_Params params) return GET("URLGetTitle", "url", params); } - public Document HTMLGetTitle(String html, String url) + public String HTMLGetTitle(String html, String url) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { return HTMLGetTitle(html, url, new AlchemyAPI_Params()); } - public Document HTMLGetTitle(String html, String url, AlchemyAPI_Params params) + public String HTMLGetTitle(String html, String url, AlchemyAPI_Params params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -505,14 +507,14 @@ public Document HTMLGetTitle(String html, String url, AlchemyAPI_Params params) return POST("HTMLGetTitle", "html", params); } - public Document URLGetFeedLinks(String url) + public String URLGetFeedLinks(String url) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { return URLGetFeedLinks(url, new AlchemyAPI_Params()); } - public Document URLGetFeedLinks(String url, AlchemyAPI_Params params) + public String URLGetFeedLinks(String url, AlchemyAPI_Params params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -523,14 +525,14 @@ public Document URLGetFeedLinks(String url, AlchemyAPI_Params params) return GET("URLGetFeedLinks", "url", params); } - public Document HTMLGetFeedLinks(String html, String url) + public String HTMLGetFeedLinks(String html, String url) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { return HTMLGetFeedLinks(html, url, new AlchemyAPI_Params()); } - public Document HTMLGetFeedLinks(String html, String url, AlchemyAPI_Params params) + public String HTMLGetFeedLinks(String html, String url, AlchemyAPI_Params params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -542,14 +544,14 @@ public Document HTMLGetFeedLinks(String html, String url, AlchemyAPI_Params para return POST("HTMLGetFeedLinks", "html", params); } - public Document URLGetMicroformats(String url) + public String URLGetMicroformats(String url) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { return URLGetMicroformats(url, new AlchemyAPI_Params()); } - public Document URLGetMicroformats(String url, AlchemyAPI_Params params) + public String URLGetMicroformats(String url, AlchemyAPI_Params params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -560,14 +562,14 @@ public Document URLGetMicroformats(String url, AlchemyAPI_Params params) return GET("URLGetMicroformatData", "url", params); } - public Document HTMLGetMicroformats(String html, String url) + public String HTMLGetMicroformats(String html, String url) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { return HTMLGetMicroformats(html, url, new AlchemyAPI_Params()); } - public Document HTMLGetMicroformats(String html, String url, AlchemyAPI_Params params) + public String HTMLGetMicroformats(String html, String url, AlchemyAPI_Params params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -579,14 +581,14 @@ public Document HTMLGetMicroformats(String html, String url, AlchemyAPI_Params p return POST("HTMLGetMicroformatData", "html", params); } - public Document URLGetConstraintQuery(String url, String query) + public String URLGetConstraintQuery(String url, String query) throws IOException, XPathExpressionException, SAXException, ParserConfigurationException { return URLGetConstraintQuery(url, query, new AlchemyAPI_ConstraintQueryParams()); } - public Document URLGetConstraintQuery(String url, String query, AlchemyAPI_ConstraintQueryParams params) + public String URLGetConstraintQuery(String url, String query, AlchemyAPI_ConstraintQueryParams params) throws IOException, XPathExpressionException, SAXException, ParserConfigurationException { @@ -601,14 +603,14 @@ public Document URLGetConstraintQuery(String url, String query, AlchemyAPI_Const } - public Document HTMLGetConstraintQuery(String html, String url, String query) + public String HTMLGetConstraintQuery(String html, String url, String query) throws IOException, XPathExpressionException, SAXException, ParserConfigurationException { return HTMLGetConstraintQuery(html, url, query, new AlchemyAPI_ConstraintQueryParams()); } - public Document HTMLGetConstraintQuery(String html, String url, String query, AlchemyAPI_ConstraintQueryParams params) + public String HTMLGetConstraintQuery(String html, String url, String query, AlchemyAPI_ConstraintQueryParams params) throws IOException, XPathExpressionException, SAXException, ParserConfigurationException { @@ -623,14 +625,14 @@ public Document HTMLGetConstraintQuery(String html, String url, String query, Al return POST("HTMLGetConstraintQuery", "html", params); } - public Document URLGetTextSentiment(String url) + public String URLGetTextSentiment(String url) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { return URLGetTextSentiment(url, new AlchemyAPI_Params()); } - public Document URLGetTextSentiment(String url, AlchemyAPI_Params params) + public String URLGetTextSentiment(String url, AlchemyAPI_Params params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -642,14 +644,14 @@ public Document URLGetTextSentiment(String url, AlchemyAPI_Params params) } - public Document HTMLGetTextSentiment(String html, String url) + public String HTMLGetTextSentiment(String html, String url) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { return HTMLGetTextSentiment(html, url, new AlchemyAPI_Params()); } - public Document HTMLGetTextSentiment(String html, String url, AlchemyAPI_Params params) + public String HTMLGetTextSentiment(String html, String url, AlchemyAPI_Params params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -661,14 +663,14 @@ public Document HTMLGetTextSentiment(String html, String url, AlchemyAPI_Params return POST("HTMLGetTextSentiment", "html", params); } - public Document TextGetTextSentiment(String text) + public String TextGetTextSentiment(String text) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { return TextGetTextSentiment(text, new AlchemyAPI_Params()); } - public Document TextGetTextSentiment(String text, AlchemyAPI_Params params) + public String TextGetTextSentiment(String text, AlchemyAPI_Params params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -681,7 +683,7 @@ public Document TextGetTextSentiment(String text, AlchemyAPI_Params params) //------------------ - public Document URLGetTargetedSentiment(String url, String target) + public String URLGetTargetedSentiment(String url, String target) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -689,7 +691,7 @@ public Document URLGetTargetedSentiment(String url, String target) new AlchemyAPI_TargetedSentimentParams()); } - public Document URLGetTargetedSentiment(String url, String target, + public String URLGetTargetedSentiment(String url, String target, AlchemyAPI_TargetedSentimentParams params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException @@ -703,7 +705,7 @@ public Document URLGetTargetedSentiment(String url, String target, return GET("URLGetTargetedSentiment", "url", params); } - public Document HTMLGetTargetedSentiment(String html, String url, String target) + public String HTMLGetTargetedSentiment(String html, String url, String target) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -711,7 +713,7 @@ public Document HTMLGetTargetedSentiment(String html, String url, String target) new AlchemyAPI_TargetedSentimentParams()); } - public Document HTMLGetTargetedSentiment(String html, String url, String target, + public String HTMLGetTargetedSentiment(String html, String url, String target, AlchemyAPI_TargetedSentimentParams params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException @@ -726,7 +728,7 @@ public Document HTMLGetTargetedSentiment(String html, String url, String target, return POST("HTMLGetTargetedSentiment", "html", params); } - public Document TextGetTargetedSentiment(String text, String target) + public String TextGetTargetedSentiment(String text, String target) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -734,7 +736,7 @@ public Document TextGetTargetedSentiment(String text, String target) new AlchemyAPI_TargetedSentimentParams()); } - public Document TextGetTargetedSentiment(String text, String target, + public String TextGetTargetedSentiment(String text, String target, AlchemyAPI_TargetedSentimentParams params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException @@ -750,13 +752,13 @@ public Document TextGetTargetedSentiment(String text, String target, //------------------ - public Document URLGetRelations(String url) throws IOException, SAXException, + public String URLGetRelations(String url) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { return URLGetRelations(url, new AlchemyAPI_RelationParams()); } - public Document URLGetRelations(String url, AlchemyAPI_RelationParams params) + public String URLGetRelations(String url, AlchemyAPI_RelationParams params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -767,14 +769,14 @@ public Document URLGetRelations(String url, AlchemyAPI_RelationParams params) return GET("URLGetRelations", "url", params); } - public Document HTMLGetRelations(String html, String url) + public String HTMLGetRelations(String html, String url) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { return HTMLGetRelations(html, url, new AlchemyAPI_RelationParams()); } - public Document HTMLGetRelations(String html, String url, AlchemyAPI_RelationParams params) + public String HTMLGetRelations(String html, String url, AlchemyAPI_RelationParams params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -786,14 +788,14 @@ public Document HTMLGetRelations(String html, String url, AlchemyAPI_RelationPar return POST("HTMLGetRelations", "html", params); } - public Document TextGetRelations(String text) + public String TextGetRelations(String text) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { return TextGetRelations(text, new AlchemyAPI_RelationParams()); } - public Document TextGetRelations(String text, AlchemyAPI_RelationParams params) + public String TextGetRelations(String text, AlchemyAPI_RelationParams params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -806,7 +808,7 @@ public Document TextGetRelations(String text, AlchemyAPI_RelationParams params) //------------------ - public Document URLGetCombined(String url) throws IOException, SAXException, + public String URLGetCombined(String url) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { AlchemyAPI_CombinedParams params = new AlchemyAPI_CombinedParams(); @@ -814,7 +816,7 @@ public Document URLGetCombined(String url) throws IOException, SAXException, return URLGetCombined(url, params); } - public Document URLGetCombined(String url, AlchemyAPI_CombinedParams params) + public String URLGetCombined(String url, AlchemyAPI_CombinedParams params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -825,7 +827,7 @@ public Document URLGetCombined(String url, AlchemyAPI_CombinedParams params) return GET("URLGetCombinedData", "url", params); } - public Document TextGetCombined(String text) + public String TextGetCombined(String text) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -834,7 +836,7 @@ public Document TextGetCombined(String text) return TextGetCombined(text, params); } - public Document TextGetCombined(String text, AlchemyAPI_CombinedParams params) + public String TextGetCombined(String text, AlchemyAPI_CombinedParams params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -847,13 +849,13 @@ public Document TextGetCombined(String text, AlchemyAPI_CombinedParams params) //------------------ - public Document URLGetTaxonomy(String url) throws IOException, SAXException, + public String URLGetTaxonomy(String url) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { return URLGetTaxonomy(url, new AlchemyAPI_TaxonomyParams()); } - public Document URLGetTaxonomy(String url, AlchemyAPI_TaxonomyParams params) + public String URLGetTaxonomy(String url, AlchemyAPI_TaxonomyParams params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -864,14 +866,14 @@ public Document URLGetTaxonomy(String url, AlchemyAPI_TaxonomyParams params) return GET("URLGetRankedTaxonomy", "url", params); } - public Document HTMLGetTaxonomy(String html, String url) + public String HTMLGetTaxonomy(String html, String url) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { return HTMLGetTaxonomy(html, url, new AlchemyAPI_TaxonomyParams()); } - public Document HTMLGetTaxonomy(String html, String url, AlchemyAPI_TaxonomyParams params) + public String HTMLGetTaxonomy(String html, String url, AlchemyAPI_TaxonomyParams params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -883,14 +885,14 @@ public Document HTMLGetTaxonomy(String html, String url, AlchemyAPI_TaxonomyPara return POST("HTMLGetRankedTaxonomy", "html", params); } - public Document TextGetTaxonomy(String text) + public String TextGetTaxonomy(String text) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { return TextGetTaxonomy(text, new AlchemyAPI_TaxonomyParams()); } - public Document TextGetTaxonomy(String text, AlchemyAPI_TaxonomyParams params) + public String TextGetTaxonomy(String text, AlchemyAPI_TaxonomyParams params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -903,13 +905,13 @@ public Document TextGetTaxonomy(String text, AlchemyAPI_TaxonomyParams params) //------------------ - public Document URLGetImage(String url) throws IOException, SAXException, + public String URLGetImage(String url) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { return URLGetImage(url, new AlchemyAPI_ImageParams()); } - public Document URLGetImage(String url, AlchemyAPI_ImageParams params) + public String URLGetImage(String url, AlchemyAPI_ImageParams params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -920,13 +922,13 @@ public Document URLGetImage(String url, AlchemyAPI_ImageParams params) return GET("URLGetImage", "url", params); } - public Document URLGetRankedImageKeywords(String url) throws IOException, SAXException, + public String URLGetRankedImageKeywords(String url) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { return URLGetRankedImageKeywords(url, new AlchemyAPI_ImageParams()); } - public Document URLGetRankedImageKeywords(String url, AlchemyAPI_ImageParams params) + public String URLGetRankedImageKeywords(String url, AlchemyAPI_ImageParams params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -937,7 +939,7 @@ public Document URLGetRankedImageKeywords(String url, AlchemyAPI_ImageParams par return GET("URLGetRankedImageKeywords", "url", params); } - public Document ImageGetRankedImageKeywords(AlchemyAPI_ImageParams params) + public String ImageGetRankedImageKeywords(AlchemyAPI_ImageParams params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -976,7 +978,7 @@ private void CheckURL(String url) { throw new IllegalArgumentException("Enter an URL to analyze."); } - private Document GET(String callName, String callPrefix, AlchemyAPI_Params params) + /*private Document GET(String callName, String callPrefix, AlchemyAPI_Params params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -990,9 +992,24 @@ private Document GET(String callName, String callPrefix, AlchemyAPI_Params param handle.setDoOutput(true); return doRequest(handle, params.getOutputMode()); - } + }*/ + private String GET(String callName, String callPrefix, AlchemyAPI_Params params) + throws IOException, SAXException, + ParserConfigurationException, XPathExpressionException + { + StringBuilder uri = new StringBuilder(); + uri.append(_requestUri).append(callPrefix).append('/').append(callName) + .append('?').append("apikey=").append(this._apiKey); + uri.append(params.getParameterString()); + + URL url = new URL(uri.toString()); + HttpURLConnection handle = (HttpURLConnection) url.openConnection(); + handle.setDoOutput(true); + + return doRequest(handle, params.getOutputMode()); + } - private Document POST(String callName, String callPrefix, AlchemyAPI_Params params) + private String POST(String callName, String callPrefix, AlchemyAPI_Params params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -1014,14 +1031,58 @@ private Document POST(String callName, String callPrefix, AlchemyAPI_Params para return doRequest(handle, params.getOutputMode()); } + /*private Document POST(String callName, String callPrefix, AlchemyAPI_Params params) + throws IOException, SAXException, + ParserConfigurationException, XPathExpressionException + { + URL url = new URL(_requestUri + callPrefix + "/" + callName); + + HttpURLConnection handle = (HttpURLConnection) url.openConnection(); + handle.setDoOutput(true); + + StringBuilder data = new StringBuilder(); + + data.append("apikey=").append(this._apiKey); + data.append(params.getParameterString()); + + handle.addRequestProperty("Content-Length", Integer.toString(data.length())); + + DataOutputStream ostream = new DataOutputStream(handle.getOutputStream()); + ostream.write(data.toString().getBytes()); + ostream.close(); + + return doRequest(handle, params.getOutputMode()); + }*/ + + + private String doRequest(HttpURLConnection handle, String outputMode) + throws IOException, SAXException, + ParserConfigurationException, XPathExpressionException + { + String doc = ""; + BufferedReader in = new BufferedReader(new InputStreamReader(handle.getInputStream())); + String inputLine; + while ((inputLine = in.readLine()) != null){ + doc += inputLine + "\n"; + } + in.close(); + handle.disconnect(); + + return doc; + } + + /* private Document doRequest(HttpURLConnection handle, String outputMode) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { DataInputStream istream = new DataInputStream(handle.getInputStream()); - Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(istream); + + + Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(istream); + istream.close(); handle.disconnect(); @@ -1047,6 +1108,16 @@ else if(AlchemyAPI_Params.OUTPUT_RDF.equals(outputMode)) { throw new IOException("Error making API call: " + statusStr + '.'); } } + else if(AlchemyAPI_Params.OUTPUT_JSON.equals(outputMode)) { + String statusStr = getNodeValue(factory, doc, "//JSON/Description/ResultStatus/text()"); + if (null == statusStr || !statusStr.equals("OK")) { + String statusInfoStr = getNodeValue(factory, doc, "//JSON/Description/ResultStatus/text()"); + if (null != statusInfoStr && statusInfoStr.length() > 0) + throw new IOException("Error making API call: " + statusInfoStr + '.'); + + throw new IOException("Error making API call: " + statusStr + '.'); + } + } return doc; } @@ -1063,5 +1134,5 @@ private String getNodeValue(XPathFactory factory, Document doc, String xpathStr) return results.item(0).getNodeValue(); return null; - } + }*/ } diff --git a/src/com/alchemyapi/api/AlchemyAPI_Params.java b/src/com/alchemyapi/api/AlchemyAPI_Params.java index 3e1f8d3..c41d3c1 100644 --- a/src/com/alchemyapi/api/AlchemyAPI_Params.java +++ b/src/com/alchemyapi/api/AlchemyAPI_Params.java @@ -8,6 +8,7 @@ public class AlchemyAPI_Params { public static final String OUTPUT_XML = "xml"; public static final String OUTPUT_RDF = "rdf"; + public static final String OUTPUT_JSON = "json"; private String url; private String html; @@ -37,7 +38,7 @@ public String getOutputMode() { return outputMode; } public void setOutputMode(String outputMode) { - if( !outputMode.equals(AlchemyAPI_Params.OUTPUT_XML) && !outputMode.equals(OUTPUT_RDF) ) + if( !outputMode.equals(AlchemyAPI_Params.OUTPUT_XML) && !outputMode.equals(AlchemyAPI_Params.OUTPUT_RDF) && !outputMode.equals(AlchemyAPI_Params.OUTPUT_JSON)) { throw new RuntimeException("Invalid setting " + outputMode + " for parameter outputMode"); } diff --git a/src/com/alchemyapi/test/AuthorTest.java b/src/com/alchemyapi/test/AuthorTest.java index 832535a..8897dfe 100644 --- a/src/com/alchemyapi/test/AuthorTest.java +++ b/src/com/alchemyapi/test/AuthorTest.java @@ -27,11 +27,11 @@ public static void main(String[] args) // Load a HTML document to analyze. String htmlDoc = getFileContents("data/example.html"); - Document doc = alchemyObj.URLGetAuthor("http://www.politico.com/blogs/media/2012/02/detroit-news-ed-upset-over-romney-edit-115247.html"); - System.out.println(getStringFromDocument(doc)); + String doc = alchemyObj.URLGetAuthor("http://www.politico.com/blogs/media/2012/02/detroit-news-ed-upset-over-romney-edit-115247.html"); + System.out.println(doc); doc = alchemyObj.HTMLGetAuthor(htmlDoc, "http://www.test.com/"); - System.out.println(getStringFromDocument(doc)); + System.out.println(doc); } // utility function diff --git a/src/com/alchemyapi/test/CategoryTest.java b/src/com/alchemyapi/test/CategoryTest.java index 9be3605..7a872c8 100644 --- a/src/com/alchemyapi/test/CategoryTest.java +++ b/src/com/alchemyapi/test/CategoryTest.java @@ -22,24 +22,24 @@ public static void main(String[] args) AlchemyAPI alchemyObj = AlchemyAPI.GetInstanceFromFile("api_key.txt"); // Categorize a web URL by topic. - Document doc = alchemyObj.URLGetCategory("http://www.techcrunch.com/"); - System.out.println(getStringFromDocument(doc)); + String doc = alchemyObj.URLGetCategory("http://www.techcrunch.com/"); + System.out.println(doc); // Categorize some text. doc = alchemyObj.TextGetCategory("Latest on the War in Iraq."); - System.out.println(getStringFromDocument(doc)); + System.out.println(doc); // Load a HTML document to analyze. String htmlDoc = getFileContents("data/example.html"); // Categorize a HTML document by topic. doc = alchemyObj.HTMLGetCategory(htmlDoc, "http://www.test.com/"); - System.out.println(getStringFromDocument(doc)); + System.out.println(doc); AlchemyAPI_CategoryParams categoryParams = new AlchemyAPI_CategoryParams(); categoryParams.setOutputMode(AlchemyAPI_Params.OUTPUT_RDF); doc = alchemyObj.HTMLGetCategory(htmlDoc, "http://www.test.com/", categoryParams); - System.out.println(getStringFromDocument(doc)); + System.out.println(doc); } // utility function diff --git a/src/com/alchemyapi/test/CombinedTest.java b/src/com/alchemyapi/test/CombinedTest.java index 7b68e5f..b548f28 100644 --- a/src/com/alchemyapi/test/CombinedTest.java +++ b/src/com/alchemyapi/test/CombinedTest.java @@ -22,14 +22,14 @@ public static void main(String[] args) AlchemyAPI alchemyObj = AlchemyAPI.GetInstanceFromFile("api_key.txt"); // Extract combined data for a web URL. - Document doc = alchemyObj.URLGetCombined("http://www.techcrunch.com/"); - System.out.println(getStringFromDocument(doc)); + String doc = alchemyObj.URLGetCombined("http://www.techcrunch.com/"); + System.out.println(doc); // Extract combined data from a text string. doc = alchemyObj.TextGetCombined( "Hello there, my name is Bob Jones. I live in the United States of America. " + "Where do you live, Fred?"); - System.out.println(getStringFromDocument(doc)); + System.out.println(doc); // Only extract entities & keywords AlchemyAPI_CombinedParams combinedParams = new AlchemyAPI_CombinedParams(); @@ -37,7 +37,7 @@ public static void main(String[] args) combinedParams.setExtract("entity"); combinedParams.setExtract("keyword"); doc = alchemyObj.TextGetCombined("Madonna enjoys tasty Pepsi. I love her style.", combinedParams); - System.out.println(getStringFromDocument(doc)); + System.out.println(doc); } // utility function diff --git a/src/com/alchemyapi/test/ConceptTest.java b/src/com/alchemyapi/test/ConceptTest.java index 69f71b8..108aeb5 100644 --- a/src/com/alchemyapi/test/ConceptTest.java +++ b/src/com/alchemyapi/test/ConceptTest.java @@ -20,20 +20,20 @@ public static void main(String[] args) throws IOException, SAXException, AlchemyAPI alchemyObj = AlchemyAPI.GetInstanceFromFile("api_key.txt"); // Extract concept tags for a web URL. - Document doc = alchemyObj.URLGetRankedConcepts("http://www.techcrunch.com/"); - System.out.println(getStringFromDocument(doc)); + String doc = alchemyObj.URLGetRankedConcepts("http://www.techcrunch.com/"); + System.out.println(doc); // Extract concept tags for a text string. doc = alchemyObj.TextGetRankedConcepts( "This thing has a steering wheel, tires, and an engine. Do you know what it is?"); - System.out.println(getStringFromDocument(doc)); + System.out.println(doc); // Load a HTML document to analyze. String htmlDoc = getFileContents("data/example.html"); // Extract concept tags for a HTML document. doc = alchemyObj.HTMLGetRankedConcepts(htmlDoc, "http://www.test.com/"); - System.out.println(getStringFromDocument(doc)); + System.out.println(doc); } // utility function diff --git a/src/com/alchemyapi/test/ConstraintQueryTest.java b/src/com/alchemyapi/test/ConstraintQueryTest.java index fd6f4f3..e5f78ef 100644 --- a/src/com/alchemyapi/test/ConstraintQueryTest.java +++ b/src/com/alchemyapi/test/ConstraintQueryTest.java @@ -22,14 +22,14 @@ public static void main(String[] args) AlchemyAPI alchemyObj = AlchemyAPI.GetInstanceFromFile("api_key.txt"); // Extract first link from an URL. - Document doc = alchemyObj.URLGetConstraintQuery("http://microformats.org/wiki/hcard", + String doc = alchemyObj.URLGetConstraintQuery("http://microformats.org/wiki/hcard", "1st link"); - System.out.println(getStringFromDocument(doc)); + System.out.println(doc); // Extract first link from a HTML. String htmlDoc = getFileContents("data/example.html"); doc = alchemyObj.HTMLGetConstraintQuery(htmlDoc, "http://www.test.com/", "1st link"); - System.out.println(getStringFromDocument(doc)); + System.out.println(doc); } // utility function diff --git a/src/com/alchemyapi/test/EntityTest.java b/src/com/alchemyapi/test/EntityTest.java index c9d5a41..1bdde66 100644 --- a/src/com/alchemyapi/test/EntityTest.java +++ b/src/com/alchemyapi/test/EntityTest.java @@ -1,6 +1,8 @@ package com.alchemyapi.test; import com.alchemyapi.api.AlchemyAPI; +import com.alchemyapi.api.AlchemyAPI_NamedEntityParams; +import com.alchemyapi.api.AlchemyAPI_Params; import org.xml.sax.SAXException; import org.w3c.dom.Document; @@ -21,22 +23,25 @@ public static void main(String[] args) // Create an AlchemyAPI object. AlchemyAPI alchemyObj = AlchemyAPI.GetInstanceFromFile("api_key.txt"); + AlchemyAPI_NamedEntityParams entityParams = new AlchemyAPI_NamedEntityParams(); + entityParams.setOutputMode(AlchemyAPI_Params.OUTPUT_JSON); + // Extract a ranked list of named entities for a web URL. - Document doc = alchemyObj.URLGetRankedNamedEntities("http://www.techcrunch.com/"); - System.out.println(getStringFromDocument(doc)); + String doc = alchemyObj.URLGetRankedNamedEntities("http://www.techcrunch.com/",entityParams); + System.out.println(doc); // Extract a ranked list of named entities from a text string. doc = alchemyObj.TextGetRankedNamedEntities( "Hello there, my name is Bob Jones. I live in the United States of America. " + - "Where do you live, Fred?"); - System.out.println(getStringFromDocument(doc)); + "Where do you live, Fred?",entityParams); + System.out.println(doc); // Load a HTML document to analyze. String htmlDoc = getFileContents("data/example.html"); // Extract a ranked list of named entities from a HTML document. - doc = alchemyObj.HTMLGetRankedNamedEntities(htmlDoc, "http://www.test.com/"); - System.out.println(getStringFromDocument(doc)); + doc = alchemyObj.HTMLGetRankedNamedEntities(htmlDoc, "http://www.test.com/",entityParams); + System.out.println(doc); } // utility function diff --git a/src/com/alchemyapi/test/FeedLinksTest.java b/src/com/alchemyapi/test/FeedLinksTest.java index 1d0563f..f7d46c3 100644 --- a/src/com/alchemyapi/test/FeedLinksTest.java +++ b/src/com/alchemyapi/test/FeedLinksTest.java @@ -22,15 +22,15 @@ public static void main(String[] args) AlchemyAPI alchemyObj = AlchemyAPI.GetInstanceFromFile("api_key.txt"); // Extract RSS / ATOM feed links from a web URL. - Document doc = alchemyObj.URLGetFeedLinks("http://www.techcrunch.com/"); - System.out.println(getStringFromDocument(doc)); + String doc = alchemyObj.URLGetFeedLinks("http://www.techcrunch.com/"); + System.out.println(doc); // Load a HTML document to analyze. String htmlDoc = getFileContents("data/example.html"); // Extract RSS / ATOM feed links from a HTML document. doc = alchemyObj.HTMLGetFeedLinks(htmlDoc, "http://www.test.com/"); - System.out.println(getStringFromDocument(doc)); + System.out.println(doc); } // utility function diff --git a/src/com/alchemyapi/test/ImageTest.java b/src/com/alchemyapi/test/ImageTest.java index af474b4..98c939b 100644 --- a/src/com/alchemyapi/test/ImageTest.java +++ b/src/com/alchemyapi/test/ImageTest.java @@ -23,12 +23,12 @@ public static void main(String[] args) AlchemyAPI alchemyObj = AlchemyAPI.GetInstanceFromFile("api_key.txt"); // Extract image for a web URL. - Document doc = alchemyObj.URLGetImage("http://www.techcrunch.com/"); - System.out.println(getStringFromDocument(doc)); + String doc = alchemyObj.URLGetImage("http://www.techcrunch.com/"); + System.out.println(doc); doc = alchemyObj.URLGetRankedImageKeywords( "http://farm4.staticflickr.com/3726/11043305726_fdcb7785ec_m.jpg"); - System.out.println(getStringFromDocument(doc)); + System.out.println(doc); byte[] imageByteArray = readFile("data/cat.jpg"); @@ -36,7 +36,7 @@ public static void main(String[] args) imageParams.setImage(imageByteArray); imageParams.setImagePostMode(AlchemyAPI_ImageParams.RAW); doc = alchemyObj.ImageGetRankedImageKeywords(imageParams); - System.out.println(getStringFromDocument(doc)); + System.out.println(doc); } // utility function diff --git a/src/com/alchemyapi/test/KeywordTest.java b/src/com/alchemyapi/test/KeywordTest.java index 6338548..0a5325a 100644 --- a/src/com/alchemyapi/test/KeywordTest.java +++ b/src/com/alchemyapi/test/KeywordTest.java @@ -20,21 +20,21 @@ public static void main(String[] args) throws IOException, SAXException, AlchemyAPI alchemyObj = AlchemyAPI.GetInstanceFromFile("api_key.txt"); // Extract topic keywords for a web URL. - Document doc = alchemyObj.URLGetRankedKeywords("http://www.techcrunch.com/"); - System.out.println(getStringFromDocument(doc)); + String doc = alchemyObj.URLGetRankedKeywords("http://www.techcrunch.com/"); + System.out.println(doc); // Extract topic keywords for a text string. doc = alchemyObj.TextGetRankedKeywords( "Hello there, my name is Bob Jones. I live in the United States of America. " + "Where do you live, Fred?"); - System.out.println(getStringFromDocument(doc)); + System.out.println(doc); // Load a HTML document to analyze. String htmlDoc = getFileContents("data/example.html"); // Extract topic keywords for a HTML document. doc = alchemyObj.HTMLGetRankedKeywords(htmlDoc, "http://www.test.com/"); - System.out.println(getStringFromDocument(doc)); + System.out.println(doc); } // utility function diff --git a/src/com/alchemyapi/test/LanguageTest.java b/src/com/alchemyapi/test/LanguageTest.java index 29f5063..95f8863 100644 --- a/src/com/alchemyapi/test/LanguageTest.java +++ b/src/com/alchemyapi/test/LanguageTest.java @@ -22,21 +22,21 @@ public static void main(String[] args) AlchemyAPI alchemyObj = AlchemyAPI.GetInstanceFromFile("api_key.txt"); // Detect the language for a web URL. - Document doc = alchemyObj.URLGetLanguage("http://news.google.fr/"); - System.out.println(getStringFromDocument(doc)); + String doc = alchemyObj.URLGetLanguage("http://news.google.fr/"); + System.out.println(doc); // Detect the language for a text string (requires at least 100 // characters). String htmlDoc = getFileContents("data/example.html"); doc = alchemyObj.TextGetLanguage("This is some english language text. What language do you speak?"); - System.out.println(getStringFromDocument(doc)); + System.out.println(doc); // Load a HTML document to analyze. htmlDoc = getFileContents("data/example.html"); // Detect the language for a HTML document. doc = alchemyObj.HTMLGetLanguage(htmlDoc, "http://www.test.com/"); - System.out.println(getStringFromDocument(doc)); + System.out.println(doc); } // utility function diff --git a/src/com/alchemyapi/test/MicroformatTest.java b/src/com/alchemyapi/test/MicroformatTest.java index 97c2c41..1daa69f 100644 --- a/src/com/alchemyapi/test/MicroformatTest.java +++ b/src/com/alchemyapi/test/MicroformatTest.java @@ -22,15 +22,15 @@ public static void main(String[] args) AlchemyAPI alchemyObj = AlchemyAPI.GetInstanceFromFile("api_key.txt"); // Extract microformats data from a web URL. - Document doc = alchemyObj.URLGetMicroformats("http://microformats.org/wiki/hcard"); - System.out.println(getStringFromDocument(doc)); + String doc = alchemyObj.URLGetMicroformats("http://microformats.org/wiki/hcard"); + System.out.println(doc); // Load a HTML document to analyze. String htmlDoc = getFileContents("data/microformats.html"); // Extract microformats data from a HTML document. doc = alchemyObj.HTMLGetMicroformats(htmlDoc, "http://www.test.com/"); - System.out.println(getStringFromDocument(doc)); + System.out.println(doc); } // utility function diff --git a/src/com/alchemyapi/test/ParameterTest.java b/src/com/alchemyapi/test/ParameterTest.java index eb81975..cd6dea2 100644 --- a/src/com/alchemyapi/test/ParameterTest.java +++ b/src/com/alchemyapi/test/ParameterTest.java @@ -25,21 +25,21 @@ public static void main(String[] args) AlchemyAPI_NamedEntityParams entityParams = new AlchemyAPI_NamedEntityParams(); entityParams.setDisambiguate(false); entityParams.setSentiment(true); - Document doc = alchemyObj.URLGetRankedNamedEntities("http://www.techcrunch.com/", entityParams); - System.out.println(getStringFromDocument(doc)); + String doc = alchemyObj.URLGetRankedNamedEntities("http://www.techcrunch.com/", entityParams); + System.out.println(doc); // Extract a ranked list of named entities from a text string. doc = alchemyObj.TextGetRankedNamedEntities( "Hello there, my name is Bob Jones. I live in the United States of America. " + "Where do you live, Fred?", entityParams); - System.out.println(getStringFromDocument(doc)); + System.out.println(doc); // Load a HTML document to analyze. String htmlDoc = getFileContents("data/example.html"); // Extract a ranked list of named entities from a HTML document. doc = alchemyObj.HTMLGetRankedNamedEntities(htmlDoc, "http://www.test.com/", entityParams); - System.out.println(getStringFromDocument(doc)); + System.out.println(doc); } diff --git a/src/com/alchemyapi/test/RelationsTest.java b/src/com/alchemyapi/test/RelationsTest.java index 311a97c..845c039 100644 --- a/src/com/alchemyapi/test/RelationsTest.java +++ b/src/com/alchemyapi/test/RelationsTest.java @@ -22,21 +22,21 @@ public static void main(String[] args) AlchemyAPI alchemyObj = AlchemyAPI.GetInstanceFromFile("api_key.txt"); // Extract a ranked list of relations for a web URL. - Document doc = alchemyObj.URLGetRelations("http://www.techcrunch.com/"); - System.out.println(getStringFromDocument(doc)); + String doc = alchemyObj.URLGetRelations("http://www.techcrunch.com/"); + System.out.println(doc); // Extract a ranked list of relations from a text string. doc = alchemyObj.TextGetRelations( "Hello there, my name is Bob Jones. I live in the United States of America. " + "Where do you live, Fred?"); - System.out.println(getStringFromDocument(doc)); + System.out.println(doc); // Load a HTML document to analyze. String htmlDoc = getFileContents("data/example.html"); // Extract a ranked list of relations from a HTML document. doc = alchemyObj.HTMLGetRelations(htmlDoc, "http://www.test.com/"); - System.out.println(getStringFromDocument(doc)); + System.out.println(doc); AlchemyAPI_RelationParams relationParams = new AlchemyAPI_RelationParams(); relationParams.setSentiment(true); @@ -44,13 +44,13 @@ public static void main(String[] args) relationParams.setDisambiguate(true); relationParams.setSentimentExcludeEntities(true); doc = alchemyObj.TextGetRelations("Madonna enjoys tasty Pepsi. I love her style.", relationParams); - System.out.println(getStringFromDocument(doc)); + System.out.println(doc); relationParams.setSentiment(true); relationParams.setRequireEntities(true); relationParams.setSentimentExcludeEntities(true); doc = alchemyObj.TextGetRelations("Madonna enjoys tasty Pepsi. I love her style.", relationParams); - System.out.println(getStringFromDocument(doc)); + System.out.println(doc); } // utility function diff --git a/src/com/alchemyapi/test/SentimentTest.java b/src/com/alchemyapi/test/SentimentTest.java index f999645..01750ae 100644 --- a/src/com/alchemyapi/test/SentimentTest.java +++ b/src/com/alchemyapi/test/SentimentTest.java @@ -21,46 +21,46 @@ public static void main(String[] args) throws IOException, SAXException, AlchemyAPI alchemyObj = AlchemyAPI.GetInstanceFromFile("api_key.txt"); // Extract sentiment for a web URL. - Document doc = alchemyObj.URLGetTextSentiment("http://www.techcrunch.com/"); - System.out.println(getStringFromDocument(doc)); + String doc = alchemyObj.URLGetTextSentiment("http://www.techcrunch.com/"); + System.out.println(doc); // Extract sentiment for a text string. doc = alchemyObj.TextGetTextSentiment( "That hat is ridiculous, Charles."); - System.out.println(getStringFromDocument(doc)); + System.out.println(doc); // Load a HTML document to analyze. String htmlDoc = getFileContents("data/example.html"); // Extract sentiment for a HTML document. doc = alchemyObj.HTMLGetTextSentiment(htmlDoc, "http://www.test.com/"); - System.out.println(getStringFromDocument(doc)); + System.out.println(doc); // Extract entity-targeted sentiment from a HTML document. AlchemyAPI_NamedEntityParams entityParams = new AlchemyAPI_NamedEntityParams(); entityParams.setSentiment(true); doc = alchemyObj.TextGetRankedNamedEntities("That Mike Tyson is such a sweetheart.", entityParams); - System.out.println(getStringFromDocument(doc)); + System.out.println(doc); // Extract keyword-targeted sentiment from a HTML document. AlchemyAPI_KeywordParams keywordParams = new AlchemyAPI_KeywordParams(); keywordParams.setSentiment(true); doc = alchemyObj.TextGetRankedKeywords("That Mike Tyson is such a sweetheart.", keywordParams); - System.out.println(getStringFromDocument(doc)); + System.out.println(doc); //Extract Targeted Sentiment from text AlchemyAPI_TargetedSentimentParams sentimentParams = new AlchemyAPI_TargetedSentimentParams(); sentimentParams.setShowSourceText(true); doc = alchemyObj.TextGetTargetedSentiment("This car is terrible.", "car", sentimentParams); - System.out.print(getStringFromDocument(doc)); + System.out.print(doc); //Extract Targeted Sentiment from url doc = alchemyObj.URLGetTargetedSentiment("http://techcrunch.com/2012/03/01/keen-on-anand-rajaraman-how-walmart-wants-to-leapfrog-over-amazon-tctv/", "Walmart",sentimentParams); - System.out.print(getStringFromDocument(doc)); + System.out.print(doc); //Extract Targeted Sentiment from html doc = alchemyObj.HTMLGetTargetedSentiment(htmlDoc, "http://www.test.com/", "WujWuj", sentimentParams); - System.out.print(getStringFromDocument(doc)); + System.out.print(doc); } // utility function diff --git a/src/com/alchemyapi/test/TaxonomyTest.java b/src/com/alchemyapi/test/TaxonomyTest.java index d3ba089..679e330 100644 --- a/src/com/alchemyapi/test/TaxonomyTest.java +++ b/src/com/alchemyapi/test/TaxonomyTest.java @@ -22,21 +22,21 @@ public static void main(String[] args) AlchemyAPI alchemyObj = AlchemyAPI.GetInstanceFromFile("api_key.txt"); // Extract a ranked list of relations for a web URL. - Document doc = alchemyObj.URLGetTaxonomy("http://www.techcrunch.com/"); - System.out.println(getStringFromDocument(doc)); + String doc = alchemyObj.URLGetTaxonomy("http://www.techcrunch.com/"); + System.out.println(doc); // Extract a ranked taxonomy from a text string. doc = alchemyObj.TextGetTaxonomy( "Hello there, my name is Bob Jones. I live in the United States of America. " + "Where do you live, Fred?"); - System.out.println(getStringFromDocument(doc)); + System.out.println(doc); // Load a HTML document to analyze. String htmlDoc = getFileContents("data/example.html"); // Extract a ranked taxonomy from a HTML document. doc = alchemyObj.HTMLGetTaxonomy(htmlDoc, "http://www.test.com/"); - System.out.println(getStringFromDocument(doc)); + System.out.println(doc); } // utility function diff --git a/src/com/alchemyapi/test/TextExtractTest.java b/src/com/alchemyapi/test/TextExtractTest.java index d5e71e5..504193a 100644 --- a/src/com/alchemyapi/test/TextExtractTest.java +++ b/src/com/alchemyapi/test/TextExtractTest.java @@ -23,17 +23,17 @@ public static void main(String[] args) // Extract page text from a web URL. (ignoring ads, navigation links, // and other content). - Document doc = alchemyObj.URLGetText("http://www.techcrunch.com/"); - System.out.println(getStringFromDocument(doc)); + String doc = alchemyObj.URLGetText("http://www.techcrunch.com/"); + System.out.println(doc); // Extract raw page text from a web URL. (including ads, navigation // links, and other content). doc = alchemyObj.URLGetRawText("http://www.techcrunch.com/"); - System.out.println(getStringFromDocument(doc)); + System.out.println(doc); // Extract a title from a web URL. doc = alchemyObj.URLGetTitle("http://www.techcrunch.com/"); - System.out.println(getStringFromDocument(doc)); + System.out.println(doc); // Load a HTML document to analyze. String htmlDoc = getFileContents("data/example.html"); @@ -41,16 +41,16 @@ public static void main(String[] args) // Extract page text from a HTML document. (ignoring ads, navigation // links, and other content). doc = alchemyObj.HTMLGetText(htmlDoc, "http://www.test.com/"); - System.out.println(getStringFromDocument(doc)); + System.out.println(doc); // Extract raw page text from a HTML document. (including ads, // navigation links, and other content). doc = alchemyObj.HTMLGetRawText(htmlDoc, "http://www.test.com/"); - System.out.println(getStringFromDocument(doc)); + System.out.println(doc); // Extract a title from a HTML document. doc = alchemyObj.HTMLGetTitle(htmlDoc, "http://www.test.com/"); - System.out.println(getStringFromDocument(doc)); + System.out.println(doc); } // utility function From 9d49bc249db99be270ea9bddd00021601c7b2bb7 Mon Sep 17 00:00:00 2001 From: Joshua Holmgren Date: Thu, 17 Jul 2014 10:42:00 -0600 Subject: [PATCH 2/3] deleted commented code in AlchemyAPI.java --- src/com/alchemyapi/api/AlchemyAPI.java | 104 ------------------------- 1 file changed, 104 deletions(-) diff --git a/src/com/alchemyapi/api/AlchemyAPI.java b/src/com/alchemyapi/api/AlchemyAPI.java index cdc9dc1..90a3dc5 100644 --- a/src/com/alchemyapi/api/AlchemyAPI.java +++ b/src/com/alchemyapi/api/AlchemyAPI.java @@ -16,8 +16,6 @@ import javax.xml.parsers.ParserConfigurationException; import javax.xml.xpath.*; -//import com.google.gson.*; - import org.w3c.dom.Document; import org.w3c.dom.NodeList; @@ -978,21 +976,6 @@ private void CheckURL(String url) { throw new IllegalArgumentException("Enter an URL to analyze."); } - /*private Document GET(String callName, String callPrefix, AlchemyAPI_Params params) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - StringBuilder uri = new StringBuilder(); - uri.append(_requestUri).append(callPrefix).append('/').append(callName) - .append('?').append("apikey=").append(this._apiKey); - uri.append(params.getParameterString()); - - URL url = new URL(uri.toString()); - HttpURLConnection handle = (HttpURLConnection) url.openConnection(); - handle.setDoOutput(true); - - return doRequest(handle, params.getOutputMode()); - }*/ private String GET(String callName, String callPrefix, AlchemyAPI_Params params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException @@ -1031,29 +1014,6 @@ private String POST(String callName, String callPrefix, AlchemyAPI_Params params return doRequest(handle, params.getOutputMode()); } - /*private Document POST(String callName, String callPrefix, AlchemyAPI_Params params) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - URL url = new URL(_requestUri + callPrefix + "/" + callName); - - HttpURLConnection handle = (HttpURLConnection) url.openConnection(); - handle.setDoOutput(true); - - StringBuilder data = new StringBuilder(); - - data.append("apikey=").append(this._apiKey); - data.append(params.getParameterString()); - - handle.addRequestProperty("Content-Length", Integer.toString(data.length())); - - DataOutputStream ostream = new DataOutputStream(handle.getOutputStream()); - ostream.write(data.toString().getBytes()); - ostream.close(); - - return doRequest(handle, params.getOutputMode()); - }*/ - private String doRequest(HttpURLConnection handle, String outputMode) throws IOException, SAXException, @@ -1071,68 +1031,4 @@ private String doRequest(HttpURLConnection handle, String outputMode) return doc; } - /* - - private Document doRequest(HttpURLConnection handle, String outputMode) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - DataInputStream istream = new DataInputStream(handle.getInputStream()); - - - - Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(istream); - - istream.close(); - handle.disconnect(); - - XPathFactory factory = XPathFactory.newInstance(); - - if(AlchemyAPI_Params.OUTPUT_XML.equals(outputMode)) { - String statusStr = getNodeValue(factory, doc, "/results/status/text()"); - if (null == statusStr || !statusStr.equals("OK")) { - String statusInfoStr = getNodeValue(factory, doc, "/results/statusInfo/text()"); - if (null != statusInfoStr && statusInfoStr.length() > 0) - throw new IOException("Error making API call: " + statusInfoStr + '.'); - - throw new IOException("Error making API call: " + statusStr + '.'); - } - } - else if(AlchemyAPI_Params.OUTPUT_RDF.equals(outputMode)) { - String statusStr = getNodeValue(factory, doc, "//RDF/Description/ResultStatus/text()"); - if (null == statusStr || !statusStr.equals("OK")) { - String statusInfoStr = getNodeValue(factory, doc, "//RDF/Description/ResultStatus/text()"); - if (null != statusInfoStr && statusInfoStr.length() > 0) - throw new IOException("Error making API call: " + statusInfoStr + '.'); - - throw new IOException("Error making API call: " + statusStr + '.'); - } - } - else if(AlchemyAPI_Params.OUTPUT_JSON.equals(outputMode)) { - String statusStr = getNodeValue(factory, doc, "//JSON/Description/ResultStatus/text()"); - if (null == statusStr || !statusStr.equals("OK")) { - String statusInfoStr = getNodeValue(factory, doc, "//JSON/Description/ResultStatus/text()"); - if (null != statusInfoStr && statusInfoStr.length() > 0) - throw new IOException("Error making API call: " + statusInfoStr + '.'); - - throw new IOException("Error making API call: " + statusStr + '.'); - } - } - - return doc; - } - - private String getNodeValue(XPathFactory factory, Document doc, String xpathStr) - throws XPathExpressionException - { - XPath xpath = factory.newXPath(); - XPathExpression expr = xpath.compile(xpathStr); - Object result = expr.evaluate(doc, XPathConstants.NODESET); - NodeList results = (NodeList) result; - - if (results.getLength() > 0 && null != results.item(0)) - return results.item(0).getNodeValue(); - - return null; - }*/ } From e0f73d4fbf64c2222b68f77172f889794ad7d7df Mon Sep 17 00:00:00 2001 From: Joshua Holmgren Date: Wed, 24 Sep 2014 09:35:13 -0600 Subject: [PATCH 3/3] got rid of 5 character limit in CheckText function --- src/com/alchemyapi/api/AlchemyAPI.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/com/alchemyapi/api/AlchemyAPI.java b/src/com/alchemyapi/api/AlchemyAPI.java index 90a3dc5..e5069e6 100644 --- a/src/com/alchemyapi/api/AlchemyAPI.java +++ b/src/com/alchemyapi/api/AlchemyAPI.java @@ -967,7 +967,7 @@ private void CheckHTML(String html, String url) { } private void CheckText(String text) { - if (null == text || text.length() < 5) + if (null == text) throw new IllegalArgumentException("Enter some text to analyze."); }