From 08f4a423f07fd621bc6de649dc25fd78c7d1bb34 Mon Sep 17 00:00:00 2001 From: Kenny Cason Date: Sun, 2 Aug 2015 02:13:53 -0500 Subject: [PATCH 01/11] #8 begin mavenization --- .gitignore | 18 +- ChangeLog | 14 -- alchemyapi_java.iml | 19 ++ build.xml | 1 - build_no_tests.xml | 44 ---- build_with_tests.xml | 196 ------------------ pom.xml | 57 +++++ src/com/alchemyapi/test/AuthorTest.java | 81 -------- .../java}/com/alchemyapi/api/AlchemyAPI.java | 0 .../api/AlchemyAPI_CategoryParams.java | 0 .../api/AlchemyAPI_CombinedParams.java | 0 .../api/AlchemyAPI_ConceptParams.java | 0 .../api/AlchemyAPI_ConstraintQueryParams.java | 0 .../api/AlchemyAPI_ImageParams.java | 0 .../api/AlchemyAPI_KeywordParams.java | 0 .../api/AlchemyAPI_LanguageParams.java | 0 .../api/AlchemyAPI_NamedEntityParams.java | 0 .../com/alchemyapi/api/AlchemyAPI_Params.java | 0 .../api/AlchemyAPI_RelationParams.java | 0 .../AlchemyAPI_TargetedSentimentParams.java | 0 .../api/AlchemyAPI_TaxonomyParams.java | 0 .../alchemyapi/api/AlchemyAPI_TextParams.java | 0 .../java/com/alchemyapi}/CategoryTest.java | 19 +- .../java/com/alchemyapi}/CombinedTest.java | 2 +- .../java/com/alchemyapi}/ConceptTest.java | 2 +- .../com/alchemyapi}/ConstraintQueryTest.java | 2 +- .../java/com/alchemyapi}/EntityTest.java | 2 +- .../java/com/alchemyapi}/FeedLinksTest.java | 2 +- .../java/com/alchemyapi}/ImageTest.java | 2 +- .../java/com/alchemyapi}/KeywordTest.java | 2 +- .../java/com/alchemyapi}/LanguageTest.java | 2 +- .../com/alchemyapi/MicroformatsTest.java} | 17 +- .../java/com/alchemyapi}/ParameterTest.java | 2 +- .../java/com/alchemyapi}/RelationsTest.java | 0 src/test/java/com/alchemyapi/STestAuthor.java | 30 +++ .../java/com/alchemyapi}/SentimentTest.java | 2 +- .../java/com/alchemyapi}/TaxonomyTest.java | 2 +- .../java/com/alchemyapi}/TextExtractTest.java | 2 +- .../com/alchemyapi/util/DocumentUtils.java | 35 ++++ .../com/alchemyapi/util/ResourceUtils.java | 21 ++ src/test/resources/api_key.txt | 1 + {testdir => src/test/resources}/data/cat.jpg | Bin .../test/resources}/data/example.html | 0 .../test/resources}/data/example2.html | 0 .../resources}/data/example2_nolinks.html | 0 .../test/resources}/data/example3.html | 0 .../test/resources}/data/microformats.html | 0 testdir/api_key.txt | 0 48 files changed, 215 insertions(+), 362 deletions(-) delete mode 100644 ChangeLog create mode 100644 alchemyapi_java.iml delete mode 120000 build.xml delete mode 100644 build_no_tests.xml delete mode 100644 build_with_tests.xml create mode 100644 pom.xml delete mode 100644 src/com/alchemyapi/test/AuthorTest.java rename src/{ => main/java}/com/alchemyapi/api/AlchemyAPI.java (100%) rename src/{ => main/java}/com/alchemyapi/api/AlchemyAPI_CategoryParams.java (100%) rename src/{ => main/java}/com/alchemyapi/api/AlchemyAPI_CombinedParams.java (100%) rename src/{ => main/java}/com/alchemyapi/api/AlchemyAPI_ConceptParams.java (100%) rename src/{ => main/java}/com/alchemyapi/api/AlchemyAPI_ConstraintQueryParams.java (100%) rename src/{ => main/java}/com/alchemyapi/api/AlchemyAPI_ImageParams.java (100%) rename src/{ => main/java}/com/alchemyapi/api/AlchemyAPI_KeywordParams.java (100%) rename src/{ => main/java}/com/alchemyapi/api/AlchemyAPI_LanguageParams.java (100%) rename src/{ => main/java}/com/alchemyapi/api/AlchemyAPI_NamedEntityParams.java (100%) rename src/{ => main/java}/com/alchemyapi/api/AlchemyAPI_Params.java (100%) rename src/{ => main/java}/com/alchemyapi/api/AlchemyAPI_RelationParams.java (100%) rename src/{ => main/java}/com/alchemyapi/api/AlchemyAPI_TargetedSentimentParams.java (100%) rename src/{ => main/java}/com/alchemyapi/api/AlchemyAPI_TaxonomyParams.java (100%) rename src/{ => main/java}/com/alchemyapi/api/AlchemyAPI_TextParams.java (100%) rename src/{com/alchemyapi/test => test/java/com/alchemyapi}/CategoryTest.java (89%) rename src/{com/alchemyapi/test => test/java/com/alchemyapi}/CombinedTest.java (98%) rename src/{com/alchemyapi/test => test/java/com/alchemyapi}/ConceptTest.java (98%) rename src/{com/alchemyapi/test => test/java/com/alchemyapi}/ConstraintQueryTest.java (98%) rename src/{com/alchemyapi/test => test/java/com/alchemyapi}/EntityTest.java (98%) rename src/{com/alchemyapi/test => test/java/com/alchemyapi}/FeedLinksTest.java (98%) rename src/{com/alchemyapi/test => test/java/com/alchemyapi}/ImageTest.java (98%) rename src/{com/alchemyapi/test => test/java/com/alchemyapi}/KeywordTest.java (98%) rename src/{com/alchemyapi/test => test/java/com/alchemyapi}/LanguageTest.java (98%) rename src/{com/alchemyapi/test/MicroformatTest.java => test/java/com/alchemyapi/MicroformatsTest.java} (91%) rename src/{com/alchemyapi/test => test/java/com/alchemyapi}/ParameterTest.java (96%) rename src/{com/alchemyapi/test => test/java/com/alchemyapi}/RelationsTest.java (100%) create mode 100644 src/test/java/com/alchemyapi/STestAuthor.java rename src/{com/alchemyapi/test => test/java/com/alchemyapi}/SentimentTest.java (99%) rename src/{com/alchemyapi/test => test/java/com/alchemyapi}/TaxonomyTest.java (98%) rename src/{com/alchemyapi/test => test/java/com/alchemyapi}/TextExtractTest.java (99%) create mode 100644 src/test/java/com/alchemyapi/util/DocumentUtils.java create mode 100644 src/test/java/com/alchemyapi/util/ResourceUtils.java create mode 100644 src/test/resources/api_key.txt rename {testdir => src/test/resources}/data/cat.jpg (100%) rename {testdir => src/test/resources}/data/example.html (100%) rename {testdir => src/test/resources}/data/example2.html (100%) rename {testdir => src/test/resources}/data/example2_nolinks.html (100%) rename {testdir => src/test/resources}/data/example3.html (100%) rename {testdir => src/test/resources}/data/microformats.html (100%) delete mode 100644 testdir/api_key.txt diff --git a/.gitignore b/.gitignore index 3497551..98bd503 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,16 @@ -bin -dist \ No newline at end of file +*.class +*.DS_store +.classpath +.project +.metadata +.settings +.settings/* +.idea +/*/target/* +/target/ +/target +*.log +*.log.gz +.idea/* +*.iml +*.patch diff --git a/ChangeLog b/ChangeLog deleted file mode 100644 index 0ca12f2..0000000 --- a/ChangeLog +++ /dev/null @@ -1,14 +0,0 @@ -CHANGELOG - -0.9 - Added support for combined call, ranked taxonomy & image extraction. -0.8 - Added support for author extraction & targeted sentiment. -0.7 - Added support for relations extraction. -0.6 - Added support for sentiment analysis. -0.5 - Added GetRankedConcepts calls and parameter object. -0.4.2 - Added Parameters, added RDF support -0.4.1 - Fixed compilation error in Entities example code. -0.4 - Added GetRankedKeywords calls, added TextGetCategory call. -0.3 - Added ConstraintQuery call, imports changes, exception reporting - changes, general improvements. -0.2 - Added setApiKey() call, setApiHost() call, updated API endpoint. -0.1 - Initial Release diff --git a/alchemyapi_java.iml b/alchemyapi_java.iml new file mode 100644 index 0000000..f78a651 --- /dev/null +++ b/alchemyapi_java.iml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/build.xml b/build.xml deleted file mode 120000 index b010dab..0000000 --- a/build.xml +++ /dev/null @@ -1 +0,0 @@ -build_with_tests.xml \ No newline at end of file diff --git a/build_no_tests.xml b/build_no_tests.xml deleted file mode 100644 index 3d4402a..0000000 --- a/build_no_tests.xml +++ /dev/null @@ -1,44 +0,0 @@ - - - Build file for AlchemyAPI code / examples. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/build_with_tests.xml b/build_with_tests.xml deleted file mode 100644 index 45ebf3f..0000000 --- a/build_with_tests.xml +++ /dev/null @@ -1,196 +0,0 @@ - - - Build file for AlchemyAPI code / examples. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..73d9860 --- /dev/null +++ b/pom.xml @@ -0,0 +1,57 @@ + + + 4.0.0 + + alchemyapi + sdk + 1.0-SNAPSHOT + + + UTF-8 + target + + + + + commons-io + commons-io + 2.4 + + + + junit + junit + 4.10 + test + + + + + ${my.build.directory} + + + org.apache.maven.plugins + maven-compiler-plugin + 3.0 + + 1.7 + 1.7 + + + + org.apache.maven.plugins + maven-release-plugin + 2.5 + + deploy + + pom.xml + + + + + + + \ No newline at end of file diff --git a/src/com/alchemyapi/test/AuthorTest.java b/src/com/alchemyapi/test/AuthorTest.java deleted file mode 100644 index 832535a..0000000 --- a/src/com/alchemyapi/test/AuthorTest.java +++ /dev/null @@ -1,81 +0,0 @@ -package com.alchemyapi.test; - -import com.alchemyapi.api.*; - -import org.xml.sax.SAXException; -import org.w3c.dom.Document; -import java.io.*; - -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.xpath.XPathExpressionException; -import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerException; -import javax.xml.transform.TransformerFactory; -import javax.xml.transform.dom.DOMSource; -import javax.xml.transform.stream.StreamResult; - -public class AuthorTest { - - - public static void main(String[] args) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - // Create an AlchemyAPI object. - AlchemyAPI alchemyObj = AlchemyAPI.GetInstanceFromFile("api_key.txt"); - - // 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)); - - doc = alchemyObj.HTMLGetAuthor(htmlDoc, "http://www.test.com/"); - System.out.println(getStringFromDocument(doc)); - } - - // utility function - private static String getFileContents(String filename) - throws IOException, FileNotFoundException - { - File file = new File(filename); - StringBuilder contents = new StringBuilder(); - - BufferedReader input = new BufferedReader(new FileReader(file)); - - try { - String line = null; - - while ((line = input.readLine()) != null) { - contents.append(line); - contents.append(System.getProperty("line.separator")); - } - } finally { - input.close(); - } - - return contents.toString(); - } - - // utility method - private static String getStringFromDocument(Document doc) { - try { - DOMSource domSource = new DOMSource(doc); - StringWriter writer = new StringWriter(); - StreamResult result = new StreamResult(writer); - - TransformerFactory tf = TransformerFactory.newInstance(); - Transformer transformer = tf.newTransformer(); - transformer.transform(domSource, result); - - return writer.toString(); - } catch (TransformerException ex) { - ex.printStackTrace(); - return null; - } - } - - - - -} diff --git a/src/com/alchemyapi/api/AlchemyAPI.java b/src/main/java/com/alchemyapi/api/AlchemyAPI.java similarity index 100% rename from src/com/alchemyapi/api/AlchemyAPI.java rename to src/main/java/com/alchemyapi/api/AlchemyAPI.java diff --git a/src/com/alchemyapi/api/AlchemyAPI_CategoryParams.java b/src/main/java/com/alchemyapi/api/AlchemyAPI_CategoryParams.java similarity index 100% rename from src/com/alchemyapi/api/AlchemyAPI_CategoryParams.java rename to src/main/java/com/alchemyapi/api/AlchemyAPI_CategoryParams.java diff --git a/src/com/alchemyapi/api/AlchemyAPI_CombinedParams.java b/src/main/java/com/alchemyapi/api/AlchemyAPI_CombinedParams.java similarity index 100% rename from src/com/alchemyapi/api/AlchemyAPI_CombinedParams.java rename to src/main/java/com/alchemyapi/api/AlchemyAPI_CombinedParams.java diff --git a/src/com/alchemyapi/api/AlchemyAPI_ConceptParams.java b/src/main/java/com/alchemyapi/api/AlchemyAPI_ConceptParams.java similarity index 100% rename from src/com/alchemyapi/api/AlchemyAPI_ConceptParams.java rename to src/main/java/com/alchemyapi/api/AlchemyAPI_ConceptParams.java diff --git a/src/com/alchemyapi/api/AlchemyAPI_ConstraintQueryParams.java b/src/main/java/com/alchemyapi/api/AlchemyAPI_ConstraintQueryParams.java similarity index 100% rename from src/com/alchemyapi/api/AlchemyAPI_ConstraintQueryParams.java rename to src/main/java/com/alchemyapi/api/AlchemyAPI_ConstraintQueryParams.java diff --git a/src/com/alchemyapi/api/AlchemyAPI_ImageParams.java b/src/main/java/com/alchemyapi/api/AlchemyAPI_ImageParams.java similarity index 100% rename from src/com/alchemyapi/api/AlchemyAPI_ImageParams.java rename to src/main/java/com/alchemyapi/api/AlchemyAPI_ImageParams.java diff --git a/src/com/alchemyapi/api/AlchemyAPI_KeywordParams.java b/src/main/java/com/alchemyapi/api/AlchemyAPI_KeywordParams.java similarity index 100% rename from src/com/alchemyapi/api/AlchemyAPI_KeywordParams.java rename to src/main/java/com/alchemyapi/api/AlchemyAPI_KeywordParams.java diff --git a/src/com/alchemyapi/api/AlchemyAPI_LanguageParams.java b/src/main/java/com/alchemyapi/api/AlchemyAPI_LanguageParams.java similarity index 100% rename from src/com/alchemyapi/api/AlchemyAPI_LanguageParams.java rename to src/main/java/com/alchemyapi/api/AlchemyAPI_LanguageParams.java diff --git a/src/com/alchemyapi/api/AlchemyAPI_NamedEntityParams.java b/src/main/java/com/alchemyapi/api/AlchemyAPI_NamedEntityParams.java similarity index 100% rename from src/com/alchemyapi/api/AlchemyAPI_NamedEntityParams.java rename to src/main/java/com/alchemyapi/api/AlchemyAPI_NamedEntityParams.java diff --git a/src/com/alchemyapi/api/AlchemyAPI_Params.java b/src/main/java/com/alchemyapi/api/AlchemyAPI_Params.java similarity index 100% rename from src/com/alchemyapi/api/AlchemyAPI_Params.java rename to src/main/java/com/alchemyapi/api/AlchemyAPI_Params.java diff --git a/src/com/alchemyapi/api/AlchemyAPI_RelationParams.java b/src/main/java/com/alchemyapi/api/AlchemyAPI_RelationParams.java similarity index 100% rename from src/com/alchemyapi/api/AlchemyAPI_RelationParams.java rename to src/main/java/com/alchemyapi/api/AlchemyAPI_RelationParams.java diff --git a/src/com/alchemyapi/api/AlchemyAPI_TargetedSentimentParams.java b/src/main/java/com/alchemyapi/api/AlchemyAPI_TargetedSentimentParams.java similarity index 100% rename from src/com/alchemyapi/api/AlchemyAPI_TargetedSentimentParams.java rename to src/main/java/com/alchemyapi/api/AlchemyAPI_TargetedSentimentParams.java diff --git a/src/com/alchemyapi/api/AlchemyAPI_TaxonomyParams.java b/src/main/java/com/alchemyapi/api/AlchemyAPI_TaxonomyParams.java similarity index 100% rename from src/com/alchemyapi/api/AlchemyAPI_TaxonomyParams.java rename to src/main/java/com/alchemyapi/api/AlchemyAPI_TaxonomyParams.java diff --git a/src/com/alchemyapi/api/AlchemyAPI_TextParams.java b/src/main/java/com/alchemyapi/api/AlchemyAPI_TextParams.java similarity index 100% rename from src/com/alchemyapi/api/AlchemyAPI_TextParams.java rename to src/main/java/com/alchemyapi/api/AlchemyAPI_TextParams.java diff --git a/src/com/alchemyapi/test/CategoryTest.java b/src/test/java/com/alchemyapi/CategoryTest.java similarity index 89% rename from src/com/alchemyapi/test/CategoryTest.java rename to src/test/java/com/alchemyapi/CategoryTest.java index 9be3605..317ef7d 100644 --- a/src/com/alchemyapi/test/CategoryTest.java +++ b/src/test/java/com/alchemyapi/CategoryTest.java @@ -1,17 +1,24 @@ -package com.alchemyapi.test; +package com.alchemyapi; -import com.alchemyapi.api.*; - -import org.xml.sax.SAXException; +import com.alchemyapi.api.AlchemyAPI; +import com.alchemyapi.api.AlchemyAPI_CategoryParams; +import com.alchemyapi.api.AlchemyAPI_Params; import org.w3c.dom.Document; -import java.io.*; +import org.xml.sax.SAXException; + import javax.xml.parsers.ParserConfigurationException; -import javax.xml.xpath.XPathExpressionException; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; +import javax.xml.xpath.XPathExpressionException; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.io.StringWriter; class CategoryTest { public static void main(String[] args) diff --git a/src/com/alchemyapi/test/CombinedTest.java b/src/test/java/com/alchemyapi/CombinedTest.java similarity index 98% rename from src/com/alchemyapi/test/CombinedTest.java rename to src/test/java/com/alchemyapi/CombinedTest.java index 7b68e5f..7d05302 100644 --- a/src/com/alchemyapi/test/CombinedTest.java +++ b/src/test/java/com/alchemyapi/CombinedTest.java @@ -1,4 +1,4 @@ -package com.alchemyapi.test; +package com.alchemyapi; import com.alchemyapi.api.*; diff --git a/src/com/alchemyapi/test/ConceptTest.java b/src/test/java/com/alchemyapi/ConceptTest.java similarity index 98% rename from src/com/alchemyapi/test/ConceptTest.java rename to src/test/java/com/alchemyapi/ConceptTest.java index 69f71b8..07800f5 100644 --- a/src/com/alchemyapi/test/ConceptTest.java +++ b/src/test/java/com/alchemyapi/ConceptTest.java @@ -1,4 +1,4 @@ -package com.alchemyapi.test; +package com.alchemyapi; import com.alchemyapi.api.AlchemyAPI; diff --git a/src/com/alchemyapi/test/ConstraintQueryTest.java b/src/test/java/com/alchemyapi/ConstraintQueryTest.java similarity index 98% rename from src/com/alchemyapi/test/ConstraintQueryTest.java rename to src/test/java/com/alchemyapi/ConstraintQueryTest.java index fd6f4f3..fe41f06 100644 --- a/src/com/alchemyapi/test/ConstraintQueryTest.java +++ b/src/test/java/com/alchemyapi/ConstraintQueryTest.java @@ -1,4 +1,4 @@ -package com.alchemyapi.test; +package com.alchemyapi; import com.alchemyapi.api.AlchemyAPI; diff --git a/src/com/alchemyapi/test/EntityTest.java b/src/test/java/com/alchemyapi/EntityTest.java similarity index 98% rename from src/com/alchemyapi/test/EntityTest.java rename to src/test/java/com/alchemyapi/EntityTest.java index c9d5a41..e53265f 100644 --- a/src/com/alchemyapi/test/EntityTest.java +++ b/src/test/java/com/alchemyapi/EntityTest.java @@ -1,4 +1,4 @@ -package com.alchemyapi.test; +package com.alchemyapi; import com.alchemyapi.api.AlchemyAPI; diff --git a/src/com/alchemyapi/test/FeedLinksTest.java b/src/test/java/com/alchemyapi/FeedLinksTest.java similarity index 98% rename from src/com/alchemyapi/test/FeedLinksTest.java rename to src/test/java/com/alchemyapi/FeedLinksTest.java index 1d0563f..bf549cd 100644 --- a/src/com/alchemyapi/test/FeedLinksTest.java +++ b/src/test/java/com/alchemyapi/FeedLinksTest.java @@ -1,4 +1,4 @@ -package com.alchemyapi.test; +package com.alchemyapi; import com.alchemyapi.api.AlchemyAPI; diff --git a/src/com/alchemyapi/test/ImageTest.java b/src/test/java/com/alchemyapi/ImageTest.java similarity index 98% rename from src/com/alchemyapi/test/ImageTest.java rename to src/test/java/com/alchemyapi/ImageTest.java index af474b4..c5f64ca 100644 --- a/src/com/alchemyapi/test/ImageTest.java +++ b/src/test/java/com/alchemyapi/ImageTest.java @@ -1,4 +1,4 @@ -package com.alchemyapi.test; +package com.alchemyapi; import com.alchemyapi.api.*; diff --git a/src/com/alchemyapi/test/KeywordTest.java b/src/test/java/com/alchemyapi/KeywordTest.java similarity index 98% rename from src/com/alchemyapi/test/KeywordTest.java rename to src/test/java/com/alchemyapi/KeywordTest.java index 6338548..1345528 100644 --- a/src/com/alchemyapi/test/KeywordTest.java +++ b/src/test/java/com/alchemyapi/KeywordTest.java @@ -1,4 +1,4 @@ -package com.alchemyapi.test; +package com.alchemyapi; import com.alchemyapi.api.AlchemyAPI; diff --git a/src/com/alchemyapi/test/LanguageTest.java b/src/test/java/com/alchemyapi/LanguageTest.java similarity index 98% rename from src/com/alchemyapi/test/LanguageTest.java rename to src/test/java/com/alchemyapi/LanguageTest.java index 29f5063..6cc8df3 100644 --- a/src/com/alchemyapi/test/LanguageTest.java +++ b/src/test/java/com/alchemyapi/LanguageTest.java @@ -1,4 +1,4 @@ -package com.alchemyapi.test; +package com.alchemyapi; import com.alchemyapi.api.AlchemyAPI; diff --git a/src/com/alchemyapi/test/MicroformatTest.java b/src/test/java/com/alchemyapi/MicroformatsTest.java similarity index 91% rename from src/com/alchemyapi/test/MicroformatTest.java rename to src/test/java/com/alchemyapi/MicroformatsTest.java index 97c2c41..f6eebe0 100644 --- a/src/com/alchemyapi/test/MicroformatTest.java +++ b/src/test/java/com/alchemyapi/MicroformatsTest.java @@ -1,19 +1,24 @@ -package com.alchemyapi.test; +package com.alchemyapi; import com.alchemyapi.api.AlchemyAPI; - -import org.xml.sax.SAXException; import org.w3c.dom.Document; -import java.io.*; +import org.xml.sax.SAXException; + import javax.xml.parsers.ParserConfigurationException; -import javax.xml.xpath.XPathExpressionException; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; +import javax.xml.xpath.XPathExpressionException; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.io.StringWriter; -class MicroformatsTest { +public class MicroformatsTest { public static void main(String[] args) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException diff --git a/src/com/alchemyapi/test/ParameterTest.java b/src/test/java/com/alchemyapi/ParameterTest.java similarity index 96% rename from src/com/alchemyapi/test/ParameterTest.java rename to src/test/java/com/alchemyapi/ParameterTest.java index eb81975..4a60f01 100644 --- a/src/com/alchemyapi/test/ParameterTest.java +++ b/src/test/java/com/alchemyapi/ParameterTest.java @@ -1,4 +1,4 @@ -package com.alchemyapi.test; +package com.alchemyapi; import com.alchemyapi.api.*; diff --git a/src/com/alchemyapi/test/RelationsTest.java b/src/test/java/com/alchemyapi/RelationsTest.java similarity index 100% rename from src/com/alchemyapi/test/RelationsTest.java rename to src/test/java/com/alchemyapi/RelationsTest.java diff --git a/src/test/java/com/alchemyapi/STestAuthor.java b/src/test/java/com/alchemyapi/STestAuthor.java new file mode 100644 index 0000000..97c426f --- /dev/null +++ b/src/test/java/com/alchemyapi/STestAuthor.java @@ -0,0 +1,30 @@ +package com.alchemyapi; + +import com.alchemyapi.api.AlchemyAPI; +import com.alchemyapi.util.DocumentUtils; +import com.alchemyapi.util.ResourceUtils; +import org.junit.Test; +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.xpath.XPathExpressionException; +import java.io.IOException; + +public class STestAuthor { + + @Test + public void demo() throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { + // Create an AlchemyAPI object. + final AlchemyAPI alchemyAPI = AlchemyAPI.GetInstanceFromString(ResourceUtils.toString("api_key.txt")); + + // Load a Html document to analyze. + final String htmlDoc = ResourceUtils.toString("data/example.html"); + final Document doc = alchemyAPI.URLGetAuthor("http://www.politico.com/blogs/media/2012/02/detroit-news-ed-upset-over-romney-edit-115247.html"); + System.out.println(DocumentUtils.toString(doc)); + + final Document doc2 = alchemyAPI.HTMLGetAuthor(htmlDoc, "http://www.test.com/"); + System.out.println(DocumentUtils.toString(doc2)); + } + +} diff --git a/src/com/alchemyapi/test/SentimentTest.java b/src/test/java/com/alchemyapi/SentimentTest.java similarity index 99% rename from src/com/alchemyapi/test/SentimentTest.java rename to src/test/java/com/alchemyapi/SentimentTest.java index f999645..a1731aa 100644 --- a/src/com/alchemyapi/test/SentimentTest.java +++ b/src/test/java/com/alchemyapi/SentimentTest.java @@ -1,4 +1,4 @@ -package com.alchemyapi.test; +package com.alchemyapi; import com.alchemyapi.api.AlchemyAPI; import com.alchemyapi.api.*; diff --git a/src/com/alchemyapi/test/TaxonomyTest.java b/src/test/java/com/alchemyapi/TaxonomyTest.java similarity index 98% rename from src/com/alchemyapi/test/TaxonomyTest.java rename to src/test/java/com/alchemyapi/TaxonomyTest.java index d3ba089..730148f 100644 --- a/src/com/alchemyapi/test/TaxonomyTest.java +++ b/src/test/java/com/alchemyapi/TaxonomyTest.java @@ -1,4 +1,4 @@ -package com.alchemyapi.test; +package com.alchemyapi; import com.alchemyapi.api.*; diff --git a/src/com/alchemyapi/test/TextExtractTest.java b/src/test/java/com/alchemyapi/TextExtractTest.java similarity index 99% rename from src/com/alchemyapi/test/TextExtractTest.java rename to src/test/java/com/alchemyapi/TextExtractTest.java index d5e71e5..9f0b7d6 100644 --- a/src/com/alchemyapi/test/TextExtractTest.java +++ b/src/test/java/com/alchemyapi/TextExtractTest.java @@ -1,4 +1,4 @@ -package com.alchemyapi.test; +package com.alchemyapi; import com.alchemyapi.api.AlchemyAPI; diff --git a/src/test/java/com/alchemyapi/util/DocumentUtils.java b/src/test/java/com/alchemyapi/util/DocumentUtils.java new file mode 100644 index 0000000..77234d4 --- /dev/null +++ b/src/test/java/com/alchemyapi/util/DocumentUtils.java @@ -0,0 +1,35 @@ +package com.alchemyapi.util; + +import org.w3c.dom.Document; + +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; +import java.io.StringWriter; + +/** + * Created by kenny + */ +// replace with HtmlSoup +public class DocumentUtils { + + private static final TransformerFactory TRANSFORMER_FACTORY = TransformerFactory.newInstance(); + + public static String toString(final Document document) { + try { + final DOMSource domSource = new DOMSource(document); + final StringWriter writer = new StringWriter(); + final StreamResult result = new StreamResult(writer); + final Transformer transformer = TRANSFORMER_FACTORY.newTransformer(); + transformer.transform(domSource, result); + + return writer.toString(); + } catch (TransformerException e) { + e.printStackTrace(); + return null; + } + } + +} diff --git a/src/test/java/com/alchemyapi/util/ResourceUtils.java b/src/test/java/com/alchemyapi/util/ResourceUtils.java new file mode 100644 index 0000000..6c9cce6 --- /dev/null +++ b/src/test/java/com/alchemyapi/util/ResourceUtils.java @@ -0,0 +1,21 @@ +package com.alchemyapi.util; + +import org.apache.commons.io.IOUtils; + +import java.io.IOException; +import java.io.InputStream; + +/** + * Created by kenny + */ +public class ResourceUtils { + + public static String toString(final String resource) throws IOException { + return IOUtils.toString(toInputStream(resource)); + } + + public static InputStream toInputStream(final String resource) { + return Thread.currentThread().getContextClassLoader().getResourceAsStream(resource); + } + +} diff --git a/src/test/resources/api_key.txt b/src/test/resources/api_key.txt new file mode 100644 index 0000000..3950f0b --- /dev/null +++ b/src/test/resources/api_key.txt @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/testdir/data/cat.jpg b/src/test/resources/data/cat.jpg similarity index 100% rename from testdir/data/cat.jpg rename to src/test/resources/data/cat.jpg diff --git a/testdir/data/example.html b/src/test/resources/data/example.html similarity index 100% rename from testdir/data/example.html rename to src/test/resources/data/example.html diff --git a/testdir/data/example2.html b/src/test/resources/data/example2.html similarity index 100% rename from testdir/data/example2.html rename to src/test/resources/data/example2.html diff --git a/testdir/data/example2_nolinks.html b/src/test/resources/data/example2_nolinks.html similarity index 100% rename from testdir/data/example2_nolinks.html rename to src/test/resources/data/example2_nolinks.html diff --git a/testdir/data/example3.html b/src/test/resources/data/example3.html similarity index 100% rename from testdir/data/example3.html rename to src/test/resources/data/example3.html diff --git a/testdir/data/microformats.html b/src/test/resources/data/microformats.html similarity index 100% rename from testdir/data/microformats.html rename to src/test/resources/data/microformats.html diff --git a/testdir/api_key.txt b/testdir/api_key.txt deleted file mode 100644 index e69de29..0000000 From bd46b6e1155dbdd005080d06a32dbea6c37a5cc1 Mon Sep 17 00:00:00 2001 From: Kenny Cason Date: Sun, 2 Aug 2015 02:57:30 -0500 Subject: [PATCH 02/11] #8 refactor sentiment test --- src/test/java/com/alchemyapi/STestAuthor.java | 28 ++--- .../java/com/alchemyapi/STestSentiment.java | 79 +++++++++++++ .../java/com/alchemyapi/SentimentTest.java | 106 ------------------ .../com/alchemyapi/util/ResourceUtils.java | 9 +- 4 files changed, 101 insertions(+), 121 deletions(-) create mode 100644 src/test/java/com/alchemyapi/STestSentiment.java delete mode 100644 src/test/java/com/alchemyapi/SentimentTest.java diff --git a/src/test/java/com/alchemyapi/STestAuthor.java b/src/test/java/com/alchemyapi/STestAuthor.java index 97c426f..6944075 100644 --- a/src/test/java/com/alchemyapi/STestAuthor.java +++ b/src/test/java/com/alchemyapi/STestAuthor.java @@ -12,19 +12,21 @@ import java.io.IOException; public class STestAuthor { - - @Test - public void demo() throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { - // Create an AlchemyAPI object. - final AlchemyAPI alchemyAPI = AlchemyAPI.GetInstanceFromString(ResourceUtils.toString("api_key.txt")); - - // Load a Html document to analyze. - final String htmlDoc = ResourceUtils.toString("data/example.html"); - final Document doc = alchemyAPI.URLGetAuthor("http://www.politico.com/blogs/media/2012/02/detroit-news-ed-upset-over-romney-edit-115247.html"); - System.out.println(DocumentUtils.toString(doc)); - - final Document doc2 = alchemyAPI.HTMLGetAuthor(htmlDoc, "http://www.test.com/"); - System.out.println(DocumentUtils.toString(doc2)); + + private final AlchemyAPI alchemyAPI = AlchemyAPI.GetInstanceFromString(ResourceUtils.toString("api_key.txt")); + + @Test + public void parseFromTestData() throws SAXException, ParserConfigurationException, XPathExpressionException, IOException { + final String html = ResourceUtils.toString("data/example.html"); + + final Document document = alchemyAPI.HTMLGetAuthor(html, "http://www.test.com/"); + System.out.println(DocumentUtils.toString(document)); + } + + @Test + public void parseFromUrl() throws SAXException, ParserConfigurationException, XPathExpressionException, IOException { + final Document document = alchemyAPI.URLGetAuthor("http://www.politico.com/blogs/media/2012/02/detroit-news-ed-upset-over-romney-edit-115247.html"); + System.out.println(DocumentUtils.toString(document)); } } diff --git a/src/test/java/com/alchemyapi/STestSentiment.java b/src/test/java/com/alchemyapi/STestSentiment.java new file mode 100644 index 0000000..9ef7560 --- /dev/null +++ b/src/test/java/com/alchemyapi/STestSentiment.java @@ -0,0 +1,79 @@ +package com.alchemyapi; + +import com.alchemyapi.api.AlchemyAPI; +import com.alchemyapi.api.AlchemyAPI_KeywordParams; +import com.alchemyapi.api.AlchemyAPI_NamedEntityParams; +import com.alchemyapi.api.AlchemyAPI_TargetedSentimentParams; +import com.alchemyapi.util.DocumentUtils; +import com.alchemyapi.util.ResourceUtils; +import org.junit.Test; +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.xpath.XPathExpressionException; +import java.io.IOException; + +public class STestSentiment { + + private final AlchemyAPI alchemyAPI = AlchemyAPI.GetInstanceFromString(ResourceUtils.toString("api_key.txt")); + + @Test + public void url() throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { + final Document document = alchemyAPI.URLGetTextSentiment("http://www.techcrunch.com/"); + System.out.println(DocumentUtils.toString(document)); + } + + @Test + public void text() throws SAXException, ParserConfigurationException, XPathExpressionException, IOException { + final Document document = alchemyAPI.TextGetTextSentiment("That hat is ridiculous, Charles."); + System.out.println(DocumentUtils.toString(document)); + } + + @Test + public void file() throws SAXException, ParserConfigurationException, XPathExpressionException, IOException { + final String html = ResourceUtils.toString("data/example.html"); + + final Document document = alchemyAPI.HTMLGetTextSentiment(html, "http://www.test.com/"); + System.out.println(DocumentUtils.toString(document)); + } + + @Test + public void entityTargetedSentimentText() throws SAXException, ParserConfigurationException, XPathExpressionException, IOException { + final AlchemyAPI_NamedEntityParams entityParams = new AlchemyAPI_NamedEntityParams(); + entityParams.setSentiment(true); + final Document document = alchemyAPI.TextGetRankedNamedEntities("That Mike Tyson is such a sweetheart.", entityParams); + System.out.println(DocumentUtils.toString(document)); + + final AlchemyAPI_KeywordParams keywordParams = new AlchemyAPI_KeywordParams(); + keywordParams.setSentiment(true); + final Document document2 = alchemyAPI.TextGetRankedKeywords("That Mike Tyson is such a sweetheart.", keywordParams); + System.out.println(DocumentUtils.toString(document2)); + + final AlchemyAPI_TargetedSentimentParams sentimentParams = new AlchemyAPI_TargetedSentimentParams(); + sentimentParams.setShowSourceText(true); + final Document document3 = alchemyAPI.TextGetTargetedSentiment("This car is terrible.", "car", sentimentParams); + System.out.print(DocumentUtils.toString(document3)); + } + + @Test + public void entityTargetedSentimentUrl() throws SAXException, ParserConfigurationException, XPathExpressionException, IOException { + final AlchemyAPI_TargetedSentimentParams sentimentParams = new AlchemyAPI_TargetedSentimentParams(); + sentimentParams.setShowSourceText(true); + + final Document document = alchemyAPI.URLGetTargetedSentiment("http://techcrunch.com/2012/03/01/keen-on-anand-rajaraman-how-walmart-wants-to-leapfrog-over-amazon-tctv/", "Walmart", sentimentParams); + System.out.print(DocumentUtils.toString(document)); + } + + @Test + public void entityTargetedSentimentHtml() throws SAXException, ParserConfigurationException, XPathExpressionException, IOException { + final AlchemyAPI_TargetedSentimentParams sentimentParams = new AlchemyAPI_TargetedSentimentParams(); + sentimentParams.setShowSourceText(true); + + final String html = ResourceUtils.toString("data/example.html"); + final Document document2 = alchemyAPI.HTMLGetTargetedSentiment(html, "http://www.test.com/", "WujWuj", sentimentParams); + System.out.print(DocumentUtils.toString(document2)); + } + + +} diff --git a/src/test/java/com/alchemyapi/SentimentTest.java b/src/test/java/com/alchemyapi/SentimentTest.java deleted file mode 100644 index a1731aa..0000000 --- a/src/test/java/com/alchemyapi/SentimentTest.java +++ /dev/null @@ -1,106 +0,0 @@ -package com.alchemyapi; - -import com.alchemyapi.api.AlchemyAPI; -import com.alchemyapi.api.*; - -import org.xml.sax.SAXException; -import org.w3c.dom.Document; -import java.io.*; -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.xpath.XPathExpressionException; -import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerException; -import javax.xml.transform.TransformerFactory; -import javax.xml.transform.dom.DOMSource; -import javax.xml.transform.stream.StreamResult; - -class SentimentTest { - public static void main(String[] args) throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException { - // Create an AlchemyAPI object. - 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)); - - // Extract sentiment for a text string. - doc = alchemyObj.TextGetTextSentiment( - "That hat is ridiculous, Charles."); - System.out.println(getStringFromDocument(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)); - - // 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)); - - // 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)); - - //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)); - - //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)); - - //Extract Targeted Sentiment from html - doc = alchemyObj.HTMLGetTargetedSentiment(htmlDoc, "http://www.test.com/", "WujWuj", sentimentParams); - System.out.print(getStringFromDocument(doc)); -} - - // utility function - private static String getFileContents(String filename) - throws IOException, FileNotFoundException - { - File file = new File(filename); - StringBuilder contents = new StringBuilder(); - - BufferedReader input = new BufferedReader(new FileReader(file)); - - try { - String line = null; - - while ((line = input.readLine()) != null) { - contents.append(line); - contents.append(System.getProperty("line.separator")); - } - } finally { - input.close(); - } - - return contents.toString(); - } - - // utility method - private static String getStringFromDocument(Document doc) { - try { - DOMSource domSource = new DOMSource(doc); - StringWriter writer = new StringWriter(); - StreamResult result = new StreamResult(writer); - - TransformerFactory tf = TransformerFactory.newInstance(); - Transformer transformer = tf.newTransformer(); - transformer.transform(domSource, result); - - return writer.toString(); - } catch (TransformerException ex) { - ex.printStackTrace(); - return null; - } - } -} diff --git a/src/test/java/com/alchemyapi/util/ResourceUtils.java b/src/test/java/com/alchemyapi/util/ResourceUtils.java index 6c9cce6..c278220 100644 --- a/src/test/java/com/alchemyapi/util/ResourceUtils.java +++ b/src/test/java/com/alchemyapi/util/ResourceUtils.java @@ -10,8 +10,13 @@ */ public class ResourceUtils { - public static String toString(final String resource) throws IOException { - return IOUtils.toString(toInputStream(resource)); + public static String toString(final String resource) { + try { + return IOUtils.toString(toInputStream(resource)); + } catch (IOException e) { + e.printStackTrace(); + return null; + } } public static InputStream toInputStream(final String resource) { From 9da308cad8a7e022d0760db8346d5593d3f7bd9f Mon Sep 17 00:00:00 2001 From: Kenny Cason Date: Sun, 2 Aug 2015 03:08:05 -0500 Subject: [PATCH 03/11] #8 refactor parameters code --- .../java/com/alchemyapi/api/AlchemyAPI.java | 236 ++++++++++-------- .../api/AlchemyAPI_CategoryParams.java | 61 ----- .../api/AlchemyAPI_CombinedParams.java | 166 ------------ .../api/AlchemyAPI_ConceptParams.java | 96 ------- .../api/AlchemyAPI_ConstraintQueryParams.java | 35 --- .../api/AlchemyAPI_ImageParams.java | 70 ------ .../api/AlchemyAPI_KeywordParams.java | 122 --------- .../api/AlchemyAPI_LanguageParams.java | 60 ----- .../api/AlchemyAPI_NamedEntityParams.java | 125 ---------- .../com/alchemyapi/api/AlchemyAPI_Params.java | 80 ------ .../api/AlchemyAPI_RelationParams.java | 140 ----------- .../AlchemyAPI_TargetedSentimentParams.java | 40 --- .../api/AlchemyAPI_TaxonomyParams.java | 119 --------- .../alchemyapi/api/AlchemyAPI_TextParams.java | 32 --- .../api/parameters/CategoryParameters.java | 66 +++++ .../api/parameters/CombinedParameters.java | 187 ++++++++++++++ .../api/parameters/ConceptParameters.java | 93 +++++++ .../parameters/ConstraintQueryParameters.java | 34 +++ .../api/parameters/ImageParameters.java | 80 ++++++ .../api/parameters/KeywordParameters.java | 119 +++++++++ .../api/parameters/LanguageParameters.java | 58 +++++ .../api/parameters/NamedEntityParameters.java | 143 +++++++++++ .../alchemyapi/api/parameters/Parameters.java | 85 +++++++ .../api/parameters/RelationParameters.java | 163 ++++++++++++ .../TargetedSentimentParameters.java | 39 +++ .../api/parameters/TaxonomyParameters.java | 138 ++++++++++ .../api/parameters/TextParameters.java | 32 +++ .../java/com/alchemyapi/CategoryTest.java | 10 +- .../java/com/alchemyapi/CombinedTest.java | 3 +- src/test/java/com/alchemyapi/ImageTest.java | 6 +- .../java/com/alchemyapi/ParameterTest.java | 3 +- .../java/com/alchemyapi/RelationsTest.java | 3 +- .../java/com/alchemyapi/STestSentiment.java | 16 +- 33 files changed, 1385 insertions(+), 1275 deletions(-) delete mode 100644 src/main/java/com/alchemyapi/api/AlchemyAPI_CategoryParams.java delete mode 100644 src/main/java/com/alchemyapi/api/AlchemyAPI_CombinedParams.java delete mode 100644 src/main/java/com/alchemyapi/api/AlchemyAPI_ConceptParams.java delete mode 100644 src/main/java/com/alchemyapi/api/AlchemyAPI_ConstraintQueryParams.java delete mode 100644 src/main/java/com/alchemyapi/api/AlchemyAPI_ImageParams.java delete mode 100644 src/main/java/com/alchemyapi/api/AlchemyAPI_KeywordParams.java delete mode 100644 src/main/java/com/alchemyapi/api/AlchemyAPI_LanguageParams.java delete mode 100644 src/main/java/com/alchemyapi/api/AlchemyAPI_NamedEntityParams.java delete mode 100644 src/main/java/com/alchemyapi/api/AlchemyAPI_Params.java delete mode 100644 src/main/java/com/alchemyapi/api/AlchemyAPI_RelationParams.java delete mode 100644 src/main/java/com/alchemyapi/api/AlchemyAPI_TargetedSentimentParams.java delete mode 100644 src/main/java/com/alchemyapi/api/AlchemyAPI_TaxonomyParams.java delete mode 100644 src/main/java/com/alchemyapi/api/AlchemyAPI_TextParams.java create mode 100644 src/main/java/com/alchemyapi/api/parameters/CategoryParameters.java create mode 100644 src/main/java/com/alchemyapi/api/parameters/CombinedParameters.java create mode 100644 src/main/java/com/alchemyapi/api/parameters/ConceptParameters.java create mode 100644 src/main/java/com/alchemyapi/api/parameters/ConstraintQueryParameters.java create mode 100644 src/main/java/com/alchemyapi/api/parameters/ImageParameters.java create mode 100644 src/main/java/com/alchemyapi/api/parameters/KeywordParameters.java create mode 100644 src/main/java/com/alchemyapi/api/parameters/LanguageParameters.java create mode 100644 src/main/java/com/alchemyapi/api/parameters/NamedEntityParameters.java create mode 100644 src/main/java/com/alchemyapi/api/parameters/Parameters.java create mode 100644 src/main/java/com/alchemyapi/api/parameters/RelationParameters.java create mode 100644 src/main/java/com/alchemyapi/api/parameters/TargetedSentimentParameters.java create mode 100644 src/main/java/com/alchemyapi/api/parameters/TaxonomyParameters.java create mode 100644 src/main/java/com/alchemyapi/api/parameters/TextParameters.java diff --git a/src/main/java/com/alchemyapi/api/AlchemyAPI.java b/src/main/java/com/alchemyapi/api/AlchemyAPI.java index 615580b..733f332 100644 --- a/src/main/java/com/alchemyapi/api/AlchemyAPI.java +++ b/src/main/java/com/alchemyapi/api/AlchemyAPI.java @@ -1,26 +1,40 @@ package com.alchemyapi.api; -import java.io.File; -import java.io.FileInputStream; +import com.alchemyapi.api.parameters.CategoryParameters; +import com.alchemyapi.api.parameters.CombinedParameters; +import com.alchemyapi.api.parameters.ConceptParameters; +import com.alchemyapi.api.parameters.ConstraintQueryParameters; +import com.alchemyapi.api.parameters.ImageParameters; +import com.alchemyapi.api.parameters.KeywordParameters; +import com.alchemyapi.api.parameters.LanguageParameters; +import com.alchemyapi.api.parameters.NamedEntityParameters; +import com.alchemyapi.api.parameters.Parameters; +import com.alchemyapi.api.parameters.RelationParameters; +import com.alchemyapi.api.parameters.TargetedSentimentParameters; +import com.alchemyapi.api.parameters.TaxonomyParameters; +import com.alchemyapi.api.parameters.TextParameters; +import org.w3c.dom.Document; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; + +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathConstants; +import javax.xml.xpath.XPathExpression; +import javax.xml.xpath.XPathExpressionException; +import javax.xml.xpath.XPathFactory; +import java.io.BufferedReader; import java.io.DataInputStream; import java.io.DataOutputStream; -import java.io.InputStreamReader; -import java.io.BufferedReader; -import java.io.IOException; +import java.io.File; +import java.io.FileInputStream; import java.io.FileNotFoundException; - +import java.io.IOException; +import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.xpath.*; - -import org.w3c.dom.Document; -import org.w3c.dom.NodeList; - -import org.xml.sax.SAXException; - public class AlchemyAPI { private String _apiKey; @@ -84,10 +98,10 @@ public Document URLGetAuthor(String url) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { - return URLGetAuthor(url, new AlchemyAPI_Params()); + return URLGetAuthor(url, new Parameters()); } - public Document URLGetAuthor(String url, AlchemyAPI_Params params) + public Document URLGetAuthor(String url, Parameters params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -102,11 +116,11 @@ public Document HTMLGetAuthor(String html, String url) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { - return HTMLGetAuthor(html, url, new AlchemyAPI_Params()); + return HTMLGetAuthor(html, url, new Parameters()); } public Document HTMLGetAuthor(String html, String url, - AlchemyAPI_Params params) + Parameters params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -122,10 +136,10 @@ public Document URLGetRankedNamedEntities(String url) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { - return URLGetRankedNamedEntities(url, new AlchemyAPI_NamedEntityParams()); + return URLGetRankedNamedEntities(url, new NamedEntityParameters()); } - public Document URLGetRankedNamedEntities(String url, AlchemyAPI_NamedEntityParams params) + public Document URLGetRankedNamedEntities(String url, NamedEntityParameters params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -140,11 +154,11 @@ public Document HTMLGetRankedNamedEntities(String html, String url) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { - return HTMLGetRankedNamedEntities(html, url, new AlchemyAPI_NamedEntityParams()); + return HTMLGetRankedNamedEntities(html, url, new NamedEntityParameters()); } - public Document HTMLGetRankedNamedEntities(String html, String url, AlchemyAPI_NamedEntityParams params) + public Document HTMLGetRankedNamedEntities(String html, String url, NamedEntityParameters params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -160,10 +174,10 @@ public Document TextGetRankedNamedEntities(String text) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { - return TextGetRankedNamedEntities(text, new AlchemyAPI_NamedEntityParams()); + return TextGetRankedNamedEntities(text, new NamedEntityParameters()); } - public Document TextGetRankedNamedEntities(String text, AlchemyAPI_NamedEntityParams params) + public Document TextGetRankedNamedEntities(String text, NamedEntityParameters params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -180,10 +194,10 @@ public Document URLGetRankedConcepts(String url) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { - return URLGetRankedConcepts(url, new AlchemyAPI_ConceptParams()); + return URLGetRankedConcepts(url, new ConceptParameters()); } - public Document URLGetRankedConcepts(String url, AlchemyAPI_ConceptParams params) + public Document URLGetRankedConcepts(String url, ConceptParameters params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -199,10 +213,10 @@ public Document HTMLGetRankedConcepts(String html, String url) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { - return HTMLGetRankedConcepts(html, url, new AlchemyAPI_ConceptParams()); + return HTMLGetRankedConcepts(html, url, new ConceptParameters()); } - public Document HTMLGetRankedConcepts(String html, String url, AlchemyAPI_ConceptParams params) + public Document HTMLGetRankedConcepts(String html, String url, ConceptParameters params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -216,10 +230,10 @@ public Document HTMLGetRankedConcepts(String html, String url, AlchemyAPI_Concep public Document TextGetRankedConcepts(String text) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { - return TextGetRankedConcepts(text, new AlchemyAPI_ConceptParams()); + return TextGetRankedConcepts(text, new ConceptParameters()); } - public Document TextGetRankedConcepts(String text, AlchemyAPI_ConceptParams params) throws IOException, SAXException, + public Document TextGetRankedConcepts(String text, ConceptParameters params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { CheckText(text); @@ -235,10 +249,10 @@ public Document URLGetRankedKeywords(String url) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { - return URLGetRankedKeywords(url, new AlchemyAPI_KeywordParams()); + return URLGetRankedKeywords(url, new KeywordParameters()); } - public Document URLGetRankedKeywords(String url, AlchemyAPI_KeywordParams params) + public Document URLGetRankedKeywords(String url, KeywordParameters params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -254,10 +268,10 @@ public Document HTMLGetRankedKeywords(String html, String url) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { - return HTMLGetRankedKeywords(html, url, new AlchemyAPI_KeywordParams()); + return HTMLGetRankedKeywords(html, url, new KeywordParameters()); } - public Document HTMLGetRankedKeywords(String html, String url, AlchemyAPI_KeywordParams params) + public Document HTMLGetRankedKeywords(String html, String url, KeywordParameters params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -271,10 +285,10 @@ public Document HTMLGetRankedKeywords(String html, String url, AlchemyAPI_Keywor public Document TextGetRankedKeywords(String text) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { - return TextGetRankedKeywords(text, new AlchemyAPI_KeywordParams()); + return TextGetRankedKeywords(text, new KeywordParameters()); } - public Document TextGetRankedKeywords(String text, AlchemyAPI_KeywordParams params) throws IOException, SAXException, + public Document TextGetRankedKeywords(String text, KeywordParameters params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { CheckText(text); @@ -288,10 +302,10 @@ public Document URLGetLanguage(String url) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { - return URLGetLanguage(url, new AlchemyAPI_LanguageParams()); + return URLGetLanguage(url, new LanguageParameters()); } - public Document URLGetLanguage(String url, AlchemyAPI_LanguageParams params) + public Document URLGetLanguage(String url, LanguageParameters params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -306,10 +320,10 @@ public Document HTMLGetLanguage(String html, String url) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { - return HTMLGetLanguage(html, url, new AlchemyAPI_LanguageParams()); + return HTMLGetLanguage(html, url, new LanguageParameters()); } - public Document HTMLGetLanguage(String html, String url, AlchemyAPI_LanguageParams params) + public Document HTMLGetLanguage(String html, String url, LanguageParameters params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -325,10 +339,10 @@ public Document TextGetLanguage(String text) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { - return TextGetLanguage(text, new AlchemyAPI_LanguageParams()); + return TextGetLanguage(text, new LanguageParameters()); } - public Document TextGetLanguage(String text, AlchemyAPI_LanguageParams params) + public Document TextGetLanguage(String text, LanguageParameters params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -343,10 +357,10 @@ public Document URLGetCategory(String url) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { - return URLGetCategory(url, new AlchemyAPI_CategoryParams()); + return URLGetCategory(url, new CategoryParameters()); } - public Document URLGetCategory(String url, AlchemyAPI_CategoryParams params) + public Document URLGetCategory(String url, CategoryParameters params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -361,10 +375,10 @@ public Document HTMLGetCategory(String html, String url) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { - return HTMLGetCategory(html, url, new AlchemyAPI_CategoryParams()); + return HTMLGetCategory(html, url, new CategoryParameters()); } - public Document HTMLGetCategory(String html, String url, AlchemyAPI_CategoryParams params) + public Document HTMLGetCategory(String html, String url, CategoryParameters params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -380,10 +394,10 @@ public Document TextGetCategory(String text) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { - return TextGetCategory(text, new AlchemyAPI_TextParams()); + return TextGetCategory(text, new TextParameters()); } - public Document TextGetCategory(String text, AlchemyAPI_TextParams params) + public Document TextGetCategory(String text, TextParameters params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -398,10 +412,10 @@ public Document URLGetText(String url) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { - return URLGetText(url, new AlchemyAPI_TextParams()); + return URLGetText(url, new TextParameters()); } - public Document URLGetText(String url, AlchemyAPI_TextParams params) + public Document URLGetText(String url, TextParameters params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -416,10 +430,10 @@ public Document HTMLGetText(String html, String url) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { - return HTMLGetText(html, url, new AlchemyAPI_TextParams()); + return HTMLGetText(html, url, new TextParameters()); } - public Document HTMLGetText(String html, String url, AlchemyAPI_TextParams params) + public Document HTMLGetText(String html, String url, TextParameters params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -435,10 +449,10 @@ public Document URLGetRawText(String url) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { - return URLGetRawText(url, new AlchemyAPI_Params()); + return URLGetRawText(url, new Parameters()); } - public Document URLGetRawText(String url, AlchemyAPI_Params params) + public Document URLGetRawText(String url, Parameters params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -453,10 +467,10 @@ public Document HTMLGetRawText(String html, String url) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { - return HTMLGetRawText(html, url, new AlchemyAPI_Params()); + return HTMLGetRawText(html, url, new Parameters()); } - public Document HTMLGetRawText(String html, String url, AlchemyAPI_Params params) + public Document HTMLGetRawText(String html, String url, Parameters params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -472,10 +486,10 @@ public Document URLGetTitle(String url) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { - return URLGetTitle(url, new AlchemyAPI_Params()); + return URLGetTitle(url, new Parameters()); } - public Document URLGetTitle(String url, AlchemyAPI_Params params) + public Document URLGetTitle(String url, Parameters params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -490,10 +504,10 @@ public Document HTMLGetTitle(String html, String url) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { - return HTMLGetTitle(html, url, new AlchemyAPI_Params()); + return HTMLGetTitle(html, url, new Parameters()); } - public Document HTMLGetTitle(String html, String url, AlchemyAPI_Params params) + public Document HTMLGetTitle(String html, String url, Parameters params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -509,10 +523,10 @@ public Document URLGetFeedLinks(String url) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { - return URLGetFeedLinks(url, new AlchemyAPI_Params()); + return URLGetFeedLinks(url, new Parameters()); } - public Document URLGetFeedLinks(String url, AlchemyAPI_Params params) + public Document URLGetFeedLinks(String url, Parameters params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -527,10 +541,10 @@ public Document HTMLGetFeedLinks(String html, String url) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { - return HTMLGetFeedLinks(html, url, new AlchemyAPI_Params()); + return HTMLGetFeedLinks(html, url, new Parameters()); } - public Document HTMLGetFeedLinks(String html, String url, AlchemyAPI_Params params) + public Document HTMLGetFeedLinks(String html, String url, Parameters params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -546,10 +560,10 @@ public Document URLGetMicroformats(String url) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { - return URLGetMicroformats(url, new AlchemyAPI_Params()); + return URLGetMicroformats(url, new Parameters()); } - public Document URLGetMicroformats(String url, AlchemyAPI_Params params) + public Document URLGetMicroformats(String url, Parameters params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -564,10 +578,10 @@ public Document HTMLGetMicroformats(String html, String url) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { - return HTMLGetMicroformats(html, url, new AlchemyAPI_Params()); + return HTMLGetMicroformats(html, url, new Parameters()); } - public Document HTMLGetMicroformats(String html, String url, AlchemyAPI_Params params) + public Document HTMLGetMicroformats(String html, String url, Parameters params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -583,10 +597,10 @@ public Document URLGetConstraintQuery(String url, String query) throws IOException, XPathExpressionException, SAXException, ParserConfigurationException { - return URLGetConstraintQuery(url, query, new AlchemyAPI_ConstraintQueryParams()); + return URLGetConstraintQuery(url, query, new ConstraintQueryParameters()); } - public Document URLGetConstraintQuery(String url, String query, AlchemyAPI_ConstraintQueryParams params) + public Document URLGetConstraintQuery(String url, String query, ConstraintQueryParameters params) throws IOException, XPathExpressionException, SAXException, ParserConfigurationException { @@ -605,10 +619,10 @@ public Document HTMLGetConstraintQuery(String html, String url, String query) throws IOException, XPathExpressionException, SAXException, ParserConfigurationException { - return HTMLGetConstraintQuery(html, url, query, new AlchemyAPI_ConstraintQueryParams()); + return HTMLGetConstraintQuery(html, url, query, new ConstraintQueryParameters()); } - public Document HTMLGetConstraintQuery(String html, String url, String query, AlchemyAPI_ConstraintQueryParams params) + public Document HTMLGetConstraintQuery(String html, String url, String query, ConstraintQueryParameters params) throws IOException, XPathExpressionException, SAXException, ParserConfigurationException { @@ -627,10 +641,10 @@ public Document URLGetTextSentiment(String url) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { - return URLGetTextSentiment(url, new AlchemyAPI_Params()); + return URLGetTextSentiment(url, new Parameters()); } - public Document URLGetTextSentiment(String url, AlchemyAPI_Params params) + public Document URLGetTextSentiment(String url, Parameters params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -646,10 +660,10 @@ public Document HTMLGetTextSentiment(String html, String url) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { - return HTMLGetTextSentiment(html, url, new AlchemyAPI_Params()); + return HTMLGetTextSentiment(html, url, new Parameters()); } - public Document HTMLGetTextSentiment(String html, String url, AlchemyAPI_Params params) + public Document HTMLGetTextSentiment(String html, String url, Parameters params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -665,10 +679,10 @@ public Document TextGetTextSentiment(String text) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { - return TextGetTextSentiment(text, new AlchemyAPI_Params()); + return TextGetTextSentiment(text, new Parameters()); } - public Document TextGetTextSentiment(String text, AlchemyAPI_Params params) + public Document TextGetTextSentiment(String text, Parameters params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -686,11 +700,11 @@ public Document URLGetTargetedSentiment(String url, String target) XPathExpressionException { return URLGetTargetedSentiment(url, target, - new AlchemyAPI_TargetedSentimentParams()); + new TargetedSentimentParameters()); } public Document URLGetTargetedSentiment(String url, String target, - AlchemyAPI_TargetedSentimentParams params) + TargetedSentimentParameters params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -708,11 +722,11 @@ public Document HTMLGetTargetedSentiment(String html, String url, String target) XPathExpressionException { return HTMLGetTargetedSentiment(html, url, target, - new AlchemyAPI_TargetedSentimentParams()); + new TargetedSentimentParameters()); } public Document HTMLGetTargetedSentiment(String html, String url, String target, - AlchemyAPI_TargetedSentimentParams params) + TargetedSentimentParameters params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -731,11 +745,11 @@ public Document TextGetTargetedSentiment(String text, String target) XPathExpressionException { return TextGetTargetedSentiment(text, target, - new AlchemyAPI_TargetedSentimentParams()); + new TargetedSentimentParameters()); } public Document TextGetTargetedSentiment(String text, String target, - AlchemyAPI_TargetedSentimentParams params) + TargetedSentimentParameters params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -753,10 +767,10 @@ public Document TextGetTargetedSentiment(String text, String target, public Document URLGetRelations(String url) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { - return URLGetRelations(url, new AlchemyAPI_RelationParams()); + return URLGetRelations(url, new RelationParameters()); } - public Document URLGetRelations(String url, AlchemyAPI_RelationParams params) + public Document URLGetRelations(String url, RelationParameters params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -771,10 +785,10 @@ public Document HTMLGetRelations(String html, String url) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { - return HTMLGetRelations(html, url, new AlchemyAPI_RelationParams()); + return HTMLGetRelations(html, url, new RelationParameters()); } - public Document HTMLGetRelations(String html, String url, AlchemyAPI_RelationParams params) + public Document HTMLGetRelations(String html, String url, RelationParameters params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -790,10 +804,10 @@ public Document TextGetRelations(String text) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { - return TextGetRelations(text, new AlchemyAPI_RelationParams()); + return TextGetRelations(text, new RelationParameters()); } - public Document TextGetRelations(String text, AlchemyAPI_RelationParams params) + public Document TextGetRelations(String text, RelationParameters params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -809,12 +823,12 @@ public Document TextGetRelations(String text, AlchemyAPI_RelationParams params) public Document URLGetCombined(String url) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { - AlchemyAPI_CombinedParams params = new AlchemyAPI_CombinedParams(); + CombinedParameters params = new CombinedParameters(); params.setExtractAll(); return URLGetCombined(url, params); } - public Document URLGetCombined(String url, AlchemyAPI_CombinedParams params) + public Document URLGetCombined(String url, CombinedParameters params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -829,12 +843,12 @@ public Document TextGetCombined(String text) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { - AlchemyAPI_CombinedParams params = new AlchemyAPI_CombinedParams(); + CombinedParameters params = new CombinedParameters(); params.setExtractAll(); return TextGetCombined(text, params); } - public Document TextGetCombined(String text, AlchemyAPI_CombinedParams params) + public Document TextGetCombined(String text, CombinedParameters params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -850,10 +864,10 @@ public Document TextGetCombined(String text, AlchemyAPI_CombinedParams params) public Document URLGetTaxonomy(String url) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { - return URLGetTaxonomy(url, new AlchemyAPI_TaxonomyParams()); + return URLGetTaxonomy(url, new TaxonomyParameters()); } - public Document URLGetTaxonomy(String url, AlchemyAPI_TaxonomyParams params) + public Document URLGetTaxonomy(String url, TaxonomyParameters params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -868,10 +882,10 @@ public Document HTMLGetTaxonomy(String html, String url) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { - return HTMLGetTaxonomy(html, url, new AlchemyAPI_TaxonomyParams()); + return HTMLGetTaxonomy(html, url, new TaxonomyParameters()); } - public Document HTMLGetTaxonomy(String html, String url, AlchemyAPI_TaxonomyParams params) + public Document HTMLGetTaxonomy(String html, String url, TaxonomyParameters params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -887,10 +901,10 @@ public Document TextGetTaxonomy(String text) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { - return TextGetTaxonomy(text, new AlchemyAPI_TaxonomyParams()); + return TextGetTaxonomy(text, new TaxonomyParameters()); } - public Document TextGetTaxonomy(String text, AlchemyAPI_TaxonomyParams params) + public Document TextGetTaxonomy(String text, TaxonomyParameters params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -906,10 +920,10 @@ public Document TextGetTaxonomy(String text, AlchemyAPI_TaxonomyParams params) public Document URLGetImage(String url) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { - return URLGetImage(url, new AlchemyAPI_ImageParams()); + return URLGetImage(url, new ImageParameters()); } - public Document URLGetImage(String url, AlchemyAPI_ImageParams params) + public Document URLGetImage(String url, ImageParameters params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -923,10 +937,10 @@ public Document URLGetImage(String url, AlchemyAPI_ImageParams params) public Document URLGetRankedImageKeywords(String url) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { - return URLGetRankedImageKeywords(url, new AlchemyAPI_ImageParams()); + return URLGetRankedImageKeywords(url, new ImageParameters()); } - public Document URLGetRankedImageKeywords(String url, AlchemyAPI_ImageParams params) + public Document URLGetRankedImageKeywords(String url, ImageParameters params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -937,7 +951,7 @@ public Document URLGetRankedImageKeywords(String url, AlchemyAPI_ImageParams par return GET("URLGetRankedImageKeywords", "url", params); } - public Document ImageGetRankedImageKeywords(AlchemyAPI_ImageParams params) + public Document ImageGetRankedImageKeywords(ImageParameters params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -976,7 +990,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, Parameters params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -992,7 +1006,7 @@ private Document GET(String callName, String callPrefix, AlchemyAPI_Params param return doRequest(handle, params.getOutputMode()); } - private Document POST(String callName, String callPrefix, AlchemyAPI_Params params) + private Document POST(String callName, String callPrefix, Parameters params) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { @@ -1015,6 +1029,8 @@ private Document POST(String callName, String callPrefix, AlchemyAPI_Params para return doRequest(handle, params.getOutputMode()); } + // TODO currently hard-coded to handle xml, create xml/json adapter based on request + // TODO return pojo with parsed field, but allow a "raw" xml/json getter to protect against api updates private Document doRequest(HttpURLConnection handle, String outputMode) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException @@ -1027,7 +1043,7 @@ private Document doRequest(HttpURLConnection handle, String outputMode) XPathFactory factory = XPathFactory.newInstance(); - if(AlchemyAPI_Params.OUTPUT_XML.equals(outputMode)) { + if(Parameters.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()"); @@ -1037,7 +1053,7 @@ private Document doRequest(HttpURLConnection handle, String outputMode) throw new IOException("Error making API call: " + statusStr + '.'); } } - else if(AlchemyAPI_Params.OUTPUT_RDF.equals(outputMode)) { + else if(Parameters.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()"); diff --git a/src/main/java/com/alchemyapi/api/AlchemyAPI_CategoryParams.java b/src/main/java/com/alchemyapi/api/AlchemyAPI_CategoryParams.java deleted file mode 100644 index 8dc5ae3..0000000 --- a/src/main/java/com/alchemyapi/api/AlchemyAPI_CategoryParams.java +++ /dev/null @@ -1,61 +0,0 @@ -package com.alchemyapi.api; - -import java.io.UnsupportedEncodingException; -import java.net.URLEncoder; - -public class AlchemyAPI_CategoryParams extends AlchemyAPI_Params { - public static final String CLEANED_OR_RAW = "cleaned_or_raw"; - public static final String CQUERY = "cquery"; - public static final String XPATH = "xpath"; - - private String sourceText; - private String cQuery; - private String xPath; - private String baseUrl; - - public String getSourceText() { - return sourceText; - } - public void setSourceText(String sourceText) { - if( !sourceText.equals(AlchemyAPI_CategoryParams.CLEANED_OR_RAW) - && !sourceText.equals(AlchemyAPI_CategoryParams.CQUERY) - && !sourceText.equals(AlchemyAPI_CategoryParams.XPATH)) - { - throw new RuntimeException("Invalid setting " + sourceText + " for parameter sourceText"); - } - this.sourceText = sourceText; - } - public String getCQuery() { - return cQuery; - } - public void setCQuery(String cQuery) { - this.cQuery = cQuery; - } - public String getXPath() { - return xPath; - } - public void setXPath(String xPath) { - this.xPath = xPath; - } - public String getBaseUrl() { - return baseUrl; - } - public void setBaseUrl(String baseUrl) { - this.baseUrl = baseUrl; - } - - public String getParameterString(){ - String retString = super.getParameterString(); - try{ - if(sourceText!=null) retString+="&sourceText="+sourceText; - if(cQuery!=null) retString+="&cquery="+URLEncoder.encode(cQuery,"UTF-8"); - if(xPath!=null) retString+="&xpath="+URLEncoder.encode(xPath,"UTF-8"); - if(baseUrl!=null) retString+="&baseUrl="+URLEncoder.encode(baseUrl,"UTF-8"); - } - catch(UnsupportedEncodingException e ){ - retString = ""; - } - return retString; - } - -} diff --git a/src/main/java/com/alchemyapi/api/AlchemyAPI_CombinedParams.java b/src/main/java/com/alchemyapi/api/AlchemyAPI_CombinedParams.java deleted file mode 100644 index e2c4fad..0000000 --- a/src/main/java/com/alchemyapi/api/AlchemyAPI_CombinedParams.java +++ /dev/null @@ -1,166 +0,0 @@ -package com.alchemyapi.api; -import java.net.URLEncoder; -import java.io.UnsupportedEncodingException; - - - - public class AlchemyAPI_CombinedParams extends AlchemyAPI_Params{ - public static final String CLEANED_OR_RAW = "cleaned_or_raw"; - public static final String CLEANED = "cleaned"; - public static final String RAW = "raw"; - public static final String CQUERY = "cquery"; - public static final String XPATH = "xpath"; - public static final String EXTRACT_MODE_TRUST = "trust-metadata"; - public static final String EXTRACT_MODE_INFER = "always-infer"; - public static final String EXTRACT_PAGE_IMAGE = "page-image"; - public static final String EXTRACT_ENTITY = "entity"; - public static final String EXTRACT_KEYWORD = "keyword"; - public static final String EXTRACT_TITLE = "title"; - public static final String EXTRACT_AUTHOR = "author"; - public static final String EXTRACT_TAXONOMY = "taxonomy"; - public static final String EXTRACT_CONCEPT = "concept"; - public static final String EXTRACT_RELATION = "relation"; - public static final String EXTRACT_DOC_SENTIMENT = "doc-sentiment"; - - private Boolean disambiguate; - private Boolean linkedData; - private Boolean coreference; - private Boolean quotations; - private String sourceText; - private Boolean showSourceText; - private String cQuery; - private String xPath; - private Integer maxRetrieve; - private String baseUrl; - private Boolean sentiment = false; - private String extractMode; - private String extract; - - public boolean isDisambiguate() { - return disambiguate; - } - public void setDisambiguate(boolean disambiguate) { - this.disambiguate = disambiguate; - } - public boolean isLinkedData() { - return linkedData; - } - public void setLinkedData(boolean linkedData) { - this.linkedData = linkedData; - } - public boolean isCoreference() { - return coreference; - } - public void setCoreference(boolean coreference) { - this.coreference = coreference; - } - public boolean isQuotations() { - return quotations; - } - public void setQuotations(boolean quotations) { - this.quotations = quotations; - } - public String getSourceText() { - return sourceText; - } - public void setSourceText(String sourceText) { - if( !sourceText.equals(AlchemyAPI_CombinedParams.CLEANED) && !sourceText.equals(AlchemyAPI_CombinedParams.CLEANED_OR_RAW) - && !sourceText.equals(AlchemyAPI_CombinedParams.RAW) && !sourceText.equals(AlchemyAPI_CombinedParams.CQUERY) - && !sourceText.equals(AlchemyAPI_CombinedParams.XPATH)) - { - throw new RuntimeException("Invalid setting " + sourceText + " for parameter sourceText"); - } - this.sourceText = sourceText; - } - public boolean isShowSourceText() { - return showSourceText; - } - public void setShowSourceText(boolean showSourceText) { - this.showSourceText = showSourceText; - } - public String getCQuery() { - return cQuery; - } - public void setCQuery(String cQuery) { - this.cQuery = cQuery; - } - public String getXPath() { - return xPath; - } - public void setXPath(String xPath) { - this.xPath = xPath; - } - public int getMaxRetrieve() { - return maxRetrieve; - } - public void setMaxRetrieve(int maxRetrieve) { - this.maxRetrieve = maxRetrieve; - } - public String getBaseUrl() { - return baseUrl; - } - public void setBaseUrl(String baseUrl) { - this.baseUrl = baseUrl; - } - public boolean isSentiment() { - return sentiment; - } - public void setSentiment(boolean sentiment) { - this.sentiment = sentiment; - } - public void setExtractMode(String extractMode) { - if( !extractMode.equals(EXTRACT_MODE_TRUST) && !extractMode.equals(EXTRACT_MODE_INFER) ) - { - throw new RuntimeException("Invalid setting " + extractMode + " for parameter extractMode"); - } - this.extractMode = extractMode; - } - public void setExtractAll() { - this.extract = EXTRACT_PAGE_IMAGE + EXTRACT_ENTITY + EXTRACT_KEYWORD - + EXTRACT_TITLE + EXTRACT_AUTHOR + EXTRACT_TAXONOMY - + EXTRACT_CONCEPT + EXTRACT_RELATION + EXTRACT_DOC_SENTIMENT; - } - public void setExtract(String extractArg) { - if( !extractArg.equals(EXTRACT_PAGE_IMAGE) - && !extractArg.equals(EXTRACT_ENTITY) - && !extractArg.equals(EXTRACT_KEYWORD) - && !extractArg.equals(EXTRACT_TITLE) - && !extractArg.equals(EXTRACT_AUTHOR) - && !extractArg.equals(EXTRACT_TAXONOMY) - && !extractArg.equals(EXTRACT_CONCEPT) - && !extractArg.equals(EXTRACT_RELATION) - && !extractArg.equals(EXTRACT_DOC_SENTIMENT) ) - { - throw new RuntimeException("Invalid setting " + extractArg + " for parameter extract"); - } - if ((null == this.extract) || (0 == this.extract.length())) - this.extract = extractArg; - else - this.extract += "," + extractArg; - } - public String getParameterString(){ - String retString = super.getParameterString(); - try{ - if (extractMode!=null) retString+="&extractMode="+extractMode; - if (extract!=null) retString+="&extract="+extract; - if(sentiment!=null) retString+="&sentiment="+(sentiment?"1":"0"); - if(showSourceText!=null) retString+="&showSourceText="+(showSourceText?"1":"0"); - if(maxRetrieve!=null) retString+="&maxRetrieve="+maxRetrieve.toString(); - if(baseUrl!=null) retString+="&baseUrl="+URLEncoder.encode(baseUrl,"UTF-8"); - if(disambiguate!=null) retString+="&disambiguate="+(disambiguate ? "1":"0"); - if(linkedData!=null) retString+="&linkedData="+(linkedData?"1":"0"); - if(coreference!=null) retString+="&coreference="+(coreference?"1":"0"); - if(quotations!=null) retString+=""ations="+(quotations?"1":"0"); - if(sourceText!=null) retString+="&sourceText="+sourceText; - if(cQuery!=null) retString+="&cquery="+URLEncoder.encode(cQuery,"UTF-8"); - if(xPath!=null) retString+="&xpath="+URLEncoder.encode(xPath,"UTF-8"); - } - catch(UnsupportedEncodingException e ){ - retString = ""; - } - return retString; - } - } - - - diff --git a/src/main/java/com/alchemyapi/api/AlchemyAPI_ConceptParams.java b/src/main/java/com/alchemyapi/api/AlchemyAPI_ConceptParams.java deleted file mode 100644 index c65f54d..0000000 --- a/src/main/java/com/alchemyapi/api/AlchemyAPI_ConceptParams.java +++ /dev/null @@ -1,96 +0,0 @@ -package com.alchemyapi.api; -import java.net.URLEncoder; -import java.io.UnsupportedEncodingException; - - - - public class AlchemyAPI_ConceptParams extends AlchemyAPI_Params{ - public static final String CLEANED_OR_RAW = "cleaned_or_raw"; - public static final String CLEANED = "cleaned"; - public static final String RAW = "raw"; - public static final String CQUERY = "cquery"; - public static final String XPATH = "xpath"; - - private Integer maxRetrieve; - private String sourceText; - private Boolean showSourceText; - private String cQuery; - private String xPath; - private Boolean linkedData; - - public String getSourceText() { - return sourceText; - } - - public void setSourceText(String sourceText) { - if( !sourceText.equals(AlchemyAPI_ConceptParams.CLEANED) && !sourceText.equals(AlchemyAPI_ConceptParams.CLEANED_OR_RAW) - && !sourceText.equals(AlchemyAPI_ConceptParams.RAW) && !sourceText.equals(AlchemyAPI_ConceptParams.CQUERY) - && !sourceText.equals(AlchemyAPI_ConceptParams.XPATH)) - { - throw new RuntimeException("Invalid setting " + sourceText + " for parameter sourceText"); - } - this.sourceText = sourceText; - } - - public boolean isShowSourceText() { - return showSourceText; - } - - public void setShowSourceText(boolean showSourceText) { - this.showSourceText = showSourceText; - } - - public String getCQuery() { - return cQuery; - } - - public void setCQuery(String cQuery) { - this.cQuery = cQuery; - } - - public String getXPath() { - return xPath; - } - - public void setXPath(String xPath) { - this.xPath = xPath; - } - - public int getMaxRetrieve() { - return maxRetrieve; - } - - public void setMaxRetrieve(int maxRetrieve) { - this.maxRetrieve = maxRetrieve; - } - - public boolean isLinkedData() { - return linkedData; - } - - public void setLinkedData(boolean linkedData) { - this.linkedData = linkedData; - } - - public String getParameterString(){ - String retString = super.getParameterString(); - try{ - if(sourceText!=null) retString+="&sourceText="+sourceText; - if(showSourceText!=null) retString+="&showSourceText="+(showSourceText?"1":"0"); - if(cQuery!=null) retString+="&cquery="+URLEncoder.encode(cQuery,"UTF-8"); - if(xPath!=null) retString+="&xpath="+URLEncoder.encode(xPath,"UTF-8"); - if(maxRetrieve!=null) retString+="&maxRetrieve="+maxRetrieve.toString(); - if(linkedData!=null) retString+="&linkedData="+(linkedData?"1":"0"); - } - catch(UnsupportedEncodingException e ){ - retString = ""; - } - return retString; - } - - - - } - - - diff --git a/src/main/java/com/alchemyapi/api/AlchemyAPI_ConstraintQueryParams.java b/src/main/java/com/alchemyapi/api/AlchemyAPI_ConstraintQueryParams.java deleted file mode 100644 index 490677a..0000000 --- a/src/main/java/com/alchemyapi/api/AlchemyAPI_ConstraintQueryParams.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.alchemyapi.api; -import java.net.URLEncoder; -import java.io.UnsupportedEncodingException; - - - -public class AlchemyAPI_ConstraintQueryParams extends AlchemyAPI_Params{ - - private String cQuery; - - public String getCQuery() { - return cQuery; - } - public void setCQuery(String cQuery) { - this.cQuery = cQuery; - } - - - public String getParameterString(){ - String retString = super.getParameterString(); - try{ - if(cQuery!=null) retString+="&cquery="+URLEncoder.encode(cQuery,"UTF-8"); - } - catch(UnsupportedEncodingException e ){ - retString = ""; - } - return retString; - } - - - -} - - - diff --git a/src/main/java/com/alchemyapi/api/AlchemyAPI_ImageParams.java b/src/main/java/com/alchemyapi/api/AlchemyAPI_ImageParams.java deleted file mode 100644 index 3e94bff..0000000 --- a/src/main/java/com/alchemyapi/api/AlchemyAPI_ImageParams.java +++ /dev/null @@ -1,70 +0,0 @@ -package com.alchemyapi.api; -import java.net.URLEncoder; -import java.io.UnsupportedEncodingException; - - - - public class AlchemyAPI_ImageParams extends AlchemyAPI_Params{ - public static final String CQUERY = "cquery"; - public static final String XPATH = "xpath"; - public static final String RAW = "raw"; - public static final String NOT_RAW = "not-raw"; - private String cQuery; - private String xPath; - private Integer maxRetrieve; - private byte[] image; - private String imagePostMode; - private String baseUrl; - - public String getCQuery() { - return cQuery; - } - public void setCQuery(String cQuery) { - this.cQuery = cQuery; - } - public String getXPath() { - return xPath; - } - public void setXPath(String xPath) { - this.xPath = xPath; - } - public int getMaxRetrieve() { - return maxRetrieve; - } - public void setMaxRetrieve(int maxRetrieve) { - this.maxRetrieve = maxRetrieve; - } - public byte[] getImage() { - return image; - } - public void setImage(byte[] image) { - this.image = image; - } - public String getImagePostMode() { - return imagePostMode; - } - public void setImagePostMode(String imagePostMode) { - this.imagePostMode = imagePostMode; - } - public String getBaseUrl() { - return baseUrl; - } - public void setBaseUrl(String baseUrl) { - this.baseUrl = baseUrl; - } - - public String getParameterString(){ - String retString = super.getParameterString(); - try{ - if(cQuery!=null) retString+="&cquery="+URLEncoder.encode(cQuery,"UTF-8"); - if(xPath!=null) retString+="&xpath="+URLEncoder.encode(xPath,"UTF-8"); - if(maxRetrieve!=null) retString+="&maxRetrieve="+maxRetrieve.toString(); - if(imagePostMode!=null) retString+="&imagePostMode="+URLEncoder.encode(imagePostMode,"UTF-8"); - if(baseUrl!=null) retString+="&baseUrl="+URLEncoder.encode(baseUrl,"UTF-8"); - } - catch(UnsupportedEncodingException e ){ - retString = ""; - } - return retString; - } - } diff --git a/src/main/java/com/alchemyapi/api/AlchemyAPI_KeywordParams.java b/src/main/java/com/alchemyapi/api/AlchemyAPI_KeywordParams.java deleted file mode 100644 index 28402b0..0000000 --- a/src/main/java/com/alchemyapi/api/AlchemyAPI_KeywordParams.java +++ /dev/null @@ -1,122 +0,0 @@ -package com.alchemyapi.api; -import java.net.URLEncoder; -import java.io.UnsupportedEncodingException; - - - - public class AlchemyAPI_KeywordParams extends AlchemyAPI_Params{ - public static final String CLEANED_OR_RAW = "cleaned_or_raw"; - public static final String CLEANED = "cleaned"; - public static final String RAW = "raw"; - public static final String CQUERY = "cquery"; - public static final String XPATH = "xpath"; - - public static final String EXTRACT_MODE_STRICT = "strict"; - - private Integer maxRetrieve; - private String sourceText; - private Boolean showSourceText; - private Boolean sentiment; - private String cQuery; - private String xPath; - private String baseUrl; - private String keywordExtractMode; - - public String getKeywordExtractMode() { - return keywordExtractMode; - } - - public void setKeywordExtractMode(String keywordExtractMode) { - if( !keywordExtractMode.equals(AlchemyAPI_KeywordParams.EXTRACT_MODE_STRICT)) - { - throw new RuntimeException("Invalid setting " + keywordExtractMode + " for parameter keywordExtractMode"); - } - this.keywordExtractMode = keywordExtractMode; - } - - public String getSourceText() { - return sourceText; - } - - public void setSourceText(String sourceText) { - if( !sourceText.equals(AlchemyAPI_KeywordParams.CLEANED) && !sourceText.equals(AlchemyAPI_KeywordParams.CLEANED_OR_RAW) - && !sourceText.equals(AlchemyAPI_KeywordParams.RAW) && !sourceText.equals(AlchemyAPI_KeywordParams.CQUERY) - && !sourceText.equals(AlchemyAPI_KeywordParams.XPATH)) - { - throw new RuntimeException("Invalid setting " + sourceText + " for parameter sourceText"); - } - this.sourceText = sourceText; - } - - public boolean isShowSourceText() { - return showSourceText; - } - - public void setShowSourceText(boolean showSourceText) { - this.showSourceText = showSourceText; - } - - public boolean isSentiment() { - return sentiment; - } - - public void setSentiment(boolean sentiment) { - this.sentiment = sentiment; - } - - public String getCQuery() { - return cQuery; - } - - public void setCQuery(String cQuery) { - this.cQuery = cQuery; - } - - public String getXPath() { - return xPath; - } - - public void setXPath(String xPath) { - this.xPath = xPath; - } - - public int getMaxRetrieve() { - return maxRetrieve; - } - - public void setMaxRetrieve(int maxRetrieve) { - this.maxRetrieve = maxRetrieve; - } - - public String getBaseUrl() { - return baseUrl; - } - - public void setBaseUrl(String baseUrl) { - this.baseUrl = baseUrl; - } - - public String getParameterString(){ - String retString = super.getParameterString(); - try{ - if(sourceText!=null) retString+="&sourceText="+sourceText; - if(showSourceText!=null) retString+="&showSourceText="+(showSourceText?"1":"0"); - if(sentiment!=null) retString+="&sentiment="+(sentiment?"1":"0"); - if(cQuery!=null) retString+="&cquery="+URLEncoder.encode(cQuery,"UTF-8"); - if(xPath!=null) retString+="&xpath="+URLEncoder.encode(xPath,"UTF-8"); - if(maxRetrieve!=null) retString+="&maxRetrieve="+maxRetrieve.toString(); - if(baseUrl!=null) retString+="&baseUrl="+URLEncoder.encode(baseUrl,"UTF-8"); - if(keywordExtractMode!=null) retString+="&keywordExtractMode="+URLEncoder.encode(keywordExtractMode,"UTF-8"); - } - catch(UnsupportedEncodingException e ){ - retString = ""; - } - return retString; - } - - - - } - - - diff --git a/src/main/java/com/alchemyapi/api/AlchemyAPI_LanguageParams.java b/src/main/java/com/alchemyapi/api/AlchemyAPI_LanguageParams.java deleted file mode 100644 index a1528f9..0000000 --- a/src/main/java/com/alchemyapi/api/AlchemyAPI_LanguageParams.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.alchemyapi.api; - -import java.io.UnsupportedEncodingException; -import java.net.URLEncoder; - -public class AlchemyAPI_LanguageParams extends AlchemyAPI_Params { - public static final String CLEANED_OR_RAW = "cleaned_or_raw"; - public static final String CLEANED = "cleaned"; - public static final String RAW = "raw"; - public static final String CQUERY = "cquery"; - public static final String XPATH = "xpath"; - - private String sourceText; - private String cQuery; - private String xPath; - - public String getSourceText() { - return sourceText; - } - - public void setSourceText(String sourceText) { - if( !sourceText.equals(AlchemyAPI_LanguageParams.CLEANED_OR_RAW) - && !sourceText.equals(AlchemyAPI_LanguageParams.CQUERY) - && !sourceText.equals(AlchemyAPI_LanguageParams.XPATH)) - { - throw new RuntimeException("Invalid setting " + sourceText + " for parameter sourceText"); - } - this.sourceText = sourceText; - } - - public String getCQuery() { - return cQuery; - } - - public void setCQuery(String cQuery) { - this.cQuery = cQuery; - } - - public String getXPath() { - return xPath; - } - - public void setXPath(String xPath) { - this.xPath = xPath; - } - - public String getParameterString(){ - String retString = super.getParameterString(); - try{ - if(sourceText!=null) retString+="&sourceText="+sourceText; - if(cQuery!=null) retString+="&cquery="+URLEncoder.encode(cQuery,"UTF-8"); - if(xPath!=null) retString+="&xpath="+URLEncoder.encode(xPath,"UTF-8"); - } - catch(UnsupportedEncodingException e ){ - retString = ""; - } - return retString; - } - -} diff --git a/src/main/java/com/alchemyapi/api/AlchemyAPI_NamedEntityParams.java b/src/main/java/com/alchemyapi/api/AlchemyAPI_NamedEntityParams.java deleted file mode 100644 index 7598323..0000000 --- a/src/main/java/com/alchemyapi/api/AlchemyAPI_NamedEntityParams.java +++ /dev/null @@ -1,125 +0,0 @@ -package com.alchemyapi.api; -import java.net.URLEncoder; -import java.io.UnsupportedEncodingException; - - - - public class AlchemyAPI_NamedEntityParams extends AlchemyAPI_Params{ - public static final String CLEANED_OR_RAW = "cleaned_or_raw"; - public static final String CLEANED = "cleaned"; - public static final String RAW = "raw"; - public static final String CQUERY = "cquery"; - public static final String XPATH = "xpath"; - - private Boolean disambiguate; - private Boolean linkedData; - private Boolean coreference; - private Boolean quotations; - private String sourceText; - private Boolean showSourceText; - private String cQuery; - private String xPath; - private Integer maxRetrieve; - private String baseUrl; - private Boolean sentiment; - - public boolean isDisambiguate() { - return disambiguate; - } - public void setDisambiguate(boolean disambiguate) { - this.disambiguate = disambiguate; - } - public boolean isLinkedData() { - return linkedData; - } - public void setLinkedData(boolean linkedData) { - this.linkedData = linkedData; - } - public boolean isCoreference() { - return coreference; - } - public void setCoreference(boolean coreference) { - this.coreference = coreference; - } - public boolean isQuotations() { - return quotations; - } - public void setQuotations(boolean quotations) { - this.quotations = quotations; - } - public String getSourceText() { - return sourceText; - } - public void setSourceText(String sourceText) { - if( !sourceText.equals(AlchemyAPI_NamedEntityParams.CLEANED) && !sourceText.equals(AlchemyAPI_NamedEntityParams.CLEANED_OR_RAW) - && !sourceText.equals(AlchemyAPI_NamedEntityParams.RAW) && !sourceText.equals(AlchemyAPI_NamedEntityParams.CQUERY) - && !sourceText.equals(AlchemyAPI_NamedEntityParams.XPATH)) - { - throw new RuntimeException("Invalid setting " + sourceText + " for parameter sourceText"); - } - this.sourceText = sourceText; - } - public boolean isShowSourceText() { - return showSourceText; - } - public void setShowSourceText(boolean showSourceText) { - this.showSourceText = showSourceText; - } - public String getCQuery() { - return cQuery; - } - public void setCQuery(String cQuery) { - this.cQuery = cQuery; - } - public String getXPath() { - return xPath; - } - public void setXPath(String xPath) { - this.xPath = xPath; - } - public int getMaxRetrieve() { - return maxRetrieve; - } - public void setMaxRetrieve(int maxRetrieve) { - this.maxRetrieve = maxRetrieve; - } - public String getBaseUrl() { - return baseUrl; - } - public void setBaseUrl(String baseUrl) { - this.baseUrl = baseUrl; - } - public boolean isSentiment() { - return sentiment; - } - public void setSentiment(boolean sentiment) { - this.sentiment = sentiment; - } - - public String getParameterString(){ - String retString = super.getParameterString(); - try{ - if(disambiguate!=null) retString+="&disambiguate="+(disambiguate ? "1":"0"); - if(linkedData!=null) retString+="&linkedData="+(linkedData?"1":"0"); - if(coreference!=null) retString+="&coreference="+(coreference?"1":"0"); - if(quotations!=null) retString+=""ations="+(quotations?"1":"0"); - if(sourceText!=null) retString+="&sourceText="+sourceText; - if(showSourceText!=null) retString+="&showSourceText="+(showSourceText?"1":"0"); - if(cQuery!=null) retString+="&cquery="+URLEncoder.encode(cQuery,"UTF-8"); - if(xPath!=null) retString+="&xpath="+URLEncoder.encode(xPath,"UTF-8"); - if(maxRetrieve!=null) retString+="&maxRetrieve="+maxRetrieve.toString(); - if(baseUrl!=null) retString+="&baseUrl="+URLEncoder.encode(baseUrl,"UTF-8"); - if(sentiment!=null) retString+="&sentiment="+(sentiment?"1":"0"); - } - catch(UnsupportedEncodingException e ){ - retString = ""; - } - return retString; - } - - - - } - - - diff --git a/src/main/java/com/alchemyapi/api/AlchemyAPI_Params.java b/src/main/java/com/alchemyapi/api/AlchemyAPI_Params.java deleted file mode 100644 index 3e1f8d3..0000000 --- a/src/main/java/com/alchemyapi/api/AlchemyAPI_Params.java +++ /dev/null @@ -1,80 +0,0 @@ -package com.alchemyapi.api; - -import java.io.UnsupportedEncodingException; -import java.net.URLEncoder; - - - -public class AlchemyAPI_Params { - public static final String OUTPUT_XML = "xml"; - public static final String OUTPUT_RDF = "rdf"; - - private String url; - private String html; - private String text; - private String outputMode = OUTPUT_XML; - private String customParameters; - - public String getUrl() { - return url; - } - public void setUrl(String url) { - this.url = url; - } - public String getHtml() { - return html; - } - public void setHtml(String html) { - this.html = html; - } - public String getText() { - return text; - } - public void setText(String text) { - this.text = text; - } - public String getOutputMode() { - return outputMode; - } - public void setOutputMode(String outputMode) { - if( !outputMode.equals(AlchemyAPI_Params.OUTPUT_XML) && !outputMode.equals(OUTPUT_RDF) ) - { - throw new RuntimeException("Invalid setting " + outputMode + " for parameter outputMode"); - } - this.outputMode = outputMode; - } - public String getCustomParameters() { - return customParameters; - } - - public void setCustomParameters(String... customParameters) { - StringBuilder data = new StringBuilder(); - try{ - for (int i = 0; i < customParameters.length; ++i) { - data.append('&').append(customParameters[i]); - if (++i < customParameters.length) - data.append('=').append(URLEncoder.encode(customParameters[i], "UTF8")); - } - } - catch(UnsupportedEncodingException e){ - this.customParameters = ""; - return; - } - this.customParameters = data.toString(); - } - - public String getParameterString(){ - String retString = ""; - try{ - if(url!=null) retString+="&url="+URLEncoder.encode(url,"UTF-8"); - if(html!=null) retString+="&html="+URLEncoder.encode(html,"UTF-8"); - if(text!=null) retString+="&text="+URLEncoder.encode(text,"UTF-8"); - if(customParameters!=null) retString+=customParameters; - if(outputMode!=null) retString+="&outputMode="+outputMode; - } - catch(UnsupportedEncodingException e ){ - retString = ""; - } - return retString; - } -} diff --git a/src/main/java/com/alchemyapi/api/AlchemyAPI_RelationParams.java b/src/main/java/com/alchemyapi/api/AlchemyAPI_RelationParams.java deleted file mode 100644 index 15e99fe..0000000 --- a/src/main/java/com/alchemyapi/api/AlchemyAPI_RelationParams.java +++ /dev/null @@ -1,140 +0,0 @@ -package com.alchemyapi.api; -import java.net.URLEncoder; -import java.io.UnsupportedEncodingException; - - - - public class AlchemyAPI_RelationParams extends AlchemyAPI_Params{ - public static final String CLEANED_OR_RAW = "cleaned_or_raw"; - public static final String CLEANED = "cleaned"; - public static final String RAW = "raw"; - public static final String CQUERY = "cquery"; - public static final String XPATH = "xpath"; - - private Boolean disambiguate; - private Boolean linkedData; - private Boolean coreference; - private String sourceText; - private Boolean showSourceText; - private Boolean entities; - private Boolean sentimentExcludeEntities; - private Boolean requireEntities; - private String cQuery; - private String xPath; - private Integer maxRetrieve; - private String baseUrl; - private Boolean sentiment; - - public boolean isDisambiguate() { - return disambiguate; - } - public void setDisambiguate(boolean disambiguate) { - this.disambiguate = disambiguate; - } - public boolean isLinkedData() { - return linkedData; - } - public void setLinkedData(boolean linkedData) { - this.linkedData = linkedData; - } - public boolean isCoreference() { - return coreference; - } - public void setCoreference(boolean coreference) { - this.coreference = coreference; - } - - public String getSourceText() { - return sourceText; - } - public void setSourceText(String sourceText) { - if( !sourceText.equals(AlchemyAPI_NamedEntityParams.CLEANED) && !sourceText.equals(AlchemyAPI_NamedEntityParams.CLEANED_OR_RAW) - && !sourceText.equals(AlchemyAPI_NamedEntityParams.RAW) && !sourceText.equals(AlchemyAPI_NamedEntityParams.CQUERY) - && !sourceText.equals(AlchemyAPI_NamedEntityParams.XPATH)) - { - throw new RuntimeException("Invalid setting " + sourceText + " for parameter sourceText"); - } - this.sourceText = sourceText; - } - public boolean isShowSourceText() { - return showSourceText; - } - public void setShowSourceText(boolean showSourceText) { - this.showSourceText = showSourceText; - } - public boolean isEntities() { - return entities; - } - public void setEntities(boolean entities) { - this.entities = entities; - } - public boolean isSentimentExcludeEntities() { - return sentimentExcludeEntities; - } - public void setSentimentExcludeEntities(boolean sentimentExcludeEntities) { - this.sentimentExcludeEntities = sentimentExcludeEntities; - } - public boolean isRequireEntities() { - return requireEntities; - } - public void setRequireEntities(boolean requireEntities) { - this.requireEntities = requireEntities; - } - public String getCQuery() { - return cQuery; - } - public void setCQuery(String cQuery) { - this.cQuery = cQuery; - } - public String getXPath() { - return xPath; - } - public void setXPath(String xPath) { - this.xPath = xPath; - } - public int getMaxRetrieve() { - return maxRetrieve; - } - public void setMaxRetrieve(int maxRetrieve) { - this.maxRetrieve = maxRetrieve; - } - public String getBaseUrl() { - return baseUrl; - } - public void setBaseUrl(String baseUrl) { - this.baseUrl = baseUrl; - } - public boolean isSentiment() { - return sentiment; - } - public void setSentiment(boolean sentiment) { - this.sentiment = sentiment; - } - - public String getParameterString(){ - String retString = super.getParameterString(); - try{ - if(disambiguate!=null) retString+="&disambiguate="+(disambiguate ? "1":"0"); - if(linkedData!=null) retString+="&linkedData="+(linkedData?"1":"0"); - if(coreference!=null) retString+="&coreference="+(coreference?"1":"0"); - if(sourceText!=null) retString+="&sourceText="+sourceText; - if(showSourceText!=null) retString+="&showSourceText="+(showSourceText?"1":"0"); - if(entities!=null) retString+="&entities="+(entities?"1":"0"); - if(sentimentExcludeEntities!=null) retString+="&sentimentExcludeEntities="+(sentimentExcludeEntities?"1":"0"); - if(requireEntities!=null) retString+="&requireEntities="+(requireEntities?"1":"0"); - if(cQuery!=null) retString+="&cquery="+URLEncoder.encode(cQuery,"UTF-8"); - if(xPath!=null) retString+="&xpath="+URLEncoder.encode(xPath,"UTF-8"); - if(maxRetrieve!=null) retString+="&maxRetrieve="+maxRetrieve.toString(); - if(baseUrl!=null) retString+="&baseUrl="+URLEncoder.encode(baseUrl,"UTF-8"); - if(sentiment!=null) retString+="&sentiment="+(sentiment?"1":"0"); - } - catch(UnsupportedEncodingException e ){ - retString = ""; - } - return retString; - } - - } - - - diff --git a/src/main/java/com/alchemyapi/api/AlchemyAPI_TargetedSentimentParams.java b/src/main/java/com/alchemyapi/api/AlchemyAPI_TargetedSentimentParams.java deleted file mode 100644 index dfb0879..0000000 --- a/src/main/java/com/alchemyapi/api/AlchemyAPI_TargetedSentimentParams.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.alchemyapi.api; - -import java.io.UnsupportedEncodingException; -import java.net.URLEncoder; - -public class AlchemyAPI_TargetedSentimentParams extends AlchemyAPI_Params { - - private Boolean showSourceText; - private String target; - - public boolean isShowSourceText() { - return showSourceText; - } - - public void setShowSourceText(boolean showSourceText) { - this.showSourceText = showSourceText; - } - - public String getTarget(){ - return target; - } - - public void setTarget(String target) { - this.target = target; - } - - public String getParameterString(){ - String retString = super.getParameterString(); - try{ - if(showSourceText!=null) retString+="&showSourceText="+(showSourceText?"1":"0"); - if(target!=null) retString+="&target="+URLEncoder.encode(target, "UTF-8"); - - } - catch(UnsupportedEncodingException e ){ - retString = ""; - } - - return retString; - } -} diff --git a/src/main/java/com/alchemyapi/api/AlchemyAPI_TaxonomyParams.java b/src/main/java/com/alchemyapi/api/AlchemyAPI_TaxonomyParams.java deleted file mode 100644 index 380da64..0000000 --- a/src/main/java/com/alchemyapi/api/AlchemyAPI_TaxonomyParams.java +++ /dev/null @@ -1,119 +0,0 @@ -package com.alchemyapi.api; -import java.net.URLEncoder; -import java.io.UnsupportedEncodingException; - - - - public class AlchemyAPI_TaxonomyParams extends AlchemyAPI_Params{ - public static final String CLEANED_OR_RAW = "cleaned_or_raw"; - public static final String CLEANED = "cleaned"; - public static final String RAW = "raw"; - public static final String CQUERY = "cquery"; - public static final String XPATH = "xpath"; - - private Boolean disambiguate; - private Boolean linkedData; - private Boolean coreference; - private Boolean quotations; - private String sourceText; - private Boolean showSourceText; - private String cQuery; - private String xPath; - private Integer maxRetrieve; - private String baseUrl; - private Boolean sentiment; - - public boolean isDisambiguate() { - return disambiguate; - } - public void setDisambiguate(boolean disambiguate) { - this.disambiguate = disambiguate; - } - public boolean isLinkedData() { - return linkedData; - } - public void setLinkedData(boolean linkedData) { - this.linkedData = linkedData; - } - public boolean isCoreference() { - return coreference; - } - public void setCoreference(boolean coreference) { - this.coreference = coreference; - } - public boolean isQuotations() { - return quotations; - } - public void setQuotations(boolean quotations) { - this.quotations = quotations; - } - public String getSourceText() { - return sourceText; - } - public void setSourceText(String sourceText) { - if( !sourceText.equals(AlchemyAPI_TaxonomyParams.CLEANED) && !sourceText.equals(AlchemyAPI_TaxonomyParams.CLEANED_OR_RAW) - && !sourceText.equals(AlchemyAPI_TaxonomyParams.RAW) && !sourceText.equals(AlchemyAPI_TaxonomyParams.CQUERY) - && !sourceText.equals(AlchemyAPI_TaxonomyParams.XPATH)) - { - throw new RuntimeException("Invalid setting " + sourceText + " for parameter sourceText"); - } - this.sourceText = sourceText; - } - public boolean isShowSourceText() { - return showSourceText; - } - public void setShowSourceText(boolean showSourceText) { - this.showSourceText = showSourceText; - } - public String getCQuery() { - return cQuery; - } - public void setCQuery(String cQuery) { - this.cQuery = cQuery; - } - public String getXPath() { - return xPath; - } - public void setXPath(String xPath) { - this.xPath = xPath; - } - public int getMaxRetrieve() { - return maxRetrieve; - } - public void setMaxRetrieve(int maxRetrieve) { - this.maxRetrieve = maxRetrieve; - } - public String getBaseUrl() { - return baseUrl; - } - public void setBaseUrl(String baseUrl) { - this.baseUrl = baseUrl; - } - public boolean isSentiment() { - return sentiment; - } - public void setSentiment(boolean sentiment) { - this.sentiment = sentiment; - } - - public String getParameterString(){ - String retString = super.getParameterString(); - try{ - if(disambiguate!=null) retString+="&disambiguate="+(disambiguate ? "1":"0"); - if(linkedData!=null) retString+="&linkedData="+(linkedData?"1":"0"); - if(coreference!=null) retString+="&coreference="+(coreference?"1":"0"); - if(quotations!=null) retString+=""ations="+(quotations?"1":"0"); - if(sourceText!=null) retString+="&sourceText="+sourceText; - if(showSourceText!=null) retString+="&showSourceText="+(showSourceText?"1":"0"); - if(cQuery!=null) retString+="&cquery="+URLEncoder.encode(cQuery,"UTF-8"); - if(xPath!=null) retString+="&xpath="+URLEncoder.encode(xPath,"UTF-8"); - if(maxRetrieve!=null) retString+="&maxRetrieve="+maxRetrieve.toString(); - if(baseUrl!=null) retString+="&baseUrl="+URLEncoder.encode(baseUrl,"UTF-8"); - if(sentiment!=null) retString+="&sentiment="+(sentiment?"1":"0"); - } - catch(UnsupportedEncodingException e ){ - retString = ""; - } - return retString; - } - } diff --git a/src/main/java/com/alchemyapi/api/AlchemyAPI_TextParams.java b/src/main/java/com/alchemyapi/api/AlchemyAPI_TextParams.java deleted file mode 100644 index fec1a3e..0000000 --- a/src/main/java/com/alchemyapi/api/AlchemyAPI_TextParams.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.alchemyapi.api; - - -public class AlchemyAPI_TextParams extends AlchemyAPI_Params{ - private Boolean useMetaData; - private Boolean extractLinks; - - public boolean isUseMetaData() { - return useMetaData; - } - - public void setUseMetaData(boolean useMetaData) { - this.useMetaData = useMetaData; - } - - public boolean isExtractLinks() { - return extractLinks; - } - - public void setExtractLinks(boolean extractLinks) { - this.extractLinks = extractLinks; - } - - public String getParameterString(){ - String retString = super.getParameterString(); - - if(useMetaData!=null) retString+="&useMetaData="+(useMetaData?"1":"0"); - if(extractLinks!=null) retString+="&extractLinks="+(extractLinks?"1":"0"); - - return retString; - } -} diff --git a/src/main/java/com/alchemyapi/api/parameters/CategoryParameters.java b/src/main/java/com/alchemyapi/api/parameters/CategoryParameters.java new file mode 100644 index 0000000..4051e79 --- /dev/null +++ b/src/main/java/com/alchemyapi/api/parameters/CategoryParameters.java @@ -0,0 +1,66 @@ +package com.alchemyapi.api.parameters; + +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; + +public class CategoryParameters extends Parameters { + public static final String CLEANED_OR_RAW = "cleaned_or_raw"; + public static final String CQUERY = "cquery"; + public static final String XPATH = "xpath"; + + private String sourceText; + private String cQuery; + private String xPath; + private String baseUrl; + + public String getSourceText() { + return sourceText; + } + + public void setSourceText(String sourceText) { + if (!sourceText.equals(CategoryParameters.CLEANED_OR_RAW) + && !sourceText.equals(CategoryParameters.CQUERY) + && !sourceText.equals(CategoryParameters.XPATH)) { + throw new RuntimeException("Invalid setting " + sourceText + " for parameter sourceText"); + } + this.sourceText = sourceText; + } + + public String getCQuery() { + return cQuery; + } + + public void setCQuery(String cQuery) { + this.cQuery = cQuery; + } + + public String getXPath() { + return xPath; + } + + public void setXPath(String xPath) { + this.xPath = xPath; + } + + public String getBaseUrl() { + return baseUrl; + } + + public void setBaseUrl(String baseUrl) { + this.baseUrl = baseUrl; + } + + public String getParameterString() { + String retString = super.getParameterString(); + try { + if (sourceText != null) retString += "&sourceText=" + sourceText; + if (cQuery != null) retString += "&cquery=" + URLEncoder.encode(cQuery, "UTF-8"); + if (xPath != null) retString += "&xpath=" + URLEncoder.encode(xPath, "UTF-8"); + if (baseUrl != null) retString += "&baseUrl=" + URLEncoder.encode(baseUrl, "UTF-8"); + } catch (UnsupportedEncodingException e) { + retString = ""; + } + return retString; + } + +} diff --git a/src/main/java/com/alchemyapi/api/parameters/CombinedParameters.java b/src/main/java/com/alchemyapi/api/parameters/CombinedParameters.java new file mode 100644 index 0000000..edc1db2 --- /dev/null +++ b/src/main/java/com/alchemyapi/api/parameters/CombinedParameters.java @@ -0,0 +1,187 @@ +package com.alchemyapi.api.parameters; + +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; + + +public class CombinedParameters extends Parameters { + public static final String CLEANED_OR_RAW = "cleaned_or_raw"; + public static final String CLEANED = "cleaned"; + public static final String RAW = "raw"; + public static final String CQUERY = "cquery"; + public static final String XPATH = "xpath"; + public static final String EXTRACT_MODE_TRUST = "trust-metadata"; + public static final String EXTRACT_MODE_INFER = "always-infer"; + public static final String EXTRACT_PAGE_IMAGE = "page-image"; + public static final String EXTRACT_ENTITY = "entity"; + public static final String EXTRACT_KEYWORD = "keyword"; + public static final String EXTRACT_TITLE = "title"; + public static final String EXTRACT_AUTHOR = "author"; + public static final String EXTRACT_TAXONOMY = "taxonomy"; + public static final String EXTRACT_CONCEPT = "concept"; + public static final String EXTRACT_RELATION = "relation"; + public static final String EXTRACT_DOC_SENTIMENT = "doc-sentiment"; + + private Boolean disambiguate; + private Boolean linkedData; + private Boolean coreference; + private Boolean quotations; + private String sourceText; + private Boolean showSourceText; + private String cQuery; + private String xPath; + private Integer maxRetrieve; + private String baseUrl; + private Boolean sentiment = false; + private String extractMode; + private String extract; + + public boolean isDisambiguate() { + return disambiguate; + } + + public void setDisambiguate(boolean disambiguate) { + this.disambiguate = disambiguate; + } + + public boolean isLinkedData() { + return linkedData; + } + + public void setLinkedData(boolean linkedData) { + this.linkedData = linkedData; + } + + public boolean isCoreference() { + return coreference; + } + + public void setCoreference(boolean coreference) { + this.coreference = coreference; + } + + public boolean isQuotations() { + return quotations; + } + + public void setQuotations(boolean quotations) { + this.quotations = quotations; + } + + public String getSourceText() { + return sourceText; + } + + public void setSourceText(String sourceText) { + if (!sourceText.equals(CombinedParameters.CLEANED) && !sourceText.equals(CombinedParameters.CLEANED_OR_RAW) + && !sourceText.equals(CombinedParameters.RAW) && !sourceText.equals(CombinedParameters.CQUERY) + && !sourceText.equals(CombinedParameters.XPATH)) { + throw new RuntimeException("Invalid setting " + sourceText + " for parameter sourceText"); + } + this.sourceText = sourceText; + } + + public boolean isShowSourceText() { + return showSourceText; + } + + public void setShowSourceText(boolean showSourceText) { + this.showSourceText = showSourceText; + } + + public String getCQuery() { + return cQuery; + } + + public void setCQuery(String cQuery) { + this.cQuery = cQuery; + } + + public String getXPath() { + return xPath; + } + + public void setXPath(String xPath) { + this.xPath = xPath; + } + + public int getMaxRetrieve() { + return maxRetrieve; + } + + public void setMaxRetrieve(int maxRetrieve) { + this.maxRetrieve = maxRetrieve; + } + + public String getBaseUrl() { + return baseUrl; + } + + public void setBaseUrl(String baseUrl) { + this.baseUrl = baseUrl; + } + + public boolean isSentiment() { + return sentiment; + } + + public void setSentiment(boolean sentiment) { + this.sentiment = sentiment; + } + + public void setExtractMode(String extractMode) { + if (!extractMode.equals(EXTRACT_MODE_TRUST) && !extractMode.equals(EXTRACT_MODE_INFER)) { + throw new RuntimeException("Invalid setting " + extractMode + " for parameter extractMode"); + } + this.extractMode = extractMode; + } + + public void setExtractAll() { + this.extract = EXTRACT_PAGE_IMAGE + EXTRACT_ENTITY + EXTRACT_KEYWORD + + EXTRACT_TITLE + EXTRACT_AUTHOR + EXTRACT_TAXONOMY + + EXTRACT_CONCEPT + EXTRACT_RELATION + EXTRACT_DOC_SENTIMENT; + } + + public void setExtract(String extractArg) { + if (!extractArg.equals(EXTRACT_PAGE_IMAGE) + && !extractArg.equals(EXTRACT_ENTITY) + && !extractArg.equals(EXTRACT_KEYWORD) + && !extractArg.equals(EXTRACT_TITLE) + && !extractArg.equals(EXTRACT_AUTHOR) + && !extractArg.equals(EXTRACT_TAXONOMY) + && !extractArg.equals(EXTRACT_CONCEPT) + && !extractArg.equals(EXTRACT_RELATION) + && !extractArg.equals(EXTRACT_DOC_SENTIMENT)) { + throw new RuntimeException("Invalid setting " + extractArg + " for parameter extract"); + } + if ((null == this.extract) || (0 == this.extract.length())) + this.extract = extractArg; + else + this.extract += "," + extractArg; + } + + public String getParameterString() { + String retString = super.getParameterString(); + try { + if (extractMode != null) retString += "&extractMode=" + extractMode; + if (extract != null) retString += "&extract=" + extract; + if (sentiment != null) retString += "&sentiment=" + (sentiment ? "1" : "0"); + if (showSourceText != null) retString += "&showSourceText=" + (showSourceText ? "1" : "0"); + if (maxRetrieve != null) retString += "&maxRetrieve=" + maxRetrieve.toString(); + if (baseUrl != null) retString += "&baseUrl=" + URLEncoder.encode(baseUrl, "UTF-8"); + if (disambiguate != null) retString += "&disambiguate=" + (disambiguate ? "1" : "0"); + if (linkedData != null) retString += "&linkedData=" + (linkedData ? "1" : "0"); + if (coreference != null) retString += "&coreference=" + (coreference ? "1" : "0"); + if (quotations != null) retString += ""ations=" + (quotations ? "1" : "0"); + if (sourceText != null) retString += "&sourceText=" + sourceText; + if (cQuery != null) retString += "&cquery=" + URLEncoder.encode(cQuery, "UTF-8"); + if (xPath != null) retString += "&xpath=" + URLEncoder.encode(xPath, "UTF-8"); + } catch (UnsupportedEncodingException e) { + retString = ""; + } + return retString; + } +} + + + diff --git a/src/main/java/com/alchemyapi/api/parameters/ConceptParameters.java b/src/main/java/com/alchemyapi/api/parameters/ConceptParameters.java new file mode 100644 index 0000000..0de4f44 --- /dev/null +++ b/src/main/java/com/alchemyapi/api/parameters/ConceptParameters.java @@ -0,0 +1,93 @@ +package com.alchemyapi.api.parameters; + +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; + + +public class ConceptParameters extends Parameters { + public static final String CLEANED_OR_RAW = "cleaned_or_raw"; + public static final String CLEANED = "cleaned"; + public static final String RAW = "raw"; + public static final String CQUERY = "cquery"; + public static final String XPATH = "xpath"; + + private Integer maxRetrieve; + private String sourceText; + private Boolean showSourceText; + private String cQuery; + private String xPath; + private Boolean linkedData; + + public String getSourceText() { + return sourceText; + } + + public void setSourceText(String sourceText) { + if (!sourceText.equals(ConceptParameters.CLEANED) && !sourceText.equals(ConceptParameters.CLEANED_OR_RAW) + && !sourceText.equals(ConceptParameters.RAW) && !sourceText.equals(ConceptParameters.CQUERY) + && !sourceText.equals(ConceptParameters.XPATH)) { + throw new RuntimeException("Invalid setting " + sourceText + " for parameter sourceText"); + } + this.sourceText = sourceText; + } + + public boolean isShowSourceText() { + return showSourceText; + } + + public void setShowSourceText(boolean showSourceText) { + this.showSourceText = showSourceText; + } + + public String getCQuery() { + return cQuery; + } + + public void setCQuery(String cQuery) { + this.cQuery = cQuery; + } + + public String getXPath() { + return xPath; + } + + public void setXPath(String xPath) { + this.xPath = xPath; + } + + public int getMaxRetrieve() { + return maxRetrieve; + } + + public void setMaxRetrieve(int maxRetrieve) { + this.maxRetrieve = maxRetrieve; + } + + public boolean isLinkedData() { + return linkedData; + } + + public void setLinkedData(boolean linkedData) { + this.linkedData = linkedData; + } + + public String getParameterString() { + String retString = super.getParameterString(); + try { + if (sourceText != null) retString += "&sourceText=" + sourceText; + if (showSourceText != null) retString += "&showSourceText=" + (showSourceText ? "1" : "0"); + if (cQuery != null) retString += "&cquery=" + URLEncoder.encode(cQuery, "UTF-8"); + if (xPath != null) retString += "&xpath=" + URLEncoder.encode(xPath, "UTF-8"); + if (maxRetrieve != null) retString += "&maxRetrieve=" + maxRetrieve.toString(); + if (linkedData != null) retString += "&linkedData=" + (linkedData ? "1" : "0"); + } catch (UnsupportedEncodingException e) { + retString = ""; + } + return retString; + } + + +} + + + diff --git a/src/main/java/com/alchemyapi/api/parameters/ConstraintQueryParameters.java b/src/main/java/com/alchemyapi/api/parameters/ConstraintQueryParameters.java new file mode 100644 index 0000000..20442ad --- /dev/null +++ b/src/main/java/com/alchemyapi/api/parameters/ConstraintQueryParameters.java @@ -0,0 +1,34 @@ +package com.alchemyapi.api.parameters; + +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; + + +public class ConstraintQueryParameters extends Parameters { + + private String cQuery; + + public String getCQuery() { + return cQuery; + } + + public void setCQuery(String cQuery) { + this.cQuery = cQuery; + } + + + public String getParameterString() { + String retString = super.getParameterString(); + try { + if (cQuery != null) retString += "&cquery=" + URLEncoder.encode(cQuery, "UTF-8"); + } catch (UnsupportedEncodingException e) { + retString = ""; + } + return retString; + } + + +} + + + diff --git a/src/main/java/com/alchemyapi/api/parameters/ImageParameters.java b/src/main/java/com/alchemyapi/api/parameters/ImageParameters.java new file mode 100644 index 0000000..b0dfdab --- /dev/null +++ b/src/main/java/com/alchemyapi/api/parameters/ImageParameters.java @@ -0,0 +1,80 @@ +package com.alchemyapi.api.parameters; + +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; + + +public class ImageParameters extends Parameters { + public static final String CQUERY = "cquery"; + public static final String XPATH = "xpath"; + public static final String RAW = "raw"; + public static final String NOT_RAW = "not-raw"; + private String cQuery; + private String xPath; + private Integer maxRetrieve; + private byte[] image; + private String imagePostMode; + private String baseUrl; + + public String getCQuery() { + return cQuery; + } + + public void setCQuery(String cQuery) { + this.cQuery = cQuery; + } + + public String getXPath() { + return xPath; + } + + public void setXPath(String xPath) { + this.xPath = xPath; + } + + public int getMaxRetrieve() { + return maxRetrieve; + } + + public void setMaxRetrieve(int maxRetrieve) { + this.maxRetrieve = maxRetrieve; + } + + public byte[] getImage() { + return image; + } + + public void setImage(byte[] image) { + this.image = image; + } + + public String getImagePostMode() { + return imagePostMode; + } + + public void setImagePostMode(String imagePostMode) { + this.imagePostMode = imagePostMode; + } + + public String getBaseUrl() { + return baseUrl; + } + + public void setBaseUrl(String baseUrl) { + this.baseUrl = baseUrl; + } + + public String getParameterString() { + String retString = super.getParameterString(); + try { + if (cQuery != null) retString += "&cquery=" + URLEncoder.encode(cQuery, "UTF-8"); + if (xPath != null) retString += "&xpath=" + URLEncoder.encode(xPath, "UTF-8"); + if (maxRetrieve != null) retString += "&maxRetrieve=" + maxRetrieve.toString(); + if (imagePostMode != null) retString += "&imagePostMode=" + URLEncoder.encode(imagePostMode, "UTF-8"); + if (baseUrl != null) retString += "&baseUrl=" + URLEncoder.encode(baseUrl, "UTF-8"); + } catch (UnsupportedEncodingException e) { + retString = ""; + } + return retString; + } +} diff --git a/src/main/java/com/alchemyapi/api/parameters/KeywordParameters.java b/src/main/java/com/alchemyapi/api/parameters/KeywordParameters.java new file mode 100644 index 0000000..5ffe604 --- /dev/null +++ b/src/main/java/com/alchemyapi/api/parameters/KeywordParameters.java @@ -0,0 +1,119 @@ +package com.alchemyapi.api.parameters; + +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; + + +public class KeywordParameters extends Parameters { + public static final String CLEANED_OR_RAW = "cleaned_or_raw"; + public static final String CLEANED = "cleaned"; + public static final String RAW = "raw"; + public static final String CQUERY = "cquery"; + public static final String XPATH = "xpath"; + + public static final String EXTRACT_MODE_STRICT = "strict"; + + private Integer maxRetrieve; + private String sourceText; + private Boolean showSourceText; + private Boolean sentiment; + private String cQuery; + private String xPath; + private String baseUrl; + private String keywordExtractMode; + + public String getKeywordExtractMode() { + return keywordExtractMode; + } + + public void setKeywordExtractMode(String keywordExtractMode) { + if (!keywordExtractMode.equals(KeywordParameters.EXTRACT_MODE_STRICT)) { + throw new RuntimeException("Invalid setting " + keywordExtractMode + " for parameter keywordExtractMode"); + } + this.keywordExtractMode = keywordExtractMode; + } + + public String getSourceText() { + return sourceText; + } + + public void setSourceText(String sourceText) { + if (!sourceText.equals(KeywordParameters.CLEANED) && !sourceText.equals(KeywordParameters.CLEANED_OR_RAW) + && !sourceText.equals(KeywordParameters.RAW) && !sourceText.equals(KeywordParameters.CQUERY) + && !sourceText.equals(KeywordParameters.XPATH)) { + throw new RuntimeException("Invalid setting " + sourceText + " for parameter sourceText"); + } + this.sourceText = sourceText; + } + + public boolean isShowSourceText() { + return showSourceText; + } + + public void setShowSourceText(boolean showSourceText) { + this.showSourceText = showSourceText; + } + + public boolean isSentiment() { + return sentiment; + } + + public void setSentiment(boolean sentiment) { + this.sentiment = sentiment; + } + + public String getCQuery() { + return cQuery; + } + + public void setCQuery(String cQuery) { + this.cQuery = cQuery; + } + + public String getXPath() { + return xPath; + } + + public void setXPath(String xPath) { + this.xPath = xPath; + } + + public int getMaxRetrieve() { + return maxRetrieve; + } + + public void setMaxRetrieve(int maxRetrieve) { + this.maxRetrieve = maxRetrieve; + } + + public String getBaseUrl() { + return baseUrl; + } + + public void setBaseUrl(String baseUrl) { + this.baseUrl = baseUrl; + } + + public String getParameterString() { + String retString = super.getParameterString(); + try { + if (sourceText != null) retString += "&sourceText=" + sourceText; + if (showSourceText != null) retString += "&showSourceText=" + (showSourceText ? "1" : "0"); + if (sentiment != null) retString += "&sentiment=" + (sentiment ? "1" : "0"); + if (cQuery != null) retString += "&cquery=" + URLEncoder.encode(cQuery, "UTF-8"); + if (xPath != null) retString += "&xpath=" + URLEncoder.encode(xPath, "UTF-8"); + if (maxRetrieve != null) retString += "&maxRetrieve=" + maxRetrieve.toString(); + if (baseUrl != null) retString += "&baseUrl=" + URLEncoder.encode(baseUrl, "UTF-8"); + if (keywordExtractMode != null) + retString += "&keywordExtractMode=" + URLEncoder.encode(keywordExtractMode, "UTF-8"); + } catch (UnsupportedEncodingException e) { + retString = ""; + } + return retString; + } + + +} + + + diff --git a/src/main/java/com/alchemyapi/api/parameters/LanguageParameters.java b/src/main/java/com/alchemyapi/api/parameters/LanguageParameters.java new file mode 100644 index 0000000..d947d48 --- /dev/null +++ b/src/main/java/com/alchemyapi/api/parameters/LanguageParameters.java @@ -0,0 +1,58 @@ +package com.alchemyapi.api.parameters; + +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; + +public class LanguageParameters extends Parameters { + public static final String CLEANED_OR_RAW = "cleaned_or_raw"; + public static final String CLEANED = "cleaned"; + public static final String RAW = "raw"; + public static final String CQUERY = "cquery"; + public static final String XPATH = "xpath"; + + private String sourceText; + private String cQuery; + private String xPath; + + public String getSourceText() { + return sourceText; + } + + public void setSourceText(String sourceText) { + if (!sourceText.equals(LanguageParameters.CLEANED_OR_RAW) + && !sourceText.equals(LanguageParameters.CQUERY) + && !sourceText.equals(LanguageParameters.XPATH)) { + throw new RuntimeException("Invalid setting " + sourceText + " for parameter sourceText"); + } + this.sourceText = sourceText; + } + + public String getCQuery() { + return cQuery; + } + + public void setCQuery(String cQuery) { + this.cQuery = cQuery; + } + + public String getXPath() { + return xPath; + } + + public void setXPath(String xPath) { + this.xPath = xPath; + } + + public String getParameterString() { + String retString = super.getParameterString(); + try { + if (sourceText != null) retString += "&sourceText=" + sourceText; + if (cQuery != null) retString += "&cquery=" + URLEncoder.encode(cQuery, "UTF-8"); + if (xPath != null) retString += "&xpath=" + URLEncoder.encode(xPath, "UTF-8"); + } catch (UnsupportedEncodingException e) { + retString = ""; + } + return retString; + } + +} diff --git a/src/main/java/com/alchemyapi/api/parameters/NamedEntityParameters.java b/src/main/java/com/alchemyapi/api/parameters/NamedEntityParameters.java new file mode 100644 index 0000000..7613333 --- /dev/null +++ b/src/main/java/com/alchemyapi/api/parameters/NamedEntityParameters.java @@ -0,0 +1,143 @@ +package com.alchemyapi.api.parameters; + +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; + + +public class NamedEntityParameters extends Parameters { + public static final String CLEANED_OR_RAW = "cleaned_or_raw"; + public static final String CLEANED = "cleaned"; + public static final String RAW = "raw"; + public static final String CQUERY = "cquery"; + public static final String XPATH = "xpath"; + + private Boolean disambiguate; + private Boolean linkedData; + private Boolean coreference; + private Boolean quotations; + private String sourceText; + private Boolean showSourceText; + private String cQuery; + private String xPath; + private Integer maxRetrieve; + private String baseUrl; + private Boolean sentiment; + + public boolean isDisambiguate() { + return disambiguate; + } + + public void setDisambiguate(boolean disambiguate) { + this.disambiguate = disambiguate; + } + + public boolean isLinkedData() { + return linkedData; + } + + public void setLinkedData(boolean linkedData) { + this.linkedData = linkedData; + } + + public boolean isCoreference() { + return coreference; + } + + public void setCoreference(boolean coreference) { + this.coreference = coreference; + } + + public boolean isQuotations() { + return quotations; + } + + public void setQuotations(boolean quotations) { + this.quotations = quotations; + } + + public String getSourceText() { + return sourceText; + } + + public void setSourceText(String sourceText) { + if (!sourceText.equals(NamedEntityParameters.CLEANED) && !sourceText.equals(NamedEntityParameters.CLEANED_OR_RAW) + && !sourceText.equals(NamedEntityParameters.RAW) && !sourceText.equals(NamedEntityParameters.CQUERY) + && !sourceText.equals(NamedEntityParameters.XPATH)) { + throw new RuntimeException("Invalid setting " + sourceText + " for parameter sourceText"); + } + this.sourceText = sourceText; + } + + public boolean isShowSourceText() { + return showSourceText; + } + + public void setShowSourceText(boolean showSourceText) { + this.showSourceText = showSourceText; + } + + public String getCQuery() { + return cQuery; + } + + public void setCQuery(String cQuery) { + this.cQuery = cQuery; + } + + public String getXPath() { + return xPath; + } + + public void setXPath(String xPath) { + this.xPath = xPath; + } + + public int getMaxRetrieve() { + return maxRetrieve; + } + + public void setMaxRetrieve(int maxRetrieve) { + this.maxRetrieve = maxRetrieve; + } + + public String getBaseUrl() { + return baseUrl; + } + + public void setBaseUrl(String baseUrl) { + this.baseUrl = baseUrl; + } + + public boolean isSentiment() { + return sentiment; + } + + public void setSentiment(boolean sentiment) { + this.sentiment = sentiment; + } + + public String getParameterString() { + String retString = super.getParameterString(); + try { + if (disambiguate != null) retString += "&disambiguate=" + (disambiguate ? "1" : "0"); + if (linkedData != null) retString += "&linkedData=" + (linkedData ? "1" : "0"); + if (coreference != null) retString += "&coreference=" + (coreference ? "1" : "0"); + if (quotations != null) retString += ""ations=" + (quotations ? "1" : "0"); + if (sourceText != null) retString += "&sourceText=" + sourceText; + if (showSourceText != null) retString += "&showSourceText=" + (showSourceText ? "1" : "0"); + if (cQuery != null) retString += "&cquery=" + URLEncoder.encode(cQuery, "UTF-8"); + if (xPath != null) retString += "&xpath=" + URLEncoder.encode(xPath, "UTF-8"); + if (maxRetrieve != null) retString += "&maxRetrieve=" + maxRetrieve.toString(); + if (baseUrl != null) retString += "&baseUrl=" + URLEncoder.encode(baseUrl, "UTF-8"); + if (sentiment != null) retString += "&sentiment=" + (sentiment ? "1" : "0"); + } catch (UnsupportedEncodingException e) { + retString = ""; + } + return retString; + } + + +} + + + diff --git a/src/main/java/com/alchemyapi/api/parameters/Parameters.java b/src/main/java/com/alchemyapi/api/parameters/Parameters.java new file mode 100644 index 0000000..8b25214 --- /dev/null +++ b/src/main/java/com/alchemyapi/api/parameters/Parameters.java @@ -0,0 +1,85 @@ +package com.alchemyapi.api.parameters; + +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; + + +public class Parameters { + public static final String OUTPUT_JSON = "json"; + public static final String OUTPUT_XML = "xml"; + public static final String OUTPUT_RDF = "rdf"; + + private String url; + private String html; + private String text; + private String outputMode = OUTPUT_XML; // TODO make json default + private String customParameters; + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + + public String getHtml() { + return html; + } + + public void setHtml(String html) { + this.html = html; + } + + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } + + public String getOutputMode() { + return outputMode; + } + + public void setOutputMode(String outputMode) { + if (!outputMode.equals(Parameters.OUTPUT_XML) && !outputMode.equals(OUTPUT_RDF)) { + throw new RuntimeException("Invalid setting " + outputMode + " for parameter outputMode"); + } + this.outputMode = outputMode; + } + + public String getCustomParameters() { + return customParameters; + } + + public void setCustomParameters(String... customParameters) { + StringBuilder data = new StringBuilder(); + try { + for (int i = 0; i < customParameters.length; ++i) { + data.append('&').append(customParameters[i]); + if (++i < customParameters.length) + data.append('=').append(URLEncoder.encode(customParameters[i], "UTF8")); + } + } catch (UnsupportedEncodingException e) { + this.customParameters = ""; + return; + } + this.customParameters = data.toString(); + } + + public String getParameterString() { + String retString = ""; + try { + if (url != null) retString += "&url=" + URLEncoder.encode(url, "UTF-8"); + if (html != null) retString += "&html=" + URLEncoder.encode(html, "UTF-8"); + if (text != null) retString += "&text=" + URLEncoder.encode(text, "UTF-8"); + if (customParameters != null) retString += customParameters; + if (outputMode != null) retString += "&outputMode=" + outputMode; + } catch (UnsupportedEncodingException e) { + retString = ""; + } + return retString; + } +} diff --git a/src/main/java/com/alchemyapi/api/parameters/RelationParameters.java b/src/main/java/com/alchemyapi/api/parameters/RelationParameters.java new file mode 100644 index 0000000..8a5cb04 --- /dev/null +++ b/src/main/java/com/alchemyapi/api/parameters/RelationParameters.java @@ -0,0 +1,163 @@ +package com.alchemyapi.api.parameters; + +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; + + +public class RelationParameters extends Parameters { + public static final String CLEANED_OR_RAW = "cleaned_or_raw"; + public static final String CLEANED = "cleaned"; + public static final String RAW = "raw"; + public static final String CQUERY = "cquery"; + public static final String XPATH = "xpath"; + + private Boolean disambiguate; + private Boolean linkedData; + private Boolean coreference; + private String sourceText; + private Boolean showSourceText; + private Boolean entities; + private Boolean sentimentExcludeEntities; + private Boolean requireEntities; + private String cQuery; + private String xPath; + private Integer maxRetrieve; + private String baseUrl; + private Boolean sentiment; + + public boolean isDisambiguate() { + return disambiguate; + } + + public void setDisambiguate(boolean disambiguate) { + this.disambiguate = disambiguate; + } + + public boolean isLinkedData() { + return linkedData; + } + + public void setLinkedData(boolean linkedData) { + this.linkedData = linkedData; + } + + public boolean isCoreference() { + return coreference; + } + + public void setCoreference(boolean coreference) { + this.coreference = coreference; + } + + public String getSourceText() { + return sourceText; + } + + public void setSourceText(String sourceText) { + if (!sourceText.equals(NamedEntityParameters.CLEANED) && !sourceText.equals(NamedEntityParameters.CLEANED_OR_RAW) + && !sourceText.equals(NamedEntityParameters.RAW) && !sourceText.equals(NamedEntityParameters.CQUERY) + && !sourceText.equals(NamedEntityParameters.XPATH)) { + throw new RuntimeException("Invalid setting " + sourceText + " for parameter sourceText"); + } + this.sourceText = sourceText; + } + + public boolean isShowSourceText() { + return showSourceText; + } + + public void setShowSourceText(boolean showSourceText) { + this.showSourceText = showSourceText; + } + + public boolean isEntities() { + return entities; + } + + public void setEntities(boolean entities) { + this.entities = entities; + } + + public boolean isSentimentExcludeEntities() { + return sentimentExcludeEntities; + } + + public void setSentimentExcludeEntities(boolean sentimentExcludeEntities) { + this.sentimentExcludeEntities = sentimentExcludeEntities; + } + + public boolean isRequireEntities() { + return requireEntities; + } + + public void setRequireEntities(boolean requireEntities) { + this.requireEntities = requireEntities; + } + + public String getCQuery() { + return cQuery; + } + + public void setCQuery(String cQuery) { + this.cQuery = cQuery; + } + + public String getXPath() { + return xPath; + } + + public void setXPath(String xPath) { + this.xPath = xPath; + } + + public int getMaxRetrieve() { + return maxRetrieve; + } + + public void setMaxRetrieve(int maxRetrieve) { + this.maxRetrieve = maxRetrieve; + } + + public String getBaseUrl() { + return baseUrl; + } + + public void setBaseUrl(String baseUrl) { + this.baseUrl = baseUrl; + } + + public boolean isSentiment() { + return sentiment; + } + + public void setSentiment(boolean sentiment) { + this.sentiment = sentiment; + } + + public String getParameterString() { + String retString = super.getParameterString(); + try { + if (disambiguate != null) retString += "&disambiguate=" + (disambiguate ? "1" : "0"); + if (linkedData != null) retString += "&linkedData=" + (linkedData ? "1" : "0"); + if (coreference != null) retString += "&coreference=" + (coreference ? "1" : "0"); + if (sourceText != null) retString += "&sourceText=" + sourceText; + if (showSourceText != null) retString += "&showSourceText=" + (showSourceText ? "1" : "0"); + if (entities != null) retString += "&entities=" + (entities ? "1" : "0"); + if (sentimentExcludeEntities != null) + retString += "&sentimentExcludeEntities=" + (sentimentExcludeEntities ? "1" : "0"); + if (requireEntities != null) retString += "&requireEntities=" + (requireEntities ? "1" : "0"); + if (cQuery != null) retString += "&cquery=" + URLEncoder.encode(cQuery, "UTF-8"); + if (xPath != null) retString += "&xpath=" + URLEncoder.encode(xPath, "UTF-8"); + if (maxRetrieve != null) retString += "&maxRetrieve=" + maxRetrieve.toString(); + if (baseUrl != null) retString += "&baseUrl=" + URLEncoder.encode(baseUrl, "UTF-8"); + if (sentiment != null) retString += "&sentiment=" + (sentiment ? "1" : "0"); + } catch (UnsupportedEncodingException e) { + retString = ""; + } + return retString; + } + +} + + + diff --git a/src/main/java/com/alchemyapi/api/parameters/TargetedSentimentParameters.java b/src/main/java/com/alchemyapi/api/parameters/TargetedSentimentParameters.java new file mode 100644 index 0000000..6a07adf --- /dev/null +++ b/src/main/java/com/alchemyapi/api/parameters/TargetedSentimentParameters.java @@ -0,0 +1,39 @@ +package com.alchemyapi.api.parameters; + +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; + +public class TargetedSentimentParameters extends Parameters { + + private Boolean showSourceText; + private String target; + + public boolean isShowSourceText() { + return showSourceText; + } + + public void setShowSourceText(boolean showSourceText) { + this.showSourceText = showSourceText; + } + + public String getTarget() { + return target; + } + + public void setTarget(String target) { + this.target = target; + } + + public String getParameterString() { + String retString = super.getParameterString(); + try { + if (showSourceText != null) retString += "&showSourceText=" + (showSourceText ? "1" : "0"); + if (target != null) retString += "&target=" + URLEncoder.encode(target, "UTF-8"); + + } catch (UnsupportedEncodingException e) { + retString = ""; + } + + return retString; + } +} diff --git a/src/main/java/com/alchemyapi/api/parameters/TaxonomyParameters.java b/src/main/java/com/alchemyapi/api/parameters/TaxonomyParameters.java new file mode 100644 index 0000000..c7ee116 --- /dev/null +++ b/src/main/java/com/alchemyapi/api/parameters/TaxonomyParameters.java @@ -0,0 +1,138 @@ +package com.alchemyapi.api.parameters; + +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; + + +public class TaxonomyParameters extends Parameters { + public static final String CLEANED_OR_RAW = "cleaned_or_raw"; + public static final String CLEANED = "cleaned"; + public static final String RAW = "raw"; + public static final String CQUERY = "cquery"; + public static final String XPATH = "xpath"; + + private Boolean disambiguate; + private Boolean linkedData; + private Boolean coreference; + private Boolean quotations; + private String sourceText; + private Boolean showSourceText; + private String cQuery; + private String xPath; + private Integer maxRetrieve; + private String baseUrl; + private Boolean sentiment; + + public boolean isDisambiguate() { + return disambiguate; + } + + public void setDisambiguate(boolean disambiguate) { + this.disambiguate = disambiguate; + } + + public boolean isLinkedData() { + return linkedData; + } + + public void setLinkedData(boolean linkedData) { + this.linkedData = linkedData; + } + + public boolean isCoreference() { + return coreference; + } + + public void setCoreference(boolean coreference) { + this.coreference = coreference; + } + + public boolean isQuotations() { + return quotations; + } + + public void setQuotations(boolean quotations) { + this.quotations = quotations; + } + + public String getSourceText() { + return sourceText; + } + + public void setSourceText(String sourceText) { + if (!sourceText.equals(TaxonomyParameters.CLEANED) && !sourceText.equals(TaxonomyParameters.CLEANED_OR_RAW) + && !sourceText.equals(TaxonomyParameters.RAW) && !sourceText.equals(TaxonomyParameters.CQUERY) + && !sourceText.equals(TaxonomyParameters.XPATH)) { + throw new RuntimeException("Invalid setting " + sourceText + " for parameter sourceText"); + } + this.sourceText = sourceText; + } + + public boolean isShowSourceText() { + return showSourceText; + } + + public void setShowSourceText(boolean showSourceText) { + this.showSourceText = showSourceText; + } + + public String getCQuery() { + return cQuery; + } + + public void setCQuery(String cQuery) { + this.cQuery = cQuery; + } + + public String getXPath() { + return xPath; + } + + public void setXPath(String xPath) { + this.xPath = xPath; + } + + public int getMaxRetrieve() { + return maxRetrieve; + } + + public void setMaxRetrieve(int maxRetrieve) { + this.maxRetrieve = maxRetrieve; + } + + public String getBaseUrl() { + return baseUrl; + } + + public void setBaseUrl(String baseUrl) { + this.baseUrl = baseUrl; + } + + public boolean isSentiment() { + return sentiment; + } + + public void setSentiment(boolean sentiment) { + this.sentiment = sentiment; + } + + public String getParameterString() { + String retString = super.getParameterString(); + try { + if (disambiguate != null) retString += "&disambiguate=" + (disambiguate ? "1" : "0"); + if (linkedData != null) retString += "&linkedData=" + (linkedData ? "1" : "0"); + if (coreference != null) retString += "&coreference=" + (coreference ? "1" : "0"); + if (quotations != null) retString += ""ations=" + (quotations ? "1" : "0"); + if (sourceText != null) retString += "&sourceText=" + sourceText; + if (showSourceText != null) retString += "&showSourceText=" + (showSourceText ? "1" : "0"); + if (cQuery != null) retString += "&cquery=" + URLEncoder.encode(cQuery, "UTF-8"); + if (xPath != null) retString += "&xpath=" + URLEncoder.encode(xPath, "UTF-8"); + if (maxRetrieve != null) retString += "&maxRetrieve=" + maxRetrieve.toString(); + if (baseUrl != null) retString += "&baseUrl=" + URLEncoder.encode(baseUrl, "UTF-8"); + if (sentiment != null) retString += "&sentiment=" + (sentiment ? "1" : "0"); + } catch (UnsupportedEncodingException e) { + retString = ""; + } + return retString; + } +} diff --git a/src/main/java/com/alchemyapi/api/parameters/TextParameters.java b/src/main/java/com/alchemyapi/api/parameters/TextParameters.java new file mode 100644 index 0000000..a6511cc --- /dev/null +++ b/src/main/java/com/alchemyapi/api/parameters/TextParameters.java @@ -0,0 +1,32 @@ +package com.alchemyapi.api.parameters; + + +public class TextParameters extends Parameters { + private Boolean useMetaData; + private Boolean extractLinks; + + public boolean isUseMetaData() { + return useMetaData; + } + + public void setUseMetaData(boolean useMetaData) { + this.useMetaData = useMetaData; + } + + public boolean isExtractLinks() { + return extractLinks; + } + + public void setExtractLinks(boolean extractLinks) { + this.extractLinks = extractLinks; + } + + public String getParameterString() { + String retString = super.getParameterString(); + + if (useMetaData != null) retString += "&useMetaData=" + (useMetaData ? "1" : "0"); + if (extractLinks != null) retString += "&extractLinks=" + (extractLinks ? "1" : "0"); + + return retString; + } +} diff --git a/src/test/java/com/alchemyapi/CategoryTest.java b/src/test/java/com/alchemyapi/CategoryTest.java index 317ef7d..4fa24a1 100644 --- a/src/test/java/com/alchemyapi/CategoryTest.java +++ b/src/test/java/com/alchemyapi/CategoryTest.java @@ -1,8 +1,8 @@ package com.alchemyapi; import com.alchemyapi.api.AlchemyAPI; -import com.alchemyapi.api.AlchemyAPI_CategoryParams; -import com.alchemyapi.api.AlchemyAPI_Params; +import com.alchemyapi.api.parameters.CategoryParameters; +import com.alchemyapi.api.parameters.Parameters; import org.w3c.dom.Document; import org.xml.sax.SAXException; @@ -43,9 +43,9 @@ public static void main(String[] args) doc = alchemyObj.HTMLGetCategory(htmlDoc, "http://www.test.com/"); System.out.println(getStringFromDocument(doc)); - AlchemyAPI_CategoryParams categoryParams = new AlchemyAPI_CategoryParams(); - categoryParams.setOutputMode(AlchemyAPI_Params.OUTPUT_RDF); - doc = alchemyObj.HTMLGetCategory(htmlDoc, "http://www.test.com/", categoryParams); + CategoryParameters categoryParameters = new CategoryParameters(); + categoryParameters.setOutputMode(Parameters.OUTPUT_RDF); + doc = alchemyObj.HTMLGetCategory(htmlDoc, "http://www.test.com/", categoryParameters); System.out.println(getStringFromDocument(doc)); } diff --git a/src/test/java/com/alchemyapi/CombinedTest.java b/src/test/java/com/alchemyapi/CombinedTest.java index 7d05302..3c92b15 100644 --- a/src/test/java/com/alchemyapi/CombinedTest.java +++ b/src/test/java/com/alchemyapi/CombinedTest.java @@ -2,6 +2,7 @@ import com.alchemyapi.api.*; +import com.alchemyapi.api.parameters.CombinedParameters; import org.xml.sax.SAXException; import org.w3c.dom.Document; import java.io.*; @@ -32,7 +33,7 @@ public static void main(String[] args) System.out.println(getStringFromDocument(doc)); // Only extract entities & keywords - AlchemyAPI_CombinedParams combinedParams = new AlchemyAPI_CombinedParams(); + CombinedParameters combinedParams = new CombinedParameters(); combinedParams.setSentiment(true); combinedParams.setExtract("entity"); combinedParams.setExtract("keyword"); diff --git a/src/test/java/com/alchemyapi/ImageTest.java b/src/test/java/com/alchemyapi/ImageTest.java index c5f64ca..919f2c2 100644 --- a/src/test/java/com/alchemyapi/ImageTest.java +++ b/src/test/java/com/alchemyapi/ImageTest.java @@ -2,6 +2,7 @@ import com.alchemyapi.api.*; +import com.alchemyapi.api.parameters.ImageParameters; import org.xml.sax.SAXException; import org.w3c.dom.Document; import java.io.*; @@ -12,7 +13,6 @@ import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; -import java.net.URLEncoder; class ImageTest { public static void main(String[] args) @@ -32,9 +32,9 @@ public static void main(String[] args) byte[] imageByteArray = readFile("data/cat.jpg"); - AlchemyAPI_ImageParams imageParams = new AlchemyAPI_ImageParams(); + ImageParameters imageParams = new ImageParameters(); imageParams.setImage(imageByteArray); - imageParams.setImagePostMode(AlchemyAPI_ImageParams.RAW); + imageParams.setImagePostMode(ImageParameters.RAW); doc = alchemyObj.ImageGetRankedImageKeywords(imageParams); System.out.println(getStringFromDocument(doc)); } diff --git a/src/test/java/com/alchemyapi/ParameterTest.java b/src/test/java/com/alchemyapi/ParameterTest.java index 4a60f01..e6a9f25 100644 --- a/src/test/java/com/alchemyapi/ParameterTest.java +++ b/src/test/java/com/alchemyapi/ParameterTest.java @@ -2,6 +2,7 @@ import com.alchemyapi.api.*; +import com.alchemyapi.api.parameters.NamedEntityParameters; import org.xml.sax.SAXException; import org.w3c.dom.Document; import java.io.*; @@ -22,7 +23,7 @@ public static void main(String[] args) AlchemyAPI alchemyObj = AlchemyAPI.GetInstanceFromFile("api_key.txt"); // Extract a ranked list of named entities for a web URL. Turn off disambiguation - AlchemyAPI_NamedEntityParams entityParams = new AlchemyAPI_NamedEntityParams(); + NamedEntityParameters entityParams = new NamedEntityParameters(); entityParams.setDisambiguate(false); entityParams.setSentiment(true); Document doc = alchemyObj.URLGetRankedNamedEntities("http://www.techcrunch.com/", entityParams); diff --git a/src/test/java/com/alchemyapi/RelationsTest.java b/src/test/java/com/alchemyapi/RelationsTest.java index 311a97c..208b565 100644 --- a/src/test/java/com/alchemyapi/RelationsTest.java +++ b/src/test/java/com/alchemyapi/RelationsTest.java @@ -2,6 +2,7 @@ import com.alchemyapi.api.*; +import com.alchemyapi.api.parameters.RelationParameters; import org.xml.sax.SAXException; import org.w3c.dom.Document; import java.io.*; @@ -38,7 +39,7 @@ public static void main(String[] args) doc = alchemyObj.HTMLGetRelations(htmlDoc, "http://www.test.com/"); System.out.println(getStringFromDocument(doc)); - AlchemyAPI_RelationParams relationParams = new AlchemyAPI_RelationParams(); + RelationParameters relationParams = new RelationParameters(); relationParams.setSentiment(true); relationParams.setEntities(true); relationParams.setDisambiguate(true); diff --git a/src/test/java/com/alchemyapi/STestSentiment.java b/src/test/java/com/alchemyapi/STestSentiment.java index 9ef7560..5ffceb0 100644 --- a/src/test/java/com/alchemyapi/STestSentiment.java +++ b/src/test/java/com/alchemyapi/STestSentiment.java @@ -1,9 +1,9 @@ package com.alchemyapi; import com.alchemyapi.api.AlchemyAPI; -import com.alchemyapi.api.AlchemyAPI_KeywordParams; -import com.alchemyapi.api.AlchemyAPI_NamedEntityParams; -import com.alchemyapi.api.AlchemyAPI_TargetedSentimentParams; +import com.alchemyapi.api.parameters.KeywordParameters; +import com.alchemyapi.api.parameters.NamedEntityParameters; +import com.alchemyapi.api.parameters.TargetedSentimentParameters; import com.alchemyapi.util.DocumentUtils; import com.alchemyapi.util.ResourceUtils; import org.junit.Test; @@ -40,17 +40,17 @@ public void file() throws SAXException, ParserConfigurationException, XPathExpre @Test public void entityTargetedSentimentText() throws SAXException, ParserConfigurationException, XPathExpressionException, IOException { - final AlchemyAPI_NamedEntityParams entityParams = new AlchemyAPI_NamedEntityParams(); + final NamedEntityParameters entityParams = new NamedEntityParameters(); entityParams.setSentiment(true); final Document document = alchemyAPI.TextGetRankedNamedEntities("That Mike Tyson is such a sweetheart.", entityParams); System.out.println(DocumentUtils.toString(document)); - final AlchemyAPI_KeywordParams keywordParams = new AlchemyAPI_KeywordParams(); + final KeywordParameters keywordParams = new KeywordParameters(); keywordParams.setSentiment(true); final Document document2 = alchemyAPI.TextGetRankedKeywords("That Mike Tyson is such a sweetheart.", keywordParams); System.out.println(DocumentUtils.toString(document2)); - final AlchemyAPI_TargetedSentimentParams sentimentParams = new AlchemyAPI_TargetedSentimentParams(); + final TargetedSentimentParameters sentimentParams = new TargetedSentimentParameters(); sentimentParams.setShowSourceText(true); final Document document3 = alchemyAPI.TextGetTargetedSentiment("This car is terrible.", "car", sentimentParams); System.out.print(DocumentUtils.toString(document3)); @@ -58,7 +58,7 @@ public void entityTargetedSentimentText() throws SAXException, ParserConfigurati @Test public void entityTargetedSentimentUrl() throws SAXException, ParserConfigurationException, XPathExpressionException, IOException { - final AlchemyAPI_TargetedSentimentParams sentimentParams = new AlchemyAPI_TargetedSentimentParams(); + final TargetedSentimentParameters sentimentParams = new TargetedSentimentParameters(); sentimentParams.setShowSourceText(true); final Document document = alchemyAPI.URLGetTargetedSentiment("http://techcrunch.com/2012/03/01/keen-on-anand-rajaraman-how-walmart-wants-to-leapfrog-over-amazon-tctv/", "Walmart", sentimentParams); @@ -67,7 +67,7 @@ public void entityTargetedSentimentUrl() throws SAXException, ParserConfiguratio @Test public void entityTargetedSentimentHtml() throws SAXException, ParserConfigurationException, XPathExpressionException, IOException { - final AlchemyAPI_TargetedSentimentParams sentimentParams = new AlchemyAPI_TargetedSentimentParams(); + final TargetedSentimentParameters sentimentParams = new TargetedSentimentParameters(); sentimentParams.setShowSourceText(true); final String html = ResourceUtils.toString("data/example.html"); From 9db597e183242f724cf4fd836169f0d357cd3ce7 Mon Sep 17 00:00:00 2001 From: Kenny Cason Date: Sun, 2 Aug 2015 04:44:50 -0500 Subject: [PATCH 04/11] #8 refactor core api --- alchemyapi_java.iml | 19 - pom.xml | 11 + .../java/com/alchemyapi/api/AlchemyAPI.java | 1432 ++++++----------- .../api/AlchemyApiConfiguration.java | 40 + .../api/exceptions/AlchemyApiException.java | 16 + .../api/parameters/CategoryParameters.java | 4 +- .../api/parameters/CombinedParameters.java | 4 +- .../api/parameters/ConceptParameters.java | 4 +- .../parameters/ConstraintQueryParameters.java | 4 +- .../api/parameters/ImageParameters.java | 4 +- .../api/parameters/KeywordParameters.java | 4 +- .../api/parameters/LanguageParameters.java | 4 +- .../api/parameters/NamedEntityParameters.java | 4 +- .../alchemyapi/api/parameters/Parameters.java | 11 +- .../api/parameters/RelationParameters.java | 4 +- .../TargetedSentimentParameters.java | 4 +- .../api/parameters/TaxonomyParameters.java | 4 +- .../api/parameters/TextParameters.java | 4 +- src/test/java/com/alchemyapi/STestAuthor.java | 15 +- .../java/com/alchemyapi/STestSentiment.java | 32 +- .../{util => helpers}/DocumentUtils.java | 2 +- .../{util => helpers}/ResourceUtils.java | 2 +- .../alchemyapi/helpers/TestApiFactory.java | 31 + src/test/resources/api_key.txt | 1 - src/test/resources/log4j.xml | 17 + .../to_fix}/CategoryTest.java | 6 +- .../to_fix}/CombinedTest.java | 4 +- .../to_fix}/ConceptTest.java | 6 +- .../to_fix}/ConstraintQueryTest.java | 6 +- .../to_fix}/EntityTest.java | 6 +- .../to_fix}/FeedLinksTest.java | 6 +- .../to_fix}/ImageTest.java | 4 +- .../to_fix}/KeywordTest.java | 6 +- .../to_fix}/LanguageTest.java | 6 +- .../to_fix}/MicroformatsTest.java | 6 +- .../to_fix}/ParameterTest.java | 4 +- .../to_fix}/RelationsTest.java | 4 +- .../to_fix}/TaxonomyTest.java | 4 +- .../to_fix}/TextExtractTest.java | 6 +- 39 files changed, 699 insertions(+), 1052 deletions(-) delete mode 100644 alchemyapi_java.iml create mode 100644 src/main/java/com/alchemyapi/api/AlchemyApiConfiguration.java create mode 100644 src/main/java/com/alchemyapi/api/exceptions/AlchemyApiException.java rename src/test/java/com/alchemyapi/{util => helpers}/DocumentUtils.java (96%) rename src/test/java/com/alchemyapi/{util => helpers}/ResourceUtils.java (94%) create mode 100644 src/test/java/com/alchemyapi/helpers/TestApiFactory.java delete mode 100644 src/test/resources/api_key.txt create mode 100644 src/test/resources/log4j.xml rename src/test/{java/com/alchemyapi => resources/to_fix}/CategoryTest.java (96%) rename src/test/{java/com/alchemyapi => resources/to_fix}/CombinedTest.java (96%) rename src/test/{java/com/alchemyapi => resources/to_fix}/ConceptTest.java (95%) rename src/test/{java/com/alchemyapi => resources/to_fix}/ConstraintQueryTest.java (94%) rename src/test/{java/com/alchemyapi => resources/to_fix}/EntityTest.java (95%) rename src/test/{java/com/alchemyapi => resources/to_fix}/FeedLinksTest.java (95%) rename src/test/{java/com/alchemyapi => resources/to_fix}/ImageTest.java (96%) rename src/test/{java/com/alchemyapi => resources/to_fix}/KeywordTest.java (95%) rename src/test/{java/com/alchemyapi => resources/to_fix}/LanguageTest.java (95%) rename src/test/{java/com/alchemyapi => resources/to_fix}/MicroformatsTest.java (95%) rename src/test/{java/com/alchemyapi => resources/to_fix}/ParameterTest.java (94%) rename src/test/{java/com/alchemyapi => resources/to_fix}/RelationsTest.java (97%) rename src/test/{java/com/alchemyapi => resources/to_fix}/TaxonomyTest.java (96%) rename src/test/{java/com/alchemyapi => resources/to_fix}/TextExtractTest.java (96%) diff --git a/alchemyapi_java.iml b/alchemyapi_java.iml deleted file mode 100644 index f78a651..0000000 --- a/alchemyapi_java.iml +++ /dev/null @@ -1,19 +0,0 @@ - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/pom.xml b/pom.xml index 73d9860..4f12a06 100644 --- a/pom.xml +++ b/pom.xml @@ -19,6 +19,17 @@ commons-io 2.4 + + org.apache.commons + commons-lang3 + 3.4 + + + + log4j + log4j + 1.2.17 + junit diff --git a/src/main/java/com/alchemyapi/api/AlchemyAPI.java b/src/main/java/com/alchemyapi/api/AlchemyAPI.java index 733f332..5780ee7 100644 --- a/src/main/java/com/alchemyapi/api/AlchemyAPI.java +++ b/src/main/java/com/alchemyapi/api/AlchemyAPI.java @@ -1,5 +1,6 @@ package com.alchemyapi.api; +import com.alchemyapi.api.exceptions.AlchemyApiException; import com.alchemyapi.api.parameters.CategoryParameters; import com.alchemyapi.api.parameters.CombinedParameters; import com.alchemyapi.api.parameters.ConceptParameters; @@ -13,6 +14,7 @@ import com.alchemyapi.api.parameters.TargetedSentimentParameters; import com.alchemyapi.api.parameters.TaxonomyParameters; import com.alchemyapi.api.parameters.TextParameters; +import org.apache.log4j.Logger; import org.w3c.dom.Document; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; @@ -24,1060 +26,602 @@ import javax.xml.xpath.XPathExpression; import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; -import java.io.BufferedReader; import java.io.DataInputStream; import java.io.DataOutputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; import java.io.IOException; -import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; +import java.nio.charset.Charset; -public class AlchemyAPI { +import static org.apache.commons.lang3.StringUtils.isBlank; +import static org.apache.commons.lang3.StringUtils.isNotBlank; +import static org.apache.commons.lang3.StringUtils.length; +import static org.apache.commons.lang3.StringUtils.trimToEmpty; - private String _apiKey; - private String _requestUri = "http://access.alchemyapi.com/calls/"; +public class AlchemyApi { - private AlchemyAPI() { + private static final Logger LOGGER = Logger.getLogger(AlchemyApi.class); + + private static final String API_URL = "http://{SUB_DOMAIN}.alchemyapi.com/calls/"; + + private final AlchemyApiConfiguration configuration; + + public AlchemyApi(final AlchemyApiConfiguration configuration) { + if(configuration == null) { throw new AlchemyApiException("Configuration must not be null"); } + valididateConfiguration(configuration); + LOGGER.info("Loaded Configuration: " + configuration); + this.configuration = configuration; + } + + private static void valididateConfiguration(final AlchemyApiConfiguration configuration) { + if(length(configuration.getApiKey()) < 5) { throw new AlchemyApiException("API key must be at least 5 characters"); } + } + + public Document urlGetAuthor(final URL url) { + return urlGetAuthor(url, new Parameters()); + } + + public Document urlGetAuthor(final URL url, final Parameters params) { + params.setUrl(url); + return get("URLGetAuthor", "url", params); + } + + public Document htmlGetAuthor(final String html, final URL url) { + return htmlGetAuthor(html, url, new Parameters()); + } + + public Document htmlGetAuthor(final String html, final URL url, final Parameters params) { + params.setHtml(html); + params.setUrl(url); + return post("HTMLGetAuthor", "html", params); + } + + public Document urlGetRankedNamedEntities(final URL url) { + return urlGetRankedNamedEntities(url, new NamedEntityParameters()); + } + + public Document urlGetRankedNamedEntities(final URL url, final NamedEntityParameters params) { + params.setUrl(url); + return get("URLGetRankedNamedEntities", "url", params); + } + + public Document htmlGetRankedNamedEntities(final String html, final URL url) { + return htmlGetRankedNamedEntities(html, url, new NamedEntityParameters()); + } + + public Document htmlGetRankedNamedEntities(final String html, final URL url, final NamedEntityParameters params) { + params.setUrl(url); + params.setHtml(html); + return post("HTMLGetRankedNamedEntities", "html", params); } + public Document textGetRankedNamedEntities(final String text) { + return textGetRankedNamedEntities(text, new NamedEntityParameters()); + } - static public AlchemyAPI GetInstanceFromFile(String keyFilename) - throws FileNotFoundException, IOException - { - AlchemyAPI api = new AlchemyAPI(); - api.LoadAPIKey(keyFilename); + public Document textGetRankedNamedEntities(final String text, final NamedEntityParameters params) { + params.setText(text); + return post("TextGetRankedNamedEntities", "text", params); + } - return api; + public Document urlGetRankedConcepts(final URL url) { + return urlGetRankedConcepts(url, new ConceptParameters()); } - static public AlchemyAPI GetInstanceFromString(String apiKey) - { - AlchemyAPI api = new AlchemyAPI(); - api.SetAPIKey(apiKey); + public Document urlGetRankedConcepts(final URL url, final ConceptParameters params) { + params.setUrl(url); + return get("URLGetRankedConcepts", "url", params); + } + + public Document htmlGetRankedConcepts(final String html, final URL url) { + return htmlGetRankedConcepts(html, url, new ConceptParameters()); + } + + public Document htmlGetRankedConcepts(final String html, final URL url, final ConceptParameters params) { + params.setUrl(url); + params.setHtml(html); + return post("HTMLGetRankedConcepts", "html", params); + } - return api; + public Document textGetRankedConcepts(final String text) { + return textGetRankedConcepts(text, new ConceptParameters()); } - public void LoadAPIKey(String filename) throws IOException, FileNotFoundException - { - if (null == filename || 0 == filename.length()) - throw new IllegalArgumentException("Empty API key file specified."); + public Document textGetRankedConcepts(final String text, final ConceptParameters params) { + params.setText(text); + return post("TextGetRankedConcepts", "text", params); + } + + public Document urlGetRankedKeywords(final URL url) { + return urlGetRankedKeywords(url, new KeywordParameters()); + } + + public Document urlGetRankedKeywords(final URL url, final KeywordParameters params) { + params.setUrl(url); + return get("URLGetRankedKeywords", "url", params); + } + + public Document htmlGetRankedKeywords(final String html, final URL url) { + return htmlGetRankedKeywords(html, url, new KeywordParameters()); + } + + public Document htmlGetRankedKeywords(final String html, final URL url, final KeywordParameters params) { + params.setUrl(url); + params.setHtml(html); + return post("HTMLGetRankedKeywords", "html", params); + } - File file = new File(filename); - FileInputStream fis = new FileInputStream(file); + public Document textGetRankedKeywords(final String text) throws IOException, SAXException, + ParserConfigurationException, XPathExpressionException { + return textGetRankedKeywords(text, new KeywordParameters()); + } - BufferedReader breader = new BufferedReader(new InputStreamReader(fis)); + public Document textGetRankedKeywords(final String text, final KeywordParameters params) { + params.setText(text); + return post("TextGetRankedKeywords", "text", params); + } - _apiKey = breader.readLine().replaceAll("\\n", "").replaceAll("\\r", ""); + public Document urlGetLanguage(final URL url) { + return urlGetLanguage(url, new LanguageParameters()); + } - fis.close(); - breader.close(); + public Document urlGetLanguage(final URL url, final LanguageParameters params) { + params.setUrl(url); + return get("URLGetLanguage", "url", params); + } - if (null == _apiKey || _apiKey.length() < 5) - throw new IllegalArgumentException("Too short API key."); + public Document htmlGetLanguage(final String html, final URL url) { + return htmlGetLanguage(html, url, new LanguageParameters()); } - public void SetAPIKey(String apiKey) { - _apiKey = apiKey; + public Document htmlGetLanguage(final String html, final URL url, final LanguageParameters params) { + params.setUrl(url); + params.setHtml(html); + return post("HTMLGetLanguage", "html", params); + } - if (null == _apiKey || _apiKey.length() < 5) - throw new IllegalArgumentException("Too short API key."); + public Document textGetLanguage(final String text) { + return textGetLanguage(text, new LanguageParameters()); } - public void SetAPIHost(String apiHost) { - if (null == apiHost || apiHost.length() < 2) - throw new IllegalArgumentException("Too short API host."); + public Document textGetLanguage(final String text, final LanguageParameters params) { + params.setText(text); + return post("TextGetLanguage", "text", params); + } - _requestUri = "http://" + apiHost + ".alchemyapi.com/calls/"; + public Document urlGetCategory(final URL url) { + return urlGetCategory(url, new CategoryParameters()); } - public Document URLGetAuthor(String url) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - return URLGetAuthor(url, new Parameters()); + public Document urlGetCategory(final URL url, final CategoryParameters params) { + params.setUrl(url); + return get("URLGetCategory", "url", params); } - public Document URLGetAuthor(String url, Parameters params) - throws IOException, SAXException, ParserConfigurationException, - XPathExpressionException - { - CheckURL(url); + public Document htmlGetCategory(final String html, URL url) { + return htmlGetCategory(html, url, new CategoryParameters()); + } + public Document htmlGetCategory(final String html, final URL url, final CategoryParameters params) { params.setUrl(url); + params.setHtml(html); + return post("HTMLGetCategory", "html", params); + } + + public Document textGetCategory(final String text) { + return textGetCategory(text, new TextParameters()); + } - return GET("URLGetAuthor", "url", params); + public Document textGetCategory(final String text, final TextParameters params) { + params.setText(text); + return post("TextGetCategory", "text", params); + } + + public Document urlGetText(final URL url) { + return urlGetText(url, new TextParameters()); } - public Document HTMLGetAuthor(String html, String url) - throws IOException, SAXException, ParserConfigurationException, - XPathExpressionException - { - return HTMLGetAuthor(html, url, new Parameters()); + public Document urlGetText(final URL url, final TextParameters params) { + params.setUrl(url); + return get("URLGetText", "url", params); } - public Document HTMLGetAuthor(String html, String url, - Parameters params) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - CheckHTML(html, url); + public Document htmlGetText(final String html, final URL url) { + return htmlGetText(html, url, new TextParameters()); + } + public Document htmlGetText(final String html, final URL url, final TextParameters params) { + params.setUrl(url); params.setHtml(html); + return post("HTMLGetText", "html", params); + } + + public Document urlGetRawText(final URL url) { + return urlGetRawText(url, new Parameters()); + } + + public Document urlGetRawText(final URL url, final Parameters params) { params.setUrl(url); + return get("URLGetRawText", "url", params); + } - return POST("HTMLGetAuthor", "html", params); + public Document htmlGetRawText(final String html, final URL url) { + return htmlGetRawText(html, url, new Parameters()); } - public Document URLGetRankedNamedEntities(String url) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - return URLGetRankedNamedEntities(url, new NamedEntityParameters()); + public Document htmlGetRawText(final String html, final URL url, final Parameters params) { + params.setUrl(url); + params.setHtml(html); + return post("HTMLGetRawText", "html", params); } - - public Document URLGetRankedNamedEntities(String url, NamedEntityParameters params) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - CheckURL(url); - - params.setUrl(url); - return GET("URLGetRankedNamedEntities", "url", params); + public Document urlGetTitle(final URL url) { + return urlGetTitle(url, new Parameters()); } - public Document HTMLGetRankedNamedEntities(String html, String url) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - return HTMLGetRankedNamedEntities(html, url, new NamedEntityParameters()); + public Document urlGetTitle(final URL url, final Parameters params) { + params.setUrl(url); + return get("URLGetTitle", "url", params); + } + + public Document htmlGetTitle(final String html, final URL url) { + return htmlGetTitle(html, url, new Parameters()); } - - public Document HTMLGetRankedNamedEntities(String html, String url, NamedEntityParameters params) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - CheckHTML(html, url); - + public Document htmlGetTitle(final String html, final URL url, final Parameters params) { params.setUrl(url); params.setHtml(html); + return post("HTMLGetTitle", "html", params); + } - return POST("HTMLGetRankedNamedEntities", "html", params); - } - - public Document TextGetRankedNamedEntities(String text) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - return TextGetRankedNamedEntities(text, new NamedEntityParameters()); - } - - public Document TextGetRankedNamedEntities(String text, NamedEntityParameters params) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - CheckText(text); - - params.setText(text); - - return POST("TextGetRankedNamedEntities", "text", params); - } - - - - public Document URLGetRankedConcepts(String url) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - return URLGetRankedConcepts(url, new ConceptParameters()); - } - - public Document URLGetRankedConcepts(String url, ConceptParameters params) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - CheckURL(url); - - params.setUrl(url); - - return GET("URLGetRankedConcepts", "url", params); - } - - - public Document HTMLGetRankedConcepts(String html, String url) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - return HTMLGetRankedConcepts(html, url, new ConceptParameters()); - } - - public Document HTMLGetRankedConcepts(String html, String url, ConceptParameters params) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - CheckHTML(html, url); - - params.setUrl(url); - params.setHtml(html); - - return POST("HTMLGetRankedConcepts", "html", params); - } - - public Document TextGetRankedConcepts(String text) throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException { - return TextGetRankedConcepts(text, new ConceptParameters()); - } - - public Document TextGetRankedConcepts(String text, ConceptParameters params) throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - CheckText(text); - - params.setText(text); - - return POST("TextGetRankedConcepts", "text", params); - } - - - - public Document URLGetRankedKeywords(String url) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - return URLGetRankedKeywords(url, new KeywordParameters()); - } - - public Document URLGetRankedKeywords(String url, KeywordParameters params) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - CheckURL(url); - - params.setUrl(url); - - return GET("URLGetRankedKeywords", "url", params); - } - - - public Document HTMLGetRankedKeywords(String html, String url) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - return HTMLGetRankedKeywords(html, url, new KeywordParameters()); - } - - public Document HTMLGetRankedKeywords(String html, String url, KeywordParameters params) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - CheckHTML(html, url); - - params.setUrl(url); - params.setHtml(html); - - return POST("HTMLGetRankedKeywords", "html", params); - } - - public Document TextGetRankedKeywords(String text) throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException { - return TextGetRankedKeywords(text, new KeywordParameters()); - } - - public Document TextGetRankedKeywords(String text, KeywordParameters params) throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - CheckText(text); - - params.setText(text); - - return POST("TextGetRankedKeywords", "text", params); - } - - public Document URLGetLanguage(String url) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - return URLGetLanguage(url, new LanguageParameters()); - } - - public Document URLGetLanguage(String url, LanguageParameters params) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - CheckURL(url); - - params.setUrl(url); - - return GET("URLGetLanguage", "url", params); - } - - public Document HTMLGetLanguage(String html, String url) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - return HTMLGetLanguage(html, url, new LanguageParameters()); - } - - public Document HTMLGetLanguage(String html, String url, LanguageParameters params) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - CheckHTML(html, url); - - params.setUrl(url); - params.setHtml(html); - - return POST("HTMLGetLanguage", "html", params); - } - - public Document TextGetLanguage(String text) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - return TextGetLanguage(text, new LanguageParameters()); - } - - public Document TextGetLanguage(String text, LanguageParameters params) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - CheckText(text); - - params.setText(text); - - return POST("TextGetLanguage", "text", params); - } - - public Document URLGetCategory(String url) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - return URLGetCategory(url, new CategoryParameters()); - } - - public Document URLGetCategory(String url, CategoryParameters params) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - CheckURL(url); - - params.setUrl(url); - - return GET("URLGetCategory", "url", params); - } - - public Document HTMLGetCategory(String html, String url) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - return HTMLGetCategory(html, url, new CategoryParameters()); - } - - public Document HTMLGetCategory(String html, String url, CategoryParameters params) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - CheckHTML(html, url); - - params.setUrl(url); - params.setHtml(html); - - return POST("HTMLGetCategory", "html", params); - } - - public Document TextGetCategory(String text) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - return TextGetCategory(text, new TextParameters()); - } - - public Document TextGetCategory(String text, TextParameters params) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - CheckText(text); - - params.setText(text); - - return POST("TextGetCategory", "text", params); - } - - public Document URLGetText(String url) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - return URLGetText(url, new TextParameters()); - } - - public Document URLGetText(String url, TextParameters params) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - CheckURL(url); - - params.setUrl(url); - - return GET("URLGetText", "url", params); - } - - public Document HTMLGetText(String html, String url) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - return HTMLGetText(html, url, new TextParameters()); - } - - public Document HTMLGetText(String html, String url, TextParameters params) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - CheckHTML(html, url); - - params.setUrl(url); - params.setHtml(html); - - return POST("HTMLGetText", "html", params); - } - - public Document URLGetRawText(String url) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - return URLGetRawText(url, new Parameters()); - } - - public Document URLGetRawText(String url, Parameters params) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - CheckURL(url); - - params.setUrl(url); - - return GET("URLGetRawText", "url", params); - } - - public Document HTMLGetRawText(String html, String url) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - return HTMLGetRawText(html, url, new Parameters()); - } - - public Document HTMLGetRawText(String html, String url, Parameters params) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - CheckHTML(html, url); - - params.setUrl(url); - params.setHtml(html); - - return POST("HTMLGetRawText", "html", params); - } - - public Document URLGetTitle(String url) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - return URLGetTitle(url, new Parameters()); - } - - public Document URLGetTitle(String url, Parameters params) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - CheckURL(url); - - params.setUrl(url); - - return GET("URLGetTitle", "url", params); - } - - public Document HTMLGetTitle(String html, String url) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - return HTMLGetTitle(html, url, new Parameters()); - } - - public Document HTMLGetTitle(String html, String url, Parameters params) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - CheckHTML(html, url); - - params.setUrl(url); - params.setHtml(html); - - return POST("HTMLGetTitle", "html", params); - } - - public Document URLGetFeedLinks(String url) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - return URLGetFeedLinks(url, new Parameters()); - } - - public Document URLGetFeedLinks(String url, Parameters params) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - CheckURL(url); - - params.setUrl(url); - - return GET("URLGetFeedLinks", "url", params); - } - - public Document HTMLGetFeedLinks(String html, String url) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - return HTMLGetFeedLinks(html, url, new Parameters()); - } - - public Document HTMLGetFeedLinks(String html, String url, Parameters params) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - CheckHTML(html, url); - - params.setUrl(url); - params.setHtml(html); - - return POST("HTMLGetFeedLinks", "html", params); - } - - public Document URLGetMicroformats(String url) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - return URLGetMicroformats(url, new Parameters()); - } - - public Document URLGetMicroformats(String url, Parameters params) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - CheckURL(url); - - params.setUrl(url); - - return GET("URLGetMicroformatData", "url", params); - } - - public Document HTMLGetMicroformats(String html, String url) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - return HTMLGetMicroformats(html, url, new Parameters()); - } - - public Document HTMLGetMicroformats(String html, String url, Parameters params) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - CheckHTML(html, url); - - params.setUrl(url); - params.setHtml(html); - - return POST("HTMLGetMicroformatData", "html", params); - } - - public Document URLGetConstraintQuery(String url, String query) - throws IOException, XPathExpressionException, - SAXException, ParserConfigurationException - { - return URLGetConstraintQuery(url, query, new ConstraintQueryParameters()); - } - - public Document URLGetConstraintQuery(String url, String query, ConstraintQueryParameters params) - throws IOException, XPathExpressionException, - SAXException, ParserConfigurationException - { - CheckURL(url); - if (null == query || query.length() < 2) - throw new IllegalArgumentException("Invalid constraint query specified"); - - params.setUrl(url); - params.setCQuery(query); - - return POST("URLGetConstraintQuery", "url", params); - } - - - public Document HTMLGetConstraintQuery(String html, String url, String query) - throws IOException, XPathExpressionException, - SAXException, ParserConfigurationException - { - return HTMLGetConstraintQuery(html, url, query, new ConstraintQueryParameters()); - } - - public Document HTMLGetConstraintQuery(String html, String url, String query, ConstraintQueryParameters params) - throws IOException, XPathExpressionException, - SAXException, ParserConfigurationException - { - CheckHTML(html, url); - if (null == query || query.length() < 2) - throw new IllegalArgumentException("Invalid constraint query specified"); - - params.setUrl(url); - params.setHtml(html); - params.setCQuery(query); - - return POST("HTMLGetConstraintQuery", "html", params); - } - - public Document URLGetTextSentiment(String url) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - return URLGetTextSentiment(url, new Parameters()); - } - - public Document URLGetTextSentiment(String url, Parameters params) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - CheckURL(url); - - params.setUrl(url); - - return GET("URLGetTextSentiment", "url", params); - } - - - public Document HTMLGetTextSentiment(String html, String url) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - return HTMLGetTextSentiment(html, url, new Parameters()); - } - - public Document HTMLGetTextSentiment(String html, String url, Parameters params) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - CheckHTML(html, url); + public Document urlGetFeedLinks(final URL url) { + return urlGetFeedLinks(url, new Parameters()); + } + + public Document urlGetFeedLinks(final URL url, final Parameters params) { + params.setUrl(url); + return get("URLGetFeedLinks", "url", params); + } + + public Document htmlGetFeedLinks(final String html, final URL url) { + return htmlGetFeedLinks(html, url, new Parameters()); + } + public Document htmlGetFeedLinks(final String html, final URL url, final Parameters params) { params.setUrl(url); params.setHtml(html); - - return POST("HTMLGetTextSentiment", "html", params); - } - - public Document TextGetTextSentiment(String text) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - return TextGetTextSentiment(text, new Parameters()); - } - - public Document TextGetTextSentiment(String text, Parameters params) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - CheckText(text); - + return post("HTMLGetFeedLinks", "html", params); + } + + public Document urlGetMicroformats(final URL url) { + return urlGetMicroformats(url, new Parameters()); + } + + public Document urlGetMicroformats(final URL url, final Parameters params) { + params.setUrl(url); + return get("URLGetMicroformatData", "url", params); + } + + public Document htmlGetMicroformats(final String html, final URL url) { + return htmlGetMicroformats(html, url, new Parameters()); + } + + public Document htmlGetMicroformats(final String html, final URL url, final Parameters params) { + params.setUrl(url); + params.setHtml(html); + return post("HTMLGetMicroformatData", "html", params); + } + + public Document urlGetConstraintQuery(final URL url, final String query) { + return urlGetConstraintQuery(url, query, new ConstraintQueryParameters()); + } + + public Document urlGetConstraintQuery(final URL url, final String query, final ConstraintQueryParameters params) { + if(trimToEmpty(query).length() < 2) { + throw new AlchemyApiException("Constraint query must be at least 2 characters long"); + } + params.setUrl(url); + params.setCQuery(query); + return post("URLGetConstraintQuery", "url", params); + } + + + public Document htmlGetConstraintQuery(final String html, final URL url, final String query) { + return htmlGetConstraintQuery(html, url, query, new ConstraintQueryParameters()); + } + + public Document htmlGetConstraintQuery(final String html, URL url, final String query, final ConstraintQueryParameters params) { + if(trimToEmpty(query).length() < 2) { + throw new AlchemyApiException("Constraint query must be at least 2 characters long"); + } + params.setUrl(url); + params.setHtml(html); + params.setCQuery(query); + return post("HTMLGetConstraintQuery", "html", params); + } + + public Document urlGetTextSentiment(final URL url) { + return urlGetTextSentiment(url, new Parameters()); + } + + public Document urlGetTextSentiment(final URL url, final Parameters params) { + params.setUrl(url); + return get("URLGetTextSentiment", "url", params); + } + + public Document htmlGetTextSentiment(final String html, final URL url) { + return htmlGetTextSentiment(html, url, new Parameters()); + } + + public Document htmlGetTextSentiment(final String html, URL url, final Parameters params) { + params.setUrl(url); + params.setHtml(html); + return post("HTMLGetTextSentiment", "html", params); + } + + public Document textGetTextSentiment(final String text) { + return textGetTextSentiment(text, new Parameters()); + } + + public Document textGetTextSentiment(final String text, final Parameters params) { params.setText(text); - - return POST("TextGetTextSentiment", "text", params); - } - - //------------------ - - public Document URLGetTargetedSentiment(String url, String target) - throws IOException, SAXException, ParserConfigurationException, - XPathExpressionException - { - return URLGetTargetedSentiment(url, target, - new TargetedSentimentParameters()); - } - - public Document URLGetTargetedSentiment(String url, String target, - TargetedSentimentParameters params) - throws IOException, SAXException, ParserConfigurationException, - XPathExpressionException - { - CheckURL(url); - CheckText(target); + return post("TextGetTextSentiment", "text", params); + } + public Document urlGetTargetedSentiment(final URL url, final String target) { + return urlGetTargetedSentiment(url, target, new TargetedSentimentParameters()); + } + + public Document urlGetTargetedSentiment(final URL url, final String target, final TargetedSentimentParameters params) { params.setUrl(url); params.setTarget(target); + return get("URLGetTargetedSentiment", "url", params); + } - return GET("URLGetTargetedSentiment", "url", params); - } - - public Document HTMLGetTargetedSentiment(String html, String url, String target) - throws IOException, SAXException, ParserConfigurationException, - XPathExpressionException - { - return HTMLGetTargetedSentiment(html, url, target, - new TargetedSentimentParameters()); - } - - public Document HTMLGetTargetedSentiment(String html, String url, String target, - TargetedSentimentParameters params) - throws IOException, SAXException, ParserConfigurationException, - XPathExpressionException - { - CheckHTML(html, url); - CheckText(target); - + public Document htmlGetTargetedSentiment(final String html, final URL url, final String target) { + return htmlGetTargetedSentiment(html, url, target, new TargetedSentimentParameters()); + } + + public Document htmlGetTargetedSentiment(final String html, final URL url, final String target, final TargetedSentimentParameters params) { params.setHtml(html); params.setUrl(url); params.setTarget(target); + return post("HTMLGetTargetedSentiment", "html", params); + } - return POST("HTMLGetTargetedSentiment", "html", params); + public Document textGetTargetedSentiment(final String text, final String target) { + return textGetTargetedSentiment(text, target, new TargetedSentimentParameters()); + } + + public Document textGetTargetedSentiment(final String text, final String target, final TargetedSentimentParameters params) { + params.setText(text); + params.setTarget(target); + return post("TextGetTargetedSentiment", "text", params); } - - public Document TextGetTargetedSentiment(String text, String target) - throws IOException, SAXException, ParserConfigurationException, - XPathExpressionException - { - return TextGetTargetedSentiment(text, target, - new TargetedSentimentParameters()); + + public Document urlGetRelations(final URL url) { + return urlGetRelations(url, new RelationParameters()); } - public Document TextGetTargetedSentiment(String text, String target, - TargetedSentimentParameters params) - throws IOException, SAXException, ParserConfigurationException, - XPathExpressionException - { - CheckText(text); - CheckText(target); + public Document urlGetRelations(final URL url, final RelationParameters params) { + params.setUrl(url); + return get("URLGetRelations", "url", params); + } + public Document HTMLGetRelations(final String html, final URL url) { + return HTMLGetRelations(html, url, new RelationParameters()); + } + + public Document HTMLGetRelations(final String html, final URL url, final RelationParameters params) { + params.setUrl(url); + params.setHtml(html); + return post("HTMLGetRelations", "html", params); + } + + public Document textGetRelations(final String text) { + return textGetRelations(text, new RelationParameters()); + } + + public Document textGetRelations(final String text, final RelationParameters params) { params.setText(text); - params.setTarget(target); + return post("TextGetRelations", "text", params); + } - return POST("TextGetTargetedSentiment", "text", params); + public Document urlGetCombined(final URL url) { + final CombinedParameters params = new CombinedParameters(); + params.setExtractAll(); + return urlGetCombined(url, params); } - //------------------ - - public Document URLGetRelations(String url) throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - return URLGetRelations(url, new RelationParameters()); + public Document urlGetCombined(final URL url, final CombinedParameters params) { + params.setUrl(url); + return get("URLGetCombinedData", "url", params); } - - public Document URLGetRelations(String url, RelationParameters params) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - CheckURL(url); - params.setUrl(url); - - return GET("URLGetRelations", "url", params); - } + public Document textGetCombined(final String text) { + final CombinedParameters params = new CombinedParameters(); + params.setExtractAll(); + return textGetCombined(text, params); + } - public Document HTMLGetRelations(String html, String url) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - return HTMLGetRelations(html, url, new RelationParameters()); + public Document textGetCombined(final String text, final CombinedParameters params) { + params.setText(text); + return post("TextGetCombinedData", "text", params); } - - public Document HTMLGetRelations(String html, String url, RelationParameters params) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - CheckHTML(html, url); - - params.setUrl(url); - params.setHtml(html); - - return POST("HTMLGetRelations", "html", params); - } - - public Document TextGetRelations(String text) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - return TextGetRelations(text, new RelationParameters()); - } - - public Document TextGetRelations(String text, RelationParameters params) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - CheckText(text); - - params.setText(text); - - return POST("TextGetRelations", "text", params); - } - - //------------------ - - public Document URLGetCombined(String url) throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - CombinedParameters params = new CombinedParameters(); - params.setExtractAll(); - return URLGetCombined(url, params); - } - - public Document URLGetCombined(String url, CombinedParameters params) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - CheckURL(url); - - params.setUrl(url); - - return GET("URLGetCombinedData", "url", params); - } - - public Document TextGetCombined(String text) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - CombinedParameters params = new CombinedParameters(); - params.setExtractAll(); - return TextGetCombined(text, params); - } - - public Document TextGetCombined(String text, CombinedParameters params) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - CheckText(text); - - params.setText(text); - - return POST("TextGetCombinedData", "text", params); - } - - //------------------ - - public Document URLGetTaxonomy(String url) throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - return URLGetTaxonomy(url, new TaxonomyParameters()); - } - - public Document URLGetTaxonomy(String url, TaxonomyParameters params) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - CheckURL(url); - - params.setUrl(url); - - return GET("URLGetRankedTaxonomy", "url", params); - } - - public Document HTMLGetTaxonomy(String html, String url) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - return HTMLGetTaxonomy(html, url, new TaxonomyParameters()); - } - - public Document HTMLGetTaxonomy(String html, String url, TaxonomyParameters params) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - CheckHTML(html, url); - - params.setUrl(url); - params.setHtml(html); - - return POST("HTMLGetRankedTaxonomy", "html", params); - } - - public Document TextGetTaxonomy(String text) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - return TextGetTaxonomy(text, new TaxonomyParameters()); - } - - public Document TextGetTaxonomy(String text, TaxonomyParameters params) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - CheckText(text); - - params.setText(text); - - return POST("TextGetRankedTaxonomy", "text", params); - } - - //------------------ - - public Document URLGetImage(String url) throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - return URLGetImage(url, new ImageParameters()); - } - - public Document URLGetImage(String url, ImageParameters params) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - CheckURL(url); - - params.setUrl(url); - - return GET("URLGetImage", "url", params); - } - - public Document URLGetRankedImageKeywords(String url) throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - return URLGetRankedImageKeywords(url, new ImageParameters()); - } - - public Document URLGetRankedImageKeywords(String url, ImageParameters params) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - CheckURL(url); + public Document urlGetTaxonomy(final URL url) { + return urlGetTaxonomy(url, new TaxonomyParameters()); + } + + public Document urlGetTaxonomy(final URL url, final TaxonomyParameters params) { params.setUrl(url); - - return GET("URLGetRankedImageKeywords", "url", params); + return get("URLGetRankedTaxonomy", "url", params); } - public Document ImageGetRankedImageKeywords(ImageParameters params) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - URL url = new URL(_requestUri + "image/ImageGetRankedImageKeywords?" + - "apikey=" + this._apiKey + params.getParameterString()); - System.out.println(url.toString()); + public Document htmlGetTaxonomy(final String html, final URL url) { + return htmlGetTaxonomy(html, url, new TaxonomyParameters()); + } - HttpURLConnection handle = (HttpURLConnection) url.openConnection(); - handle.setDoOutput(true); + public Document htmlGetTaxonomy(final String html, final URL url, final TaxonomyParameters params) { + params.setUrl(url); + params.setHtml(html); + return post("HTMLGetRankedTaxonomy", "html", params); + } - byte[] image = params.getImage(); - handle.addRequestProperty("Content-Length", Integer.toString(image.length)); + public Document textGetTaxonomy(final String text) { + return textGetTaxonomy(text, new TaxonomyParameters()); + } - DataOutputStream ostream = new DataOutputStream(handle.getOutputStream()); - ostream.write(image); - ostream.close(); + public Document textGetTaxonomy(final String text, final TaxonomyParameters params) { + params.setText(text); + return post("TextGetRankedTaxonomy", "text", params); + } - return doRequest(handle, params.getOutputMode()); + public Document urlGetImage(final URL url) { + return urlGetImage(url, new ImageParameters()); } - private void CheckHTML(String html, String url) { - if (null == html || html.length() < 5) - throw new IllegalArgumentException("Enter a HTML document to analyze."); + public Document urlGetImage(final URL url, final ImageParameters params) { + params.setUrl(url); + return get("URLGetImage", "url", params); + } - if (null == url || url.length() < 10) - throw new IllegalArgumentException("Enter an URL to analyze."); + public Document urlGetRankedImageKeywords(final URL url) { + return urlGetRankedImageKeywords(url, new ImageParameters()); } - private void CheckText(String text) { - if (null == text ) - throw new IllegalArgumentException("Enter some text to analyze."); + public Document urlGetRankedImageKeywords(final URL url, final ImageParameters params) { + params.setUrl(url); + return get("URLGetRankedImageKeywords", "url", params); } - private void CheckURL(String url) { - if (null == url || url.length() < 10) - throw new IllegalArgumentException("Enter an URL to analyze."); + public Document imageGetRankedImageKeywords(final ImageParameters params) { + try { + final String urlQuery = "?apikey=" + this.configuration.getApiKey() + params.getUrlQuery(); + final URL url = new URL(buildBaseApiUrl() + "image/ImageGetRankedImageKeywords" + urlQuery); + + final HttpURLConnection handle = (HttpURLConnection) url.openConnection(); + handle.setDoOutput(true); + + final byte[] image = params.getImage(); + handle.addRequestProperty("Content-Length", Integer.toString(image.length)); + + final DataOutputStream outputStream = new DataOutputStream(handle.getOutputStream()); + outputStream.write(image); + outputStream.close(); + + return doRequest(handle, params.getOutputMode()); + + } catch(IOException e) { + throw new AlchemyApiException(e); + } } - - private Document GET(String callName, String callPrefix, Parameters 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, Parameters params) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - URL url = new URL(_requestUri + callPrefix + "/" + callName); + private Document get(final String callName, final String callPrefix, final Parameters params) { + try { + final String urlQuery = "?apikey=" + configuration.getApiKey() + params.getUrlQuery(); + final URL url = new URL(buildBaseApiUrl() + callPrefix + "/" + callName + urlQuery); - HttpURLConnection handle = (HttpURLConnection) url.openConnection(); - handle.setDoOutput(true); + final HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); + httpURLConnection.setDoOutput(true); - StringBuilder data = new StringBuilder(); + return doRequest(httpURLConnection, params.getOutputMode()); - data.append("apikey=").append(this._apiKey); - data.append(params.getParameterString()); + } catch(IOException e) { + throw new AlchemyApiException(e); + } + } + + private Document post(final String callName, final String callPrefix, final Parameters params) { + try { + final URL url = new URL(buildBaseApiUrl() + callPrefix + "/" + callName); + final String data = "apikey=" + configuration.getApiKey() + params.getUrlQuery(); - handle.addRequestProperty("Content-Length", Integer.toString(data.length())); + final HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); + httpURLConnection.setDoOutput(true); + httpURLConnection.addRequestProperty("Content-Length", Integer.toString(data.length())); - DataOutputStream ostream = new DataOutputStream(handle.getOutputStream()); - ostream.write(data.toString().getBytes()); - ostream.close(); + final DataOutputStream outputStream = new DataOutputStream(httpURLConnection.getOutputStream()); + outputStream.write(data.getBytes(Charset.forName("UTF-8"))); + outputStream.close(); - return doRequest(handle, params.getOutputMode()); + return doRequest(httpURLConnection, params.getOutputMode()); + + } catch(IOException e) { + throw new AlchemyApiException(e); + } } - // TODO currently hard-coded to handle xml, create xml/json adapter based on request + // TODO add json handling // TODO return pojo with parsed field, but allow a "raw" xml/json getter to protect against api updates - 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(Parameters.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(Parameters.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 + '.'); - } + private Document doRequest(final HttpURLConnection httpURLConnection, final String outputMode) { + try { + final DataInputStream inputStream = new DataInputStream(httpURLConnection.getInputStream()); + final Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(inputStream); + + inputStream.close(); + httpURLConnection.disconnect(); + + switch (outputMode) { + case Parameters.OUTPUT_XML: + return parseXml(document); + + case Parameters.OUTPUT_RDF: + return praseRdf(document); + + case Parameters.OUTPUT_JSON: + throw new AlchemyApiException("Json Response not supported yet"); + } + return document; + + } catch (SAXException | ParserConfigurationException | IOException e) { + throw new AlchemyApiException(e); } + } - return doc; + private Document parseXml(final Document document) { + final XPathFactory factory = XPathFactory.newInstance(); + final String status = getNodeValue(factory, document, "/results/status/text()"); + if (isBlank(status) || !status.equals("OK")) { + final String statusInfo = getNodeValue(factory, document, "/results/statusInfo/text()"); + if (isNotBlank(statusInfo)) { + throw new AlchemyApiException("Error making API call: " + statusInfo); + } + throw new AlchemyApiException("Error making API call: " + status); + } + return document; + } + + private Document praseRdf(final Document document) { + final XPathFactory factory = XPathFactory.newInstance(); + final String status = getNodeValue(factory, document, "//RDF/Description/ResultStatus/text()"); + if (isBlank(status) || !status.equals("OK")) { + final String statusInfo = getNodeValue(factory, document, "//RDF/Description/ResultStatus/text()"); + if (isNotBlank(statusInfo)) { + throw new AlchemyApiException("Error making API call: " + statusInfo); + } + throw new AlchemyApiException("Error making API call: " + status); + } + return document; } - 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; + private String getNodeValue(XPathFactory factory, Document doc, String xpathStr) { + try { + final XPath xpath = factory.newXPath(); + final XPathExpression expr = xpath.compile(xpathStr); + final Object result = expr.evaluate(doc, XPathConstants.NODESET); + final NodeList results = (NodeList) result; - if (results.getLength() > 0 && null != results.item(0)) + if(results.getLength() == 0 || results.item(0) == null) { return null; } return results.item(0).getNodeValue(); - return null; + } catch (XPathExpressionException e) { + throw new AlchemyApiException(e); + } + } + + private String buildBaseApiUrl() { + return API_URL.replace("{SUB_DOMAIN}", configuration.getApiSubDomain()); } + } diff --git a/src/main/java/com/alchemyapi/api/AlchemyApiConfiguration.java b/src/main/java/com/alchemyapi/api/AlchemyApiConfiguration.java new file mode 100644 index 0000000..053729c --- /dev/null +++ b/src/main/java/com/alchemyapi/api/AlchemyApiConfiguration.java @@ -0,0 +1,40 @@ +package com.alchemyapi.api; + +/** + * Created by kenny + */ +public class AlchemyApiConfiguration { + + private String apiKey; + + private String apiSubDomain = "access"; + + public AlchemyApiConfiguration(final String apiKey) { + this.apiKey = apiKey; + } + + public String getApiKey() { + return apiKey; + } + + public void setApiKey(final String apiKey) { + this.apiKey = apiKey; + } + + public String getApiSubDomain() { + return apiSubDomain; + } + + public void setApiSubDomain(final String apiSubDomain) { + this.apiSubDomain = apiSubDomain; + } + + @Override + public String toString() { + return "AlchemyApiConfiguration{" + + "apiKey='" + apiKey + '\'' + + ", apiSubDomain='" + apiSubDomain + '\'' + + '}'; + } + +} diff --git a/src/main/java/com/alchemyapi/api/exceptions/AlchemyApiException.java b/src/main/java/com/alchemyapi/api/exceptions/AlchemyApiException.java new file mode 100644 index 0000000..4d96ae8 --- /dev/null +++ b/src/main/java/com/alchemyapi/api/exceptions/AlchemyApiException.java @@ -0,0 +1,16 @@ +package com.alchemyapi.api.exceptions; + +/** + * Created by kenny + */ +public class AlchemyApiException extends RuntimeException { + + public AlchemyApiException(final String message) { + super(message); + } + + public AlchemyApiException(final Exception e) { + super(e.getMessage(), e); + } + +} diff --git a/src/main/java/com/alchemyapi/api/parameters/CategoryParameters.java b/src/main/java/com/alchemyapi/api/parameters/CategoryParameters.java index 4051e79..14235c9 100644 --- a/src/main/java/com/alchemyapi/api/parameters/CategoryParameters.java +++ b/src/main/java/com/alchemyapi/api/parameters/CategoryParameters.java @@ -50,8 +50,8 @@ public void setBaseUrl(String baseUrl) { this.baseUrl = baseUrl; } - public String getParameterString() { - String retString = super.getParameterString(); + public String getUrlQuery() { + String retString = super.getUrlQuery(); try { if (sourceText != null) retString += "&sourceText=" + sourceText; if (cQuery != null) retString += "&cquery=" + URLEncoder.encode(cQuery, "UTF-8"); diff --git a/src/main/java/com/alchemyapi/api/parameters/CombinedParameters.java b/src/main/java/com/alchemyapi/api/parameters/CombinedParameters.java index edc1db2..d63f4e5 100644 --- a/src/main/java/com/alchemyapi/api/parameters/CombinedParameters.java +++ b/src/main/java/com/alchemyapi/api/parameters/CombinedParameters.java @@ -160,8 +160,8 @@ public void setExtract(String extractArg) { this.extract += "," + extractArg; } - public String getParameterString() { - String retString = super.getParameterString(); + public String getUrlQuery() { + String retString = super.getUrlQuery(); try { if (extractMode != null) retString += "&extractMode=" + extractMode; if (extract != null) retString += "&extract=" + extract; diff --git a/src/main/java/com/alchemyapi/api/parameters/ConceptParameters.java b/src/main/java/com/alchemyapi/api/parameters/ConceptParameters.java index 0de4f44..1bd19a4 100644 --- a/src/main/java/com/alchemyapi/api/parameters/ConceptParameters.java +++ b/src/main/java/com/alchemyapi/api/parameters/ConceptParameters.java @@ -71,8 +71,8 @@ public void setLinkedData(boolean linkedData) { this.linkedData = linkedData; } - public String getParameterString() { - String retString = super.getParameterString(); + public String getUrlQuery() { + String retString = super.getUrlQuery(); try { if (sourceText != null) retString += "&sourceText=" + sourceText; if (showSourceText != null) retString += "&showSourceText=" + (showSourceText ? "1" : "0"); diff --git a/src/main/java/com/alchemyapi/api/parameters/ConstraintQueryParameters.java b/src/main/java/com/alchemyapi/api/parameters/ConstraintQueryParameters.java index 20442ad..792ff47 100644 --- a/src/main/java/com/alchemyapi/api/parameters/ConstraintQueryParameters.java +++ b/src/main/java/com/alchemyapi/api/parameters/ConstraintQueryParameters.java @@ -17,8 +17,8 @@ public void setCQuery(String cQuery) { } - public String getParameterString() { - String retString = super.getParameterString(); + public String getUrlQuery() { + String retString = super.getUrlQuery(); try { if (cQuery != null) retString += "&cquery=" + URLEncoder.encode(cQuery, "UTF-8"); } catch (UnsupportedEncodingException e) { diff --git a/src/main/java/com/alchemyapi/api/parameters/ImageParameters.java b/src/main/java/com/alchemyapi/api/parameters/ImageParameters.java index b0dfdab..f5028a0 100644 --- a/src/main/java/com/alchemyapi/api/parameters/ImageParameters.java +++ b/src/main/java/com/alchemyapi/api/parameters/ImageParameters.java @@ -64,8 +64,8 @@ public void setBaseUrl(String baseUrl) { this.baseUrl = baseUrl; } - public String getParameterString() { - String retString = super.getParameterString(); + public String getUrlQuery() { + String retString = super.getUrlQuery(); try { if (cQuery != null) retString += "&cquery=" + URLEncoder.encode(cQuery, "UTF-8"); if (xPath != null) retString += "&xpath=" + URLEncoder.encode(xPath, "UTF-8"); diff --git a/src/main/java/com/alchemyapi/api/parameters/KeywordParameters.java b/src/main/java/com/alchemyapi/api/parameters/KeywordParameters.java index 5ffe604..c5fdb04 100644 --- a/src/main/java/com/alchemyapi/api/parameters/KeywordParameters.java +++ b/src/main/java/com/alchemyapi/api/parameters/KeywordParameters.java @@ -94,8 +94,8 @@ public void setBaseUrl(String baseUrl) { this.baseUrl = baseUrl; } - public String getParameterString() { - String retString = super.getParameterString(); + public String getUrlQuery() { + String retString = super.getUrlQuery(); try { if (sourceText != null) retString += "&sourceText=" + sourceText; if (showSourceText != null) retString += "&showSourceText=" + (showSourceText ? "1" : "0"); diff --git a/src/main/java/com/alchemyapi/api/parameters/LanguageParameters.java b/src/main/java/com/alchemyapi/api/parameters/LanguageParameters.java index d947d48..84a050f 100644 --- a/src/main/java/com/alchemyapi/api/parameters/LanguageParameters.java +++ b/src/main/java/com/alchemyapi/api/parameters/LanguageParameters.java @@ -43,8 +43,8 @@ public void setXPath(String xPath) { this.xPath = xPath; } - public String getParameterString() { - String retString = super.getParameterString(); + public String getUrlQuery() { + String retString = super.getUrlQuery(); try { if (sourceText != null) retString += "&sourceText=" + sourceText; if (cQuery != null) retString += "&cquery=" + URLEncoder.encode(cQuery, "UTF-8"); diff --git a/src/main/java/com/alchemyapi/api/parameters/NamedEntityParameters.java b/src/main/java/com/alchemyapi/api/parameters/NamedEntityParameters.java index 7613333..4c73b3b 100644 --- a/src/main/java/com/alchemyapi/api/parameters/NamedEntityParameters.java +++ b/src/main/java/com/alchemyapi/api/parameters/NamedEntityParameters.java @@ -116,8 +116,8 @@ public void setSentiment(boolean sentiment) { this.sentiment = sentiment; } - public String getParameterString() { - String retString = super.getParameterString(); + public String getUrlQuery() { + String retString = super.getUrlQuery(); try { if (disambiguate != null) retString += "&disambiguate=" + (disambiguate ? "1" : "0"); if (linkedData != null) retString += "&linkedData=" + (linkedData ? "1" : "0"); diff --git a/src/main/java/com/alchemyapi/api/parameters/Parameters.java b/src/main/java/com/alchemyapi/api/parameters/Parameters.java index 8b25214..3415cae 100644 --- a/src/main/java/com/alchemyapi/api/parameters/Parameters.java +++ b/src/main/java/com/alchemyapi/api/parameters/Parameters.java @@ -1,6 +1,7 @@ package com.alchemyapi.api.parameters; import java.io.UnsupportedEncodingException; +import java.net.URL; import java.net.URLEncoder; @@ -9,17 +10,17 @@ public class Parameters { public static final String OUTPUT_XML = "xml"; public static final String OUTPUT_RDF = "rdf"; - private String url; + private URL url; private String html; private String text; private String outputMode = OUTPUT_XML; // TODO make json default private String customParameters; - public String getUrl() { + public URL getUrl() { return url; } - public void setUrl(String url) { + public void setUrl(URL url) { this.url = url; } @@ -69,10 +70,10 @@ public void setCustomParameters(String... customParameters) { this.customParameters = data.toString(); } - public String getParameterString() { + public String getUrlQuery() { String retString = ""; try { - if (url != null) retString += "&url=" + URLEncoder.encode(url, "UTF-8"); + if (url != null) retString += "&url=" + URLEncoder.encode(url.toString(), "UTF-8"); if (html != null) retString += "&html=" + URLEncoder.encode(html, "UTF-8"); if (text != null) retString += "&text=" + URLEncoder.encode(text, "UTF-8"); if (customParameters != null) retString += customParameters; diff --git a/src/main/java/com/alchemyapi/api/parameters/RelationParameters.java b/src/main/java/com/alchemyapi/api/parameters/RelationParameters.java index 8a5cb04..359c140 100644 --- a/src/main/java/com/alchemyapi/api/parameters/RelationParameters.java +++ b/src/main/java/com/alchemyapi/api/parameters/RelationParameters.java @@ -134,8 +134,8 @@ public void setSentiment(boolean sentiment) { this.sentiment = sentiment; } - public String getParameterString() { - String retString = super.getParameterString(); + public String getUrlQuery() { + String retString = super.getUrlQuery(); try { if (disambiguate != null) retString += "&disambiguate=" + (disambiguate ? "1" : "0"); if (linkedData != null) retString += "&linkedData=" + (linkedData ? "1" : "0"); diff --git a/src/main/java/com/alchemyapi/api/parameters/TargetedSentimentParameters.java b/src/main/java/com/alchemyapi/api/parameters/TargetedSentimentParameters.java index 6a07adf..1b5a1da 100644 --- a/src/main/java/com/alchemyapi/api/parameters/TargetedSentimentParameters.java +++ b/src/main/java/com/alchemyapi/api/parameters/TargetedSentimentParameters.java @@ -24,8 +24,8 @@ public void setTarget(String target) { this.target = target; } - public String getParameterString() { - String retString = super.getParameterString(); + public String getUrlQuery() { + String retString = super.getUrlQuery(); try { if (showSourceText != null) retString += "&showSourceText=" + (showSourceText ? "1" : "0"); if (target != null) retString += "&target=" + URLEncoder.encode(target, "UTF-8"); diff --git a/src/main/java/com/alchemyapi/api/parameters/TaxonomyParameters.java b/src/main/java/com/alchemyapi/api/parameters/TaxonomyParameters.java index c7ee116..7a28f2e 100644 --- a/src/main/java/com/alchemyapi/api/parameters/TaxonomyParameters.java +++ b/src/main/java/com/alchemyapi/api/parameters/TaxonomyParameters.java @@ -116,8 +116,8 @@ public void setSentiment(boolean sentiment) { this.sentiment = sentiment; } - public String getParameterString() { - String retString = super.getParameterString(); + public String getUrlQuery() { + String retString = super.getUrlQuery(); try { if (disambiguate != null) retString += "&disambiguate=" + (disambiguate ? "1" : "0"); if (linkedData != null) retString += "&linkedData=" + (linkedData ? "1" : "0"); diff --git a/src/main/java/com/alchemyapi/api/parameters/TextParameters.java b/src/main/java/com/alchemyapi/api/parameters/TextParameters.java index a6511cc..ec38ffd 100644 --- a/src/main/java/com/alchemyapi/api/parameters/TextParameters.java +++ b/src/main/java/com/alchemyapi/api/parameters/TextParameters.java @@ -21,8 +21,8 @@ public void setExtractLinks(boolean extractLinks) { this.extractLinks = extractLinks; } - public String getParameterString() { - String retString = super.getParameterString(); + public String getUrlQuery() { + String retString = super.getUrlQuery(); if (useMetaData != null) retString += "&useMetaData=" + (useMetaData ? "1" : "0"); if (extractLinks != null) retString += "&extractLinks=" + (extractLinks ? "1" : "0"); diff --git a/src/test/java/com/alchemyapi/STestAuthor.java b/src/test/java/com/alchemyapi/STestAuthor.java index 6944075..21657c9 100644 --- a/src/test/java/com/alchemyapi/STestAuthor.java +++ b/src/test/java/com/alchemyapi/STestAuthor.java @@ -1,31 +1,34 @@ package com.alchemyapi; -import com.alchemyapi.api.AlchemyAPI; -import com.alchemyapi.util.DocumentUtils; -import com.alchemyapi.util.ResourceUtils; +import com.alchemyapi.api.AlchemyApi; +import com.alchemyapi.helpers.TestApiFactory; +import com.alchemyapi.helpers.DocumentUtils; +import com.alchemyapi.helpers.ResourceUtils; import org.junit.Test; import org.w3c.dom.Document; import org.xml.sax.SAXException; import javax.xml.parsers.ParserConfigurationException; import javax.xml.xpath.XPathExpressionException; +import java.io.File; import java.io.IOException; +import java.net.URL; public class STestAuthor { - private final AlchemyAPI alchemyAPI = AlchemyAPI.GetInstanceFromString(ResourceUtils.toString("api_key.txt")); + private final AlchemyApi alchemyApi = TestApiFactory.build(new File(System.getProperty("user.home"), ".alchemy/api.key")); @Test public void parseFromTestData() throws SAXException, ParserConfigurationException, XPathExpressionException, IOException { final String html = ResourceUtils.toString("data/example.html"); - final Document document = alchemyAPI.HTMLGetAuthor(html, "http://www.test.com/"); + final Document document = alchemyApi.htmlGetAuthor(html, new URL("http://www.test.com/")); System.out.println(DocumentUtils.toString(document)); } @Test public void parseFromUrl() throws SAXException, ParserConfigurationException, XPathExpressionException, IOException { - final Document document = alchemyAPI.URLGetAuthor("http://www.politico.com/blogs/media/2012/02/detroit-news-ed-upset-over-romney-edit-115247.html"); + final Document document = alchemyApi.urlGetAuthor(new URL("http://www.politico.com/blogs/media/2012/02/detroit-news-ed-upset-over-romney-edit-115247.html")); System.out.println(DocumentUtils.toString(document)); } diff --git a/src/test/java/com/alchemyapi/STestSentiment.java b/src/test/java/com/alchemyapi/STestSentiment.java index 5ffceb0..544c95d 100644 --- a/src/test/java/com/alchemyapi/STestSentiment.java +++ b/src/test/java/com/alchemyapi/STestSentiment.java @@ -1,40 +1,42 @@ package com.alchemyapi; -import com.alchemyapi.api.AlchemyAPI; +import com.alchemyapi.api.AlchemyApi; import com.alchemyapi.api.parameters.KeywordParameters; import com.alchemyapi.api.parameters.NamedEntityParameters; import com.alchemyapi.api.parameters.TargetedSentimentParameters; -import com.alchemyapi.util.DocumentUtils; -import com.alchemyapi.util.ResourceUtils; +import com.alchemyapi.helpers.DocumentUtils; +import com.alchemyapi.helpers.ResourceUtils; +import com.alchemyapi.helpers.TestApiFactory; import org.junit.Test; import org.w3c.dom.Document; import org.xml.sax.SAXException; import javax.xml.parsers.ParserConfigurationException; import javax.xml.xpath.XPathExpressionException; +import java.io.File; import java.io.IOException; +import java.net.URL; public class STestSentiment { - private final AlchemyAPI alchemyAPI = AlchemyAPI.GetInstanceFromString(ResourceUtils.toString("api_key.txt")); + private final AlchemyApi alchemyApi = TestApiFactory.build(new File(System.getProperty("user.home"), ".alchemy/api.key")); @Test public void url() throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { - final Document document = alchemyAPI.URLGetTextSentiment("http://www.techcrunch.com/"); + final Document document = alchemyApi.urlGetTextSentiment(new URL("http://www.techcrunch.com/")); System.out.println(DocumentUtils.toString(document)); } @Test public void text() throws SAXException, ParserConfigurationException, XPathExpressionException, IOException { - final Document document = alchemyAPI.TextGetTextSentiment("That hat is ridiculous, Charles."); + final Document document = alchemyApi.textGetTextSentiment("That hat is ridiculous, Charles."); System.out.println(DocumentUtils.toString(document)); } @Test public void file() throws SAXException, ParserConfigurationException, XPathExpressionException, IOException { final String html = ResourceUtils.toString("data/example.html"); - - final Document document = alchemyAPI.HTMLGetTextSentiment(html, "http://www.test.com/"); + final Document document = alchemyApi.htmlGetTextSentiment(html, new URL("http://www.test.com/")); System.out.println(DocumentUtils.toString(document)); } @@ -42,17 +44,17 @@ public void file() throws SAXException, ParserConfigurationException, XPathExpre public void entityTargetedSentimentText() throws SAXException, ParserConfigurationException, XPathExpressionException, IOException { final NamedEntityParameters entityParams = new NamedEntityParameters(); entityParams.setSentiment(true); - final Document document = alchemyAPI.TextGetRankedNamedEntities("That Mike Tyson is such a sweetheart.", entityParams); + final Document document = alchemyApi.textGetRankedNamedEntities("That Mike Tyson is such a sweetheart.", entityParams); System.out.println(DocumentUtils.toString(document)); final KeywordParameters keywordParams = new KeywordParameters(); keywordParams.setSentiment(true); - final Document document2 = alchemyAPI.TextGetRankedKeywords("That Mike Tyson is such a sweetheart.", keywordParams); + final Document document2 = alchemyApi.textGetRankedKeywords("That Mike Tyson is such a sweetheart.", keywordParams); System.out.println(DocumentUtils.toString(document2)); final TargetedSentimentParameters sentimentParams = new TargetedSentimentParameters(); sentimentParams.setShowSourceText(true); - final Document document3 = alchemyAPI.TextGetTargetedSentiment("This car is terrible.", "car", sentimentParams); + final Document document3 = alchemyApi.textGetTargetedSentiment("This car is terrible.", "car", sentimentParams); System.out.print(DocumentUtils.toString(document3)); } @@ -61,7 +63,10 @@ public void entityTargetedSentimentUrl() throws SAXException, ParserConfiguratio final TargetedSentimentParameters sentimentParams = new TargetedSentimentParameters(); sentimentParams.setShowSourceText(true); - final Document document = alchemyAPI.URLGetTargetedSentiment("http://techcrunch.com/2012/03/01/keen-on-anand-rajaraman-how-walmart-wants-to-leapfrog-over-amazon-tctv/", "Walmart", sentimentParams); + final Document document = alchemyApi.urlGetTargetedSentiment( + new URL("http://techcrunch.com/2012/03/01/keen-on-anand-rajaraman-how-walmart-wants-to-leapfrog-over-amazon-tctv/"), + "Walmart", + sentimentParams); System.out.print(DocumentUtils.toString(document)); } @@ -71,9 +76,8 @@ public void entityTargetedSentimentHtml() throws SAXException, ParserConfigurati sentimentParams.setShowSourceText(true); final String html = ResourceUtils.toString("data/example.html"); - final Document document2 = alchemyAPI.HTMLGetTargetedSentiment(html, "http://www.test.com/", "WujWuj", sentimentParams); + final Document document2 = alchemyApi.htmlGetTargetedSentiment(html, new URL("http://www.test.com/"), "WujWuj", sentimentParams); System.out.print(DocumentUtils.toString(document2)); } - } diff --git a/src/test/java/com/alchemyapi/util/DocumentUtils.java b/src/test/java/com/alchemyapi/helpers/DocumentUtils.java similarity index 96% rename from src/test/java/com/alchemyapi/util/DocumentUtils.java rename to src/test/java/com/alchemyapi/helpers/DocumentUtils.java index 77234d4..0493f7b 100644 --- a/src/test/java/com/alchemyapi/util/DocumentUtils.java +++ b/src/test/java/com/alchemyapi/helpers/DocumentUtils.java @@ -1,4 +1,4 @@ -package com.alchemyapi.util; +package com.alchemyapi.helpers; import org.w3c.dom.Document; diff --git a/src/test/java/com/alchemyapi/util/ResourceUtils.java b/src/test/java/com/alchemyapi/helpers/ResourceUtils.java similarity index 94% rename from src/test/java/com/alchemyapi/util/ResourceUtils.java rename to src/test/java/com/alchemyapi/helpers/ResourceUtils.java index c278220..2aff5ef 100644 --- a/src/test/java/com/alchemyapi/util/ResourceUtils.java +++ b/src/test/java/com/alchemyapi/helpers/ResourceUtils.java @@ -1,4 +1,4 @@ -package com.alchemyapi.util; +package com.alchemyapi.helpers; import org.apache.commons.io.IOUtils; diff --git a/src/test/java/com/alchemyapi/helpers/TestApiFactory.java b/src/test/java/com/alchemyapi/helpers/TestApiFactory.java new file mode 100644 index 0000000..43af2ad --- /dev/null +++ b/src/test/java/com/alchemyapi/helpers/TestApiFactory.java @@ -0,0 +1,31 @@ +package com.alchemyapi.helpers; + +import com.alchemyapi.api.AlchemyApi; +import com.alchemyapi.api.AlchemyApiConfiguration; +import com.alchemyapi.api.exceptions.AlchemyApiException; +import org.apache.commons.io.IOUtils; + +import java.io.File; +import java.io.FileReader; +import java.io.IOException; + +import static org.apache.commons.lang3.StringUtils.trimToEmpty; + +/** + * Created by kenny + */ +public class TestApiFactory { + + // load configuration file from external file + public static AlchemyApi build(final File configurationFile) { + try { + final String apiKey = trimToEmpty(IOUtils.toString(new FileReader(configurationFile))); + final AlchemyApiConfiguration configuration = new AlchemyApiConfiguration(apiKey); + return new AlchemyApi(configuration); + + } catch (IOException e) { + throw new AlchemyApiException(e); + } + } + +} diff --git a/src/test/resources/api_key.txt b/src/test/resources/api_key.txt deleted file mode 100644 index 3950f0b..0000000 --- a/src/test/resources/api_key.txt +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/src/test/resources/log4j.xml b/src/test/resources/log4j.xml new file mode 100644 index 0000000..8484f4e --- /dev/null +++ b/src/test/resources/log4j.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/test/java/com/alchemyapi/CategoryTest.java b/src/test/resources/to_fix/CategoryTest.java similarity index 96% rename from src/test/java/com/alchemyapi/CategoryTest.java rename to src/test/resources/to_fix/CategoryTest.java index 4fa24a1..b7b3ff1 100644 --- a/src/test/java/com/alchemyapi/CategoryTest.java +++ b/src/test/resources/to_fix/CategoryTest.java @@ -1,6 +1,6 @@ -package com.alchemyapi; +package to_fix; -import com.alchemyapi.api.AlchemyAPI; +import com.alchemyapi.api.AlchemyApi; import com.alchemyapi.api.parameters.CategoryParameters; import com.alchemyapi.api.parameters.Parameters; import org.w3c.dom.Document; @@ -26,7 +26,7 @@ public static void main(String[] args) ParserConfigurationException, XPathExpressionException { // Create an AlchemyAPI object. - AlchemyAPI alchemyObj = AlchemyAPI.GetInstanceFromFile("api_key.txt"); + AlchemyApi alchemyObj = AlchemyApi.GetInstanceFromFile("api_key.txt"); // Categorize a web URL by topic. Document doc = alchemyObj.URLGetCategory("http://www.techcrunch.com/"); diff --git a/src/test/java/com/alchemyapi/CombinedTest.java b/src/test/resources/to_fix/CombinedTest.java similarity index 96% rename from src/test/java/com/alchemyapi/CombinedTest.java rename to src/test/resources/to_fix/CombinedTest.java index 3c92b15..d2e274e 100644 --- a/src/test/java/com/alchemyapi/CombinedTest.java +++ b/src/test/resources/to_fix/CombinedTest.java @@ -1,4 +1,4 @@ -package com.alchemyapi; +package to_fix; import com.alchemyapi.api.*; @@ -20,7 +20,7 @@ public static void main(String[] args) ParserConfigurationException, XPathExpressionException { // Create an AlchemyAPI object. - AlchemyAPI alchemyObj = AlchemyAPI.GetInstanceFromFile("api_key.txt"); + AlchemyApi alchemyObj = AlchemyApi.GetInstanceFromFile("api_key.txt"); // Extract combined data for a web URL. Document doc = alchemyObj.URLGetCombined("http://www.techcrunch.com/"); diff --git a/src/test/java/com/alchemyapi/ConceptTest.java b/src/test/resources/to_fix/ConceptTest.java similarity index 95% rename from src/test/java/com/alchemyapi/ConceptTest.java rename to src/test/resources/to_fix/ConceptTest.java index 07800f5..1ad6a05 100644 --- a/src/test/java/com/alchemyapi/ConceptTest.java +++ b/src/test/resources/to_fix/ConceptTest.java @@ -1,6 +1,6 @@ -package com.alchemyapi; +package to_fix; -import com.alchemyapi.api.AlchemyAPI; +import com.alchemyapi.api.AlchemyApi; import org.xml.sax.SAXException; import org.w3c.dom.Document; @@ -17,7 +17,7 @@ class ConceptTest { public static void main(String[] args) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { // Create an AlchemyAPI object. - AlchemyAPI alchemyObj = AlchemyAPI.GetInstanceFromFile("api_key.txt"); + AlchemyApi alchemyObj = AlchemyApi.GetInstanceFromFile("api_key.txt"); // Extract concept tags for a web URL. Document doc = alchemyObj.URLGetRankedConcepts("http://www.techcrunch.com/"); diff --git a/src/test/java/com/alchemyapi/ConstraintQueryTest.java b/src/test/resources/to_fix/ConstraintQueryTest.java similarity index 94% rename from src/test/java/com/alchemyapi/ConstraintQueryTest.java rename to src/test/resources/to_fix/ConstraintQueryTest.java index fe41f06..6d2a9ed 100644 --- a/src/test/java/com/alchemyapi/ConstraintQueryTest.java +++ b/src/test/resources/to_fix/ConstraintQueryTest.java @@ -1,6 +1,6 @@ -package com.alchemyapi; +package to_fix; -import com.alchemyapi.api.AlchemyAPI; +import com.alchemyapi.api.AlchemyApi; import org.xml.sax.SAXException; import org.w3c.dom.Document; @@ -19,7 +19,7 @@ public static void main(String[] args) ParserConfigurationException, XPathExpressionException { // Create an AlchemyAPI object. - AlchemyAPI alchemyObj = AlchemyAPI.GetInstanceFromFile("api_key.txt"); + AlchemyApi alchemyObj = AlchemyApi.GetInstanceFromFile("api_key.txt"); // Extract first link from an URL. Document doc = alchemyObj.URLGetConstraintQuery("http://microformats.org/wiki/hcard", diff --git a/src/test/java/com/alchemyapi/EntityTest.java b/src/test/resources/to_fix/EntityTest.java similarity index 95% rename from src/test/java/com/alchemyapi/EntityTest.java rename to src/test/resources/to_fix/EntityTest.java index e53265f..0ba3975 100644 --- a/src/test/java/com/alchemyapi/EntityTest.java +++ b/src/test/resources/to_fix/EntityTest.java @@ -1,6 +1,6 @@ -package com.alchemyapi; +package to_fix; -import com.alchemyapi.api.AlchemyAPI; +import com.alchemyapi.api.AlchemyApi; import org.xml.sax.SAXException; import org.w3c.dom.Document; @@ -19,7 +19,7 @@ public static void main(String[] args) ParserConfigurationException, XPathExpressionException { // Create an AlchemyAPI object. - AlchemyAPI alchemyObj = AlchemyAPI.GetInstanceFromFile("api_key.txt"); + AlchemyApi alchemyObj = AlchemyApi.GetInstanceFromFile("api_key.txt"); // Extract a ranked list of named entities for a web URL. Document doc = alchemyObj.URLGetRankedNamedEntities("http://www.techcrunch.com/"); diff --git a/src/test/java/com/alchemyapi/FeedLinksTest.java b/src/test/resources/to_fix/FeedLinksTest.java similarity index 95% rename from src/test/java/com/alchemyapi/FeedLinksTest.java rename to src/test/resources/to_fix/FeedLinksTest.java index bf549cd..8409b87 100644 --- a/src/test/java/com/alchemyapi/FeedLinksTest.java +++ b/src/test/resources/to_fix/FeedLinksTest.java @@ -1,6 +1,6 @@ -package com.alchemyapi; +package to_fix; -import com.alchemyapi.api.AlchemyAPI; +import com.alchemyapi.api.AlchemyApi; import org.xml.sax.SAXException; import org.w3c.dom.Document; @@ -19,7 +19,7 @@ public static void main(String[] args) ParserConfigurationException, XPathExpressionException { // Create an AlchemyAPI object. - AlchemyAPI alchemyObj = AlchemyAPI.GetInstanceFromFile("api_key.txt"); + AlchemyApi alchemyObj = AlchemyApi.GetInstanceFromFile("api_key.txt"); // Extract RSS / ATOM feed links from a web URL. Document doc = alchemyObj.URLGetFeedLinks("http://www.techcrunch.com/"); diff --git a/src/test/java/com/alchemyapi/ImageTest.java b/src/test/resources/to_fix/ImageTest.java similarity index 96% rename from src/test/java/com/alchemyapi/ImageTest.java rename to src/test/resources/to_fix/ImageTest.java index 919f2c2..d098753 100644 --- a/src/test/java/com/alchemyapi/ImageTest.java +++ b/src/test/resources/to_fix/ImageTest.java @@ -1,4 +1,4 @@ -package com.alchemyapi; +package to_fix; import com.alchemyapi.api.*; @@ -20,7 +20,7 @@ public static void main(String[] args) ParserConfigurationException, XPathExpressionException { // Create an AlchemyAPI object. - AlchemyAPI alchemyObj = AlchemyAPI.GetInstanceFromFile("api_key.txt"); + AlchemyApi alchemyObj = AlchemyApi.GetInstanceFromFile("api_key.txt"); // Extract image for a web URL. Document doc = alchemyObj.URLGetImage("http://www.techcrunch.com/"); diff --git a/src/test/java/com/alchemyapi/KeywordTest.java b/src/test/resources/to_fix/KeywordTest.java similarity index 95% rename from src/test/java/com/alchemyapi/KeywordTest.java rename to src/test/resources/to_fix/KeywordTest.java index 1345528..de96e0e 100644 --- a/src/test/java/com/alchemyapi/KeywordTest.java +++ b/src/test/resources/to_fix/KeywordTest.java @@ -1,6 +1,6 @@ -package com.alchemyapi; +package to_fix; -import com.alchemyapi.api.AlchemyAPI; +import com.alchemyapi.api.AlchemyApi; import org.xml.sax.SAXException; import org.w3c.dom.Document; @@ -17,7 +17,7 @@ class KeywordTest { public static void main(String[] args) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { // Create an AlchemyAPI object. - AlchemyAPI alchemyObj = AlchemyAPI.GetInstanceFromFile("api_key.txt"); + AlchemyApi alchemyObj = AlchemyApi.GetInstanceFromFile("api_key.txt"); // Extract topic keywords for a web URL. Document doc = alchemyObj.URLGetRankedKeywords("http://www.techcrunch.com/"); diff --git a/src/test/java/com/alchemyapi/LanguageTest.java b/src/test/resources/to_fix/LanguageTest.java similarity index 95% rename from src/test/java/com/alchemyapi/LanguageTest.java rename to src/test/resources/to_fix/LanguageTest.java index 6cc8df3..c3c4b8c 100644 --- a/src/test/java/com/alchemyapi/LanguageTest.java +++ b/src/test/resources/to_fix/LanguageTest.java @@ -1,6 +1,6 @@ -package com.alchemyapi; +package to_fix; -import com.alchemyapi.api.AlchemyAPI; +import com.alchemyapi.api.AlchemyApi; import org.xml.sax.SAXException; import org.w3c.dom.Document; @@ -19,7 +19,7 @@ public static void main(String[] args) ParserConfigurationException, XPathExpressionException { // Create an AlchemyAPI object. - AlchemyAPI alchemyObj = AlchemyAPI.GetInstanceFromFile("api_key.txt"); + AlchemyApi alchemyObj = AlchemyApi.GetInstanceFromFile("api_key.txt"); // Detect the language for a web URL. Document doc = alchemyObj.URLGetLanguage("http://news.google.fr/"); diff --git a/src/test/java/com/alchemyapi/MicroformatsTest.java b/src/test/resources/to_fix/MicroformatsTest.java similarity index 95% rename from src/test/java/com/alchemyapi/MicroformatsTest.java rename to src/test/resources/to_fix/MicroformatsTest.java index f6eebe0..93666e0 100644 --- a/src/test/java/com/alchemyapi/MicroformatsTest.java +++ b/src/test/resources/to_fix/MicroformatsTest.java @@ -1,6 +1,6 @@ -package com.alchemyapi; +package to_fix; -import com.alchemyapi.api.AlchemyAPI; +import com.alchemyapi.api.AlchemyApi; import org.w3c.dom.Document; import org.xml.sax.SAXException; @@ -24,7 +24,7 @@ public static void main(String[] args) ParserConfigurationException, XPathExpressionException { // Create an AlchemyAPI object. - AlchemyAPI alchemyObj = AlchemyAPI.GetInstanceFromFile("api_key.txt"); + AlchemyApi alchemyObj = AlchemyApi.GetInstanceFromFile("api_key.txt"); // Extract microformats data from a web URL. Document doc = alchemyObj.URLGetMicroformats("http://microformats.org/wiki/hcard"); diff --git a/src/test/java/com/alchemyapi/ParameterTest.java b/src/test/resources/to_fix/ParameterTest.java similarity index 94% rename from src/test/java/com/alchemyapi/ParameterTest.java rename to src/test/resources/to_fix/ParameterTest.java index e6a9f25..55f6e33 100644 --- a/src/test/java/com/alchemyapi/ParameterTest.java +++ b/src/test/resources/to_fix/ParameterTest.java @@ -1,4 +1,4 @@ -package com.alchemyapi; +package to_fix; import com.alchemyapi.api.*; @@ -20,7 +20,7 @@ public static void main(String[] args) ParserConfigurationException, XPathExpressionException { // Create an AlchemyAPI object. - AlchemyAPI alchemyObj = AlchemyAPI.GetInstanceFromFile("api_key.txt"); + AlchemyApi alchemyObj = AlchemyApi.GetInstanceFromFile("api_key.txt"); // Extract a ranked list of named entities for a web URL. Turn off disambiguation NamedEntityParameters entityParams = new NamedEntityParameters(); diff --git a/src/test/java/com/alchemyapi/RelationsTest.java b/src/test/resources/to_fix/RelationsTest.java similarity index 97% rename from src/test/java/com/alchemyapi/RelationsTest.java rename to src/test/resources/to_fix/RelationsTest.java index 208b565..627b23d 100644 --- a/src/test/java/com/alchemyapi/RelationsTest.java +++ b/src/test/resources/to_fix/RelationsTest.java @@ -1,4 +1,4 @@ -package com.alchemyapi.test; +package to_fix; import com.alchemyapi.api.*; @@ -20,7 +20,7 @@ public static void main(String[] args) ParserConfigurationException, XPathExpressionException { // Create an AlchemyAPI object. - AlchemyAPI alchemyObj = AlchemyAPI.GetInstanceFromFile("api_key.txt"); + 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/"); diff --git a/src/test/java/com/alchemyapi/TaxonomyTest.java b/src/test/resources/to_fix/TaxonomyTest.java similarity index 96% rename from src/test/java/com/alchemyapi/TaxonomyTest.java rename to src/test/resources/to_fix/TaxonomyTest.java index 730148f..6b4d8e7 100644 --- a/src/test/java/com/alchemyapi/TaxonomyTest.java +++ b/src/test/resources/to_fix/TaxonomyTest.java @@ -1,4 +1,4 @@ -package com.alchemyapi; +package to_fix; import com.alchemyapi.api.*; @@ -19,7 +19,7 @@ public static void main(String[] args) ParserConfigurationException, XPathExpressionException { // Create an AlchemyAPI object. - AlchemyAPI alchemyObj = AlchemyAPI.GetInstanceFromFile("api_key.txt"); + 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/"); diff --git a/src/test/java/com/alchemyapi/TextExtractTest.java b/src/test/resources/to_fix/TextExtractTest.java similarity index 96% rename from src/test/java/com/alchemyapi/TextExtractTest.java rename to src/test/resources/to_fix/TextExtractTest.java index 9f0b7d6..1fede46 100644 --- a/src/test/java/com/alchemyapi/TextExtractTest.java +++ b/src/test/resources/to_fix/TextExtractTest.java @@ -1,6 +1,6 @@ -package com.alchemyapi; +package to_fix; -import com.alchemyapi.api.AlchemyAPI; +import com.alchemyapi.api.AlchemyApi; import org.xml.sax.SAXException; import org.w3c.dom.Document; @@ -19,7 +19,7 @@ public static void main(String[] args) ParserConfigurationException, XPathExpressionException { // Create an AlchemyAPI object. - AlchemyAPI alchemyObj = AlchemyAPI.GetInstanceFromFile("api_key.txt"); + AlchemyApi alchemyObj = AlchemyApi.GetInstanceFromFile("api_key.txt"); // Extract page text from a web URL. (ignoring ads, navigation links, // and other content). From 66d895b4aff4753130f01637035cac6f5d977283 Mon Sep 17 00:00:00 2001 From: Kenny Cason Date: Sun, 2 Aug 2015 04:46:27 -0500 Subject: [PATCH 05/11] #8 purposeful delete due to capitalization/git issues on mac --- .../java/com/alchemyapi/api/AlchemyAPI.java | 627 ------------------ 1 file changed, 627 deletions(-) delete mode 100644 src/main/java/com/alchemyapi/api/AlchemyAPI.java diff --git a/src/main/java/com/alchemyapi/api/AlchemyAPI.java b/src/main/java/com/alchemyapi/api/AlchemyAPI.java deleted file mode 100644 index 5780ee7..0000000 --- a/src/main/java/com/alchemyapi/api/AlchemyAPI.java +++ /dev/null @@ -1,627 +0,0 @@ -package com.alchemyapi.api; - -import com.alchemyapi.api.exceptions.AlchemyApiException; -import com.alchemyapi.api.parameters.CategoryParameters; -import com.alchemyapi.api.parameters.CombinedParameters; -import com.alchemyapi.api.parameters.ConceptParameters; -import com.alchemyapi.api.parameters.ConstraintQueryParameters; -import com.alchemyapi.api.parameters.ImageParameters; -import com.alchemyapi.api.parameters.KeywordParameters; -import com.alchemyapi.api.parameters.LanguageParameters; -import com.alchemyapi.api.parameters.NamedEntityParameters; -import com.alchemyapi.api.parameters.Parameters; -import com.alchemyapi.api.parameters.RelationParameters; -import com.alchemyapi.api.parameters.TargetedSentimentParameters; -import com.alchemyapi.api.parameters.TaxonomyParameters; -import com.alchemyapi.api.parameters.TextParameters; -import org.apache.log4j.Logger; -import org.w3c.dom.Document; -import org.w3c.dom.NodeList; -import org.xml.sax.SAXException; - -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.xpath.XPath; -import javax.xml.xpath.XPathConstants; -import javax.xml.xpath.XPathExpression; -import javax.xml.xpath.XPathExpressionException; -import javax.xml.xpath.XPathFactory; -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; -import java.net.HttpURLConnection; -import java.net.URL; -import java.nio.charset.Charset; - -import static org.apache.commons.lang3.StringUtils.isBlank; -import static org.apache.commons.lang3.StringUtils.isNotBlank; -import static org.apache.commons.lang3.StringUtils.length; -import static org.apache.commons.lang3.StringUtils.trimToEmpty; - -public class AlchemyApi { - - private static final Logger LOGGER = Logger.getLogger(AlchemyApi.class); - - private static final String API_URL = "http://{SUB_DOMAIN}.alchemyapi.com/calls/"; - - private final AlchemyApiConfiguration configuration; - - public AlchemyApi(final AlchemyApiConfiguration configuration) { - if(configuration == null) { throw new AlchemyApiException("Configuration must not be null"); } - valididateConfiguration(configuration); - LOGGER.info("Loaded Configuration: " + configuration); - this.configuration = configuration; - } - - private static void valididateConfiguration(final AlchemyApiConfiguration configuration) { - if(length(configuration.getApiKey()) < 5) { throw new AlchemyApiException("API key must be at least 5 characters"); } - } - - public Document urlGetAuthor(final URL url) { - return urlGetAuthor(url, new Parameters()); - } - - public Document urlGetAuthor(final URL url, final Parameters params) { - params.setUrl(url); - return get("URLGetAuthor", "url", params); - } - - public Document htmlGetAuthor(final String html, final URL url) { - return htmlGetAuthor(html, url, new Parameters()); - } - - public Document htmlGetAuthor(final String html, final URL url, final Parameters params) { - params.setHtml(html); - params.setUrl(url); - return post("HTMLGetAuthor", "html", params); - } - - public Document urlGetRankedNamedEntities(final URL url) { - return urlGetRankedNamedEntities(url, new NamedEntityParameters()); - } - - public Document urlGetRankedNamedEntities(final URL url, final NamedEntityParameters params) { - params.setUrl(url); - return get("URLGetRankedNamedEntities", "url", params); - } - - public Document htmlGetRankedNamedEntities(final String html, final URL url) { - return htmlGetRankedNamedEntities(html, url, new NamedEntityParameters()); - } - - public Document htmlGetRankedNamedEntities(final String html, final URL url, final NamedEntityParameters params) { - params.setUrl(url); - params.setHtml(html); - return post("HTMLGetRankedNamedEntities", "html", params); - } - - public Document textGetRankedNamedEntities(final String text) { - return textGetRankedNamedEntities(text, new NamedEntityParameters()); - } - - public Document textGetRankedNamedEntities(final String text, final NamedEntityParameters params) { - params.setText(text); - return post("TextGetRankedNamedEntities", "text", params); - } - - public Document urlGetRankedConcepts(final URL url) { - return urlGetRankedConcepts(url, new ConceptParameters()); - } - - public Document urlGetRankedConcepts(final URL url, final ConceptParameters params) { - params.setUrl(url); - return get("URLGetRankedConcepts", "url", params); - } - - public Document htmlGetRankedConcepts(final String html, final URL url) { - return htmlGetRankedConcepts(html, url, new ConceptParameters()); - } - - public Document htmlGetRankedConcepts(final String html, final URL url, final ConceptParameters params) { - params.setUrl(url); - params.setHtml(html); - return post("HTMLGetRankedConcepts", "html", params); - } - - public Document textGetRankedConcepts(final String text) { - return textGetRankedConcepts(text, new ConceptParameters()); - } - - public Document textGetRankedConcepts(final String text, final ConceptParameters params) { - params.setText(text); - return post("TextGetRankedConcepts", "text", params); - } - - public Document urlGetRankedKeywords(final URL url) { - return urlGetRankedKeywords(url, new KeywordParameters()); - } - - public Document urlGetRankedKeywords(final URL url, final KeywordParameters params) { - params.setUrl(url); - return get("URLGetRankedKeywords", "url", params); - } - - public Document htmlGetRankedKeywords(final String html, final URL url) { - return htmlGetRankedKeywords(html, url, new KeywordParameters()); - } - - public Document htmlGetRankedKeywords(final String html, final URL url, final KeywordParameters params) { - params.setUrl(url); - params.setHtml(html); - return post("HTMLGetRankedKeywords", "html", params); - } - - public Document textGetRankedKeywords(final String text) throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException { - return textGetRankedKeywords(text, new KeywordParameters()); - } - - public Document textGetRankedKeywords(final String text, final KeywordParameters params) { - params.setText(text); - return post("TextGetRankedKeywords", "text", params); - } - - public Document urlGetLanguage(final URL url) { - return urlGetLanguage(url, new LanguageParameters()); - } - - public Document urlGetLanguage(final URL url, final LanguageParameters params) { - params.setUrl(url); - return get("URLGetLanguage", "url", params); - } - - public Document htmlGetLanguage(final String html, final URL url) { - return htmlGetLanguage(html, url, new LanguageParameters()); - } - - public Document htmlGetLanguage(final String html, final URL url, final LanguageParameters params) { - params.setUrl(url); - params.setHtml(html); - return post("HTMLGetLanguage", "html", params); - } - - public Document textGetLanguage(final String text) { - return textGetLanguage(text, new LanguageParameters()); - } - - public Document textGetLanguage(final String text, final LanguageParameters params) { - params.setText(text); - return post("TextGetLanguage", "text", params); - } - - public Document urlGetCategory(final URL url) { - return urlGetCategory(url, new CategoryParameters()); - } - - public Document urlGetCategory(final URL url, final CategoryParameters params) { - params.setUrl(url); - return get("URLGetCategory", "url", params); - } - - public Document htmlGetCategory(final String html, URL url) { - return htmlGetCategory(html, url, new CategoryParameters()); - } - - public Document htmlGetCategory(final String html, final URL url, final CategoryParameters params) { - params.setUrl(url); - params.setHtml(html); - return post("HTMLGetCategory", "html", params); - } - - public Document textGetCategory(final String text) { - return textGetCategory(text, new TextParameters()); - } - - public Document textGetCategory(final String text, final TextParameters params) { - params.setText(text); - return post("TextGetCategory", "text", params); - } - - public Document urlGetText(final URL url) { - return urlGetText(url, new TextParameters()); - } - - public Document urlGetText(final URL url, final TextParameters params) { - params.setUrl(url); - return get("URLGetText", "url", params); - } - - public Document htmlGetText(final String html, final URL url) { - return htmlGetText(html, url, new TextParameters()); - } - - public Document htmlGetText(final String html, final URL url, final TextParameters params) { - params.setUrl(url); - params.setHtml(html); - return post("HTMLGetText", "html", params); - } - - public Document urlGetRawText(final URL url) { - return urlGetRawText(url, new Parameters()); - } - - public Document urlGetRawText(final URL url, final Parameters params) { - params.setUrl(url); - return get("URLGetRawText", "url", params); - } - - public Document htmlGetRawText(final String html, final URL url) { - return htmlGetRawText(html, url, new Parameters()); - } - - public Document htmlGetRawText(final String html, final URL url, final Parameters params) { - params.setUrl(url); - params.setHtml(html); - return post("HTMLGetRawText", "html", params); - } - - public Document urlGetTitle(final URL url) { - return urlGetTitle(url, new Parameters()); - } - - public Document urlGetTitle(final URL url, final Parameters params) { - params.setUrl(url); - return get("URLGetTitle", "url", params); - } - - public Document htmlGetTitle(final String html, final URL url) { - return htmlGetTitle(html, url, new Parameters()); - } - - public Document htmlGetTitle(final String html, final URL url, final Parameters params) { - params.setUrl(url); - params.setHtml(html); - return post("HTMLGetTitle", "html", params); - } - - public Document urlGetFeedLinks(final URL url) { - return urlGetFeedLinks(url, new Parameters()); - } - - public Document urlGetFeedLinks(final URL url, final Parameters params) { - params.setUrl(url); - return get("URLGetFeedLinks", "url", params); - } - - public Document htmlGetFeedLinks(final String html, final URL url) { - return htmlGetFeedLinks(html, url, new Parameters()); - } - - public Document htmlGetFeedLinks(final String html, final URL url, final Parameters params) { - params.setUrl(url); - params.setHtml(html); - return post("HTMLGetFeedLinks", "html", params); - } - - public Document urlGetMicroformats(final URL url) { - return urlGetMicroformats(url, new Parameters()); - } - - public Document urlGetMicroformats(final URL url, final Parameters params) { - params.setUrl(url); - return get("URLGetMicroformatData", "url", params); - } - - public Document htmlGetMicroformats(final String html, final URL url) { - return htmlGetMicroformats(html, url, new Parameters()); - } - - public Document htmlGetMicroformats(final String html, final URL url, final Parameters params) { - params.setUrl(url); - params.setHtml(html); - return post("HTMLGetMicroformatData", "html", params); - } - - public Document urlGetConstraintQuery(final URL url, final String query) { - return urlGetConstraintQuery(url, query, new ConstraintQueryParameters()); - } - - public Document urlGetConstraintQuery(final URL url, final String query, final ConstraintQueryParameters params) { - if(trimToEmpty(query).length() < 2) { - throw new AlchemyApiException("Constraint query must be at least 2 characters long"); - } - params.setUrl(url); - params.setCQuery(query); - return post("URLGetConstraintQuery", "url", params); - } - - - public Document htmlGetConstraintQuery(final String html, final URL url, final String query) { - return htmlGetConstraintQuery(html, url, query, new ConstraintQueryParameters()); - } - - public Document htmlGetConstraintQuery(final String html, URL url, final String query, final ConstraintQueryParameters params) { - if(trimToEmpty(query).length() < 2) { - throw new AlchemyApiException("Constraint query must be at least 2 characters long"); - } - params.setUrl(url); - params.setHtml(html); - params.setCQuery(query); - return post("HTMLGetConstraintQuery", "html", params); - } - - public Document urlGetTextSentiment(final URL url) { - return urlGetTextSentiment(url, new Parameters()); - } - - public Document urlGetTextSentiment(final URL url, final Parameters params) { - params.setUrl(url); - return get("URLGetTextSentiment", "url", params); - } - - public Document htmlGetTextSentiment(final String html, final URL url) { - return htmlGetTextSentiment(html, url, new Parameters()); - } - - public Document htmlGetTextSentiment(final String html, URL url, final Parameters params) { - params.setUrl(url); - params.setHtml(html); - return post("HTMLGetTextSentiment", "html", params); - } - - public Document textGetTextSentiment(final String text) { - return textGetTextSentiment(text, new Parameters()); - } - - public Document textGetTextSentiment(final String text, final Parameters params) { - params.setText(text); - return post("TextGetTextSentiment", "text", params); - } - - public Document urlGetTargetedSentiment(final URL url, final String target) { - return urlGetTargetedSentiment(url, target, new TargetedSentimentParameters()); - } - - public Document urlGetTargetedSentiment(final URL url, final String target, final TargetedSentimentParameters params) { - params.setUrl(url); - params.setTarget(target); - return get("URLGetTargetedSentiment", "url", params); - } - - public Document htmlGetTargetedSentiment(final String html, final URL url, final String target) { - return htmlGetTargetedSentiment(html, url, target, new TargetedSentimentParameters()); - } - - public Document htmlGetTargetedSentiment(final String html, final URL url, final String target, final TargetedSentimentParameters params) { - params.setHtml(html); - params.setUrl(url); - params.setTarget(target); - return post("HTMLGetTargetedSentiment", "html", params); - } - - public Document textGetTargetedSentiment(final String text, final String target) { - return textGetTargetedSentiment(text, target, new TargetedSentimentParameters()); - } - - public Document textGetTargetedSentiment(final String text, final String target, final TargetedSentimentParameters params) { - params.setText(text); - params.setTarget(target); - return post("TextGetTargetedSentiment", "text", params); - } - - public Document urlGetRelations(final URL url) { - return urlGetRelations(url, new RelationParameters()); - } - - public Document urlGetRelations(final URL url, final RelationParameters params) { - params.setUrl(url); - return get("URLGetRelations", "url", params); - } - - public Document HTMLGetRelations(final String html, final URL url) { - return HTMLGetRelations(html, url, new RelationParameters()); - } - - public Document HTMLGetRelations(final String html, final URL url, final RelationParameters params) { - params.setUrl(url); - params.setHtml(html); - return post("HTMLGetRelations", "html", params); - } - - public Document textGetRelations(final String text) { - return textGetRelations(text, new RelationParameters()); - } - - public Document textGetRelations(final String text, final RelationParameters params) { - params.setText(text); - return post("TextGetRelations", "text", params); - } - - public Document urlGetCombined(final URL url) { - final CombinedParameters params = new CombinedParameters(); - params.setExtractAll(); - return urlGetCombined(url, params); - } - - public Document urlGetCombined(final URL url, final CombinedParameters params) { - params.setUrl(url); - return get("URLGetCombinedData", "url", params); - } - - public Document textGetCombined(final String text) { - final CombinedParameters params = new CombinedParameters(); - params.setExtractAll(); - return textGetCombined(text, params); - } - - public Document textGetCombined(final String text, final CombinedParameters params) { - params.setText(text); - return post("TextGetCombinedData", "text", params); - } - - public Document urlGetTaxonomy(final URL url) { - return urlGetTaxonomy(url, new TaxonomyParameters()); - } - - public Document urlGetTaxonomy(final URL url, final TaxonomyParameters params) { - params.setUrl(url); - return get("URLGetRankedTaxonomy", "url", params); - } - - public Document htmlGetTaxonomy(final String html, final URL url) { - return htmlGetTaxonomy(html, url, new TaxonomyParameters()); - } - - public Document htmlGetTaxonomy(final String html, final URL url, final TaxonomyParameters params) { - params.setUrl(url); - params.setHtml(html); - return post("HTMLGetRankedTaxonomy", "html", params); - } - - public Document textGetTaxonomy(final String text) { - return textGetTaxonomy(text, new TaxonomyParameters()); - } - - public Document textGetTaxonomy(final String text, final TaxonomyParameters params) { - params.setText(text); - return post("TextGetRankedTaxonomy", "text", params); - } - - public Document urlGetImage(final URL url) { - return urlGetImage(url, new ImageParameters()); - } - - public Document urlGetImage(final URL url, final ImageParameters params) { - params.setUrl(url); - return get("URLGetImage", "url", params); - } - - public Document urlGetRankedImageKeywords(final URL url) { - return urlGetRankedImageKeywords(url, new ImageParameters()); - } - - public Document urlGetRankedImageKeywords(final URL url, final ImageParameters params) { - params.setUrl(url); - return get("URLGetRankedImageKeywords", "url", params); - } - - public Document imageGetRankedImageKeywords(final ImageParameters params) { - try { - final String urlQuery = "?apikey=" + this.configuration.getApiKey() + params.getUrlQuery(); - final URL url = new URL(buildBaseApiUrl() + "image/ImageGetRankedImageKeywords" + urlQuery); - - final HttpURLConnection handle = (HttpURLConnection) url.openConnection(); - handle.setDoOutput(true); - - final byte[] image = params.getImage(); - handle.addRequestProperty("Content-Length", Integer.toString(image.length)); - - final DataOutputStream outputStream = new DataOutputStream(handle.getOutputStream()); - outputStream.write(image); - outputStream.close(); - - return doRequest(handle, params.getOutputMode()); - - } catch(IOException e) { - throw new AlchemyApiException(e); - } - } - - private Document get(final String callName, final String callPrefix, final Parameters params) { - try { - final String urlQuery = "?apikey=" + configuration.getApiKey() + params.getUrlQuery(); - final URL url = new URL(buildBaseApiUrl() + callPrefix + "/" + callName + urlQuery); - - final HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); - httpURLConnection.setDoOutput(true); - - return doRequest(httpURLConnection, params.getOutputMode()); - - } catch(IOException e) { - throw new AlchemyApiException(e); - } - } - - private Document post(final String callName, final String callPrefix, final Parameters params) { - try { - final URL url = new URL(buildBaseApiUrl() + callPrefix + "/" + callName); - final String data = "apikey=" + configuration.getApiKey() + params.getUrlQuery(); - - final HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); - httpURLConnection.setDoOutput(true); - httpURLConnection.addRequestProperty("Content-Length", Integer.toString(data.length())); - - final DataOutputStream outputStream = new DataOutputStream(httpURLConnection.getOutputStream()); - outputStream.write(data.getBytes(Charset.forName("UTF-8"))); - outputStream.close(); - - return doRequest(httpURLConnection, params.getOutputMode()); - - } catch(IOException e) { - throw new AlchemyApiException(e); - } - } - - // TODO add json handling - // TODO return pojo with parsed field, but allow a "raw" xml/json getter to protect against api updates - private Document doRequest(final HttpURLConnection httpURLConnection, final String outputMode) { - try { - final DataInputStream inputStream = new DataInputStream(httpURLConnection.getInputStream()); - final Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(inputStream); - - inputStream.close(); - httpURLConnection.disconnect(); - - switch (outputMode) { - case Parameters.OUTPUT_XML: - return parseXml(document); - - case Parameters.OUTPUT_RDF: - return praseRdf(document); - - case Parameters.OUTPUT_JSON: - throw new AlchemyApiException("Json Response not supported yet"); - } - return document; - - } catch (SAXException | ParserConfigurationException | IOException e) { - throw new AlchemyApiException(e); - } - } - - private Document parseXml(final Document document) { - final XPathFactory factory = XPathFactory.newInstance(); - final String status = getNodeValue(factory, document, "/results/status/text()"); - if (isBlank(status) || !status.equals("OK")) { - final String statusInfo = getNodeValue(factory, document, "/results/statusInfo/text()"); - if (isNotBlank(statusInfo)) { - throw new AlchemyApiException("Error making API call: " + statusInfo); - } - throw new AlchemyApiException("Error making API call: " + status); - } - return document; - } - - private Document praseRdf(final Document document) { - final XPathFactory factory = XPathFactory.newInstance(); - final String status = getNodeValue(factory, document, "//RDF/Description/ResultStatus/text()"); - if (isBlank(status) || !status.equals("OK")) { - final String statusInfo = getNodeValue(factory, document, "//RDF/Description/ResultStatus/text()"); - if (isNotBlank(statusInfo)) { - throw new AlchemyApiException("Error making API call: " + statusInfo); - } - throw new AlchemyApiException("Error making API call: " + status); - } - return document; - } - - private String getNodeValue(XPathFactory factory, Document doc, String xpathStr) { - try { - final XPath xpath = factory.newXPath(); - final XPathExpression expr = xpath.compile(xpathStr); - final Object result = expr.evaluate(doc, XPathConstants.NODESET); - final NodeList results = (NodeList) result; - - if(results.getLength() == 0 || results.item(0) == null) { return null; } - return results.item(0).getNodeValue(); - - } catch (XPathExpressionException e) { - throw new AlchemyApiException(e); - } - } - - private String buildBaseApiUrl() { - return API_URL.replace("{SUB_DOMAIN}", configuration.getApiSubDomain()); - } - -} From 41390d21b0b5d303c238af638ecbfbe41dd5bd6c Mon Sep 17 00:00:00 2001 From: Kenny Cason Date: Sun, 2 Aug 2015 04:47:04 -0500 Subject: [PATCH 06/11] #8 re-add alchemy api class --- .../java/com/alchemyapi/api/AlchemyApi.java | 627 ++++++++++++++++++ 1 file changed, 627 insertions(+) create mode 100644 src/main/java/com/alchemyapi/api/AlchemyApi.java diff --git a/src/main/java/com/alchemyapi/api/AlchemyApi.java b/src/main/java/com/alchemyapi/api/AlchemyApi.java new file mode 100644 index 0000000..5780ee7 --- /dev/null +++ b/src/main/java/com/alchemyapi/api/AlchemyApi.java @@ -0,0 +1,627 @@ +package com.alchemyapi.api; + +import com.alchemyapi.api.exceptions.AlchemyApiException; +import com.alchemyapi.api.parameters.CategoryParameters; +import com.alchemyapi.api.parameters.CombinedParameters; +import com.alchemyapi.api.parameters.ConceptParameters; +import com.alchemyapi.api.parameters.ConstraintQueryParameters; +import com.alchemyapi.api.parameters.ImageParameters; +import com.alchemyapi.api.parameters.KeywordParameters; +import com.alchemyapi.api.parameters.LanguageParameters; +import com.alchemyapi.api.parameters.NamedEntityParameters; +import com.alchemyapi.api.parameters.Parameters; +import com.alchemyapi.api.parameters.RelationParameters; +import com.alchemyapi.api.parameters.TargetedSentimentParameters; +import com.alchemyapi.api.parameters.TaxonomyParameters; +import com.alchemyapi.api.parameters.TextParameters; +import org.apache.log4j.Logger; +import org.w3c.dom.Document; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; + +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathConstants; +import javax.xml.xpath.XPathExpression; +import javax.xml.xpath.XPathExpressionException; +import javax.xml.xpath.XPathFactory; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.URL; +import java.nio.charset.Charset; + +import static org.apache.commons.lang3.StringUtils.isBlank; +import static org.apache.commons.lang3.StringUtils.isNotBlank; +import static org.apache.commons.lang3.StringUtils.length; +import static org.apache.commons.lang3.StringUtils.trimToEmpty; + +public class AlchemyApi { + + private static final Logger LOGGER = Logger.getLogger(AlchemyApi.class); + + private static final String API_URL = "http://{SUB_DOMAIN}.alchemyapi.com/calls/"; + + private final AlchemyApiConfiguration configuration; + + public AlchemyApi(final AlchemyApiConfiguration configuration) { + if(configuration == null) { throw new AlchemyApiException("Configuration must not be null"); } + valididateConfiguration(configuration); + LOGGER.info("Loaded Configuration: " + configuration); + this.configuration = configuration; + } + + private static void valididateConfiguration(final AlchemyApiConfiguration configuration) { + if(length(configuration.getApiKey()) < 5) { throw new AlchemyApiException("API key must be at least 5 characters"); } + } + + public Document urlGetAuthor(final URL url) { + return urlGetAuthor(url, new Parameters()); + } + + public Document urlGetAuthor(final URL url, final Parameters params) { + params.setUrl(url); + return get("URLGetAuthor", "url", params); + } + + public Document htmlGetAuthor(final String html, final URL url) { + return htmlGetAuthor(html, url, new Parameters()); + } + + public Document htmlGetAuthor(final String html, final URL url, final Parameters params) { + params.setHtml(html); + params.setUrl(url); + return post("HTMLGetAuthor", "html", params); + } + + public Document urlGetRankedNamedEntities(final URL url) { + return urlGetRankedNamedEntities(url, new NamedEntityParameters()); + } + + public Document urlGetRankedNamedEntities(final URL url, final NamedEntityParameters params) { + params.setUrl(url); + return get("URLGetRankedNamedEntities", "url", params); + } + + public Document htmlGetRankedNamedEntities(final String html, final URL url) { + return htmlGetRankedNamedEntities(html, url, new NamedEntityParameters()); + } + + public Document htmlGetRankedNamedEntities(final String html, final URL url, final NamedEntityParameters params) { + params.setUrl(url); + params.setHtml(html); + return post("HTMLGetRankedNamedEntities", "html", params); + } + + public Document textGetRankedNamedEntities(final String text) { + return textGetRankedNamedEntities(text, new NamedEntityParameters()); + } + + public Document textGetRankedNamedEntities(final String text, final NamedEntityParameters params) { + params.setText(text); + return post("TextGetRankedNamedEntities", "text", params); + } + + public Document urlGetRankedConcepts(final URL url) { + return urlGetRankedConcepts(url, new ConceptParameters()); + } + + public Document urlGetRankedConcepts(final URL url, final ConceptParameters params) { + params.setUrl(url); + return get("URLGetRankedConcepts", "url", params); + } + + public Document htmlGetRankedConcepts(final String html, final URL url) { + return htmlGetRankedConcepts(html, url, new ConceptParameters()); + } + + public Document htmlGetRankedConcepts(final String html, final URL url, final ConceptParameters params) { + params.setUrl(url); + params.setHtml(html); + return post("HTMLGetRankedConcepts", "html", params); + } + + public Document textGetRankedConcepts(final String text) { + return textGetRankedConcepts(text, new ConceptParameters()); + } + + public Document textGetRankedConcepts(final String text, final ConceptParameters params) { + params.setText(text); + return post("TextGetRankedConcepts", "text", params); + } + + public Document urlGetRankedKeywords(final URL url) { + return urlGetRankedKeywords(url, new KeywordParameters()); + } + + public Document urlGetRankedKeywords(final URL url, final KeywordParameters params) { + params.setUrl(url); + return get("URLGetRankedKeywords", "url", params); + } + + public Document htmlGetRankedKeywords(final String html, final URL url) { + return htmlGetRankedKeywords(html, url, new KeywordParameters()); + } + + public Document htmlGetRankedKeywords(final String html, final URL url, final KeywordParameters params) { + params.setUrl(url); + params.setHtml(html); + return post("HTMLGetRankedKeywords", "html", params); + } + + public Document textGetRankedKeywords(final String text) throws IOException, SAXException, + ParserConfigurationException, XPathExpressionException { + return textGetRankedKeywords(text, new KeywordParameters()); + } + + public Document textGetRankedKeywords(final String text, final KeywordParameters params) { + params.setText(text); + return post("TextGetRankedKeywords", "text", params); + } + + public Document urlGetLanguage(final URL url) { + return urlGetLanguage(url, new LanguageParameters()); + } + + public Document urlGetLanguage(final URL url, final LanguageParameters params) { + params.setUrl(url); + return get("URLGetLanguage", "url", params); + } + + public Document htmlGetLanguage(final String html, final URL url) { + return htmlGetLanguage(html, url, new LanguageParameters()); + } + + public Document htmlGetLanguage(final String html, final URL url, final LanguageParameters params) { + params.setUrl(url); + params.setHtml(html); + return post("HTMLGetLanguage", "html", params); + } + + public Document textGetLanguage(final String text) { + return textGetLanguage(text, new LanguageParameters()); + } + + public Document textGetLanguage(final String text, final LanguageParameters params) { + params.setText(text); + return post("TextGetLanguage", "text", params); + } + + public Document urlGetCategory(final URL url) { + return urlGetCategory(url, new CategoryParameters()); + } + + public Document urlGetCategory(final URL url, final CategoryParameters params) { + params.setUrl(url); + return get("URLGetCategory", "url", params); + } + + public Document htmlGetCategory(final String html, URL url) { + return htmlGetCategory(html, url, new CategoryParameters()); + } + + public Document htmlGetCategory(final String html, final URL url, final CategoryParameters params) { + params.setUrl(url); + params.setHtml(html); + return post("HTMLGetCategory", "html", params); + } + + public Document textGetCategory(final String text) { + return textGetCategory(text, new TextParameters()); + } + + public Document textGetCategory(final String text, final TextParameters params) { + params.setText(text); + return post("TextGetCategory", "text", params); + } + + public Document urlGetText(final URL url) { + return urlGetText(url, new TextParameters()); + } + + public Document urlGetText(final URL url, final TextParameters params) { + params.setUrl(url); + return get("URLGetText", "url", params); + } + + public Document htmlGetText(final String html, final URL url) { + return htmlGetText(html, url, new TextParameters()); + } + + public Document htmlGetText(final String html, final URL url, final TextParameters params) { + params.setUrl(url); + params.setHtml(html); + return post("HTMLGetText", "html", params); + } + + public Document urlGetRawText(final URL url) { + return urlGetRawText(url, new Parameters()); + } + + public Document urlGetRawText(final URL url, final Parameters params) { + params.setUrl(url); + return get("URLGetRawText", "url", params); + } + + public Document htmlGetRawText(final String html, final URL url) { + return htmlGetRawText(html, url, new Parameters()); + } + + public Document htmlGetRawText(final String html, final URL url, final Parameters params) { + params.setUrl(url); + params.setHtml(html); + return post("HTMLGetRawText", "html", params); + } + + public Document urlGetTitle(final URL url) { + return urlGetTitle(url, new Parameters()); + } + + public Document urlGetTitle(final URL url, final Parameters params) { + params.setUrl(url); + return get("URLGetTitle", "url", params); + } + + public Document htmlGetTitle(final String html, final URL url) { + return htmlGetTitle(html, url, new Parameters()); + } + + public Document htmlGetTitle(final String html, final URL url, final Parameters params) { + params.setUrl(url); + params.setHtml(html); + return post("HTMLGetTitle", "html", params); + } + + public Document urlGetFeedLinks(final URL url) { + return urlGetFeedLinks(url, new Parameters()); + } + + public Document urlGetFeedLinks(final URL url, final Parameters params) { + params.setUrl(url); + return get("URLGetFeedLinks", "url", params); + } + + public Document htmlGetFeedLinks(final String html, final URL url) { + return htmlGetFeedLinks(html, url, new Parameters()); + } + + public Document htmlGetFeedLinks(final String html, final URL url, final Parameters params) { + params.setUrl(url); + params.setHtml(html); + return post("HTMLGetFeedLinks", "html", params); + } + + public Document urlGetMicroformats(final URL url) { + return urlGetMicroformats(url, new Parameters()); + } + + public Document urlGetMicroformats(final URL url, final Parameters params) { + params.setUrl(url); + return get("URLGetMicroformatData", "url", params); + } + + public Document htmlGetMicroformats(final String html, final URL url) { + return htmlGetMicroformats(html, url, new Parameters()); + } + + public Document htmlGetMicroformats(final String html, final URL url, final Parameters params) { + params.setUrl(url); + params.setHtml(html); + return post("HTMLGetMicroformatData", "html", params); + } + + public Document urlGetConstraintQuery(final URL url, final String query) { + return urlGetConstraintQuery(url, query, new ConstraintQueryParameters()); + } + + public Document urlGetConstraintQuery(final URL url, final String query, final ConstraintQueryParameters params) { + if(trimToEmpty(query).length() < 2) { + throw new AlchemyApiException("Constraint query must be at least 2 characters long"); + } + params.setUrl(url); + params.setCQuery(query); + return post("URLGetConstraintQuery", "url", params); + } + + + public Document htmlGetConstraintQuery(final String html, final URL url, final String query) { + return htmlGetConstraintQuery(html, url, query, new ConstraintQueryParameters()); + } + + public Document htmlGetConstraintQuery(final String html, URL url, final String query, final ConstraintQueryParameters params) { + if(trimToEmpty(query).length() < 2) { + throw new AlchemyApiException("Constraint query must be at least 2 characters long"); + } + params.setUrl(url); + params.setHtml(html); + params.setCQuery(query); + return post("HTMLGetConstraintQuery", "html", params); + } + + public Document urlGetTextSentiment(final URL url) { + return urlGetTextSentiment(url, new Parameters()); + } + + public Document urlGetTextSentiment(final URL url, final Parameters params) { + params.setUrl(url); + return get("URLGetTextSentiment", "url", params); + } + + public Document htmlGetTextSentiment(final String html, final URL url) { + return htmlGetTextSentiment(html, url, new Parameters()); + } + + public Document htmlGetTextSentiment(final String html, URL url, final Parameters params) { + params.setUrl(url); + params.setHtml(html); + return post("HTMLGetTextSentiment", "html", params); + } + + public Document textGetTextSentiment(final String text) { + return textGetTextSentiment(text, new Parameters()); + } + + public Document textGetTextSentiment(final String text, final Parameters params) { + params.setText(text); + return post("TextGetTextSentiment", "text", params); + } + + public Document urlGetTargetedSentiment(final URL url, final String target) { + return urlGetTargetedSentiment(url, target, new TargetedSentimentParameters()); + } + + public Document urlGetTargetedSentiment(final URL url, final String target, final TargetedSentimentParameters params) { + params.setUrl(url); + params.setTarget(target); + return get("URLGetTargetedSentiment", "url", params); + } + + public Document htmlGetTargetedSentiment(final String html, final URL url, final String target) { + return htmlGetTargetedSentiment(html, url, target, new TargetedSentimentParameters()); + } + + public Document htmlGetTargetedSentiment(final String html, final URL url, final String target, final TargetedSentimentParameters params) { + params.setHtml(html); + params.setUrl(url); + params.setTarget(target); + return post("HTMLGetTargetedSentiment", "html", params); + } + + public Document textGetTargetedSentiment(final String text, final String target) { + return textGetTargetedSentiment(text, target, new TargetedSentimentParameters()); + } + + public Document textGetTargetedSentiment(final String text, final String target, final TargetedSentimentParameters params) { + params.setText(text); + params.setTarget(target); + return post("TextGetTargetedSentiment", "text", params); + } + + public Document urlGetRelations(final URL url) { + return urlGetRelations(url, new RelationParameters()); + } + + public Document urlGetRelations(final URL url, final RelationParameters params) { + params.setUrl(url); + return get("URLGetRelations", "url", params); + } + + public Document HTMLGetRelations(final String html, final URL url) { + return HTMLGetRelations(html, url, new RelationParameters()); + } + + public Document HTMLGetRelations(final String html, final URL url, final RelationParameters params) { + params.setUrl(url); + params.setHtml(html); + return post("HTMLGetRelations", "html", params); + } + + public Document textGetRelations(final String text) { + return textGetRelations(text, new RelationParameters()); + } + + public Document textGetRelations(final String text, final RelationParameters params) { + params.setText(text); + return post("TextGetRelations", "text", params); + } + + public Document urlGetCombined(final URL url) { + final CombinedParameters params = new CombinedParameters(); + params.setExtractAll(); + return urlGetCombined(url, params); + } + + public Document urlGetCombined(final URL url, final CombinedParameters params) { + params.setUrl(url); + return get("URLGetCombinedData", "url", params); + } + + public Document textGetCombined(final String text) { + final CombinedParameters params = new CombinedParameters(); + params.setExtractAll(); + return textGetCombined(text, params); + } + + public Document textGetCombined(final String text, final CombinedParameters params) { + params.setText(text); + return post("TextGetCombinedData", "text", params); + } + + public Document urlGetTaxonomy(final URL url) { + return urlGetTaxonomy(url, new TaxonomyParameters()); + } + + public Document urlGetTaxonomy(final URL url, final TaxonomyParameters params) { + params.setUrl(url); + return get("URLGetRankedTaxonomy", "url", params); + } + + public Document htmlGetTaxonomy(final String html, final URL url) { + return htmlGetTaxonomy(html, url, new TaxonomyParameters()); + } + + public Document htmlGetTaxonomy(final String html, final URL url, final TaxonomyParameters params) { + params.setUrl(url); + params.setHtml(html); + return post("HTMLGetRankedTaxonomy", "html", params); + } + + public Document textGetTaxonomy(final String text) { + return textGetTaxonomy(text, new TaxonomyParameters()); + } + + public Document textGetTaxonomy(final String text, final TaxonomyParameters params) { + params.setText(text); + return post("TextGetRankedTaxonomy", "text", params); + } + + public Document urlGetImage(final URL url) { + return urlGetImage(url, new ImageParameters()); + } + + public Document urlGetImage(final URL url, final ImageParameters params) { + params.setUrl(url); + return get("URLGetImage", "url", params); + } + + public Document urlGetRankedImageKeywords(final URL url) { + return urlGetRankedImageKeywords(url, new ImageParameters()); + } + + public Document urlGetRankedImageKeywords(final URL url, final ImageParameters params) { + params.setUrl(url); + return get("URLGetRankedImageKeywords", "url", params); + } + + public Document imageGetRankedImageKeywords(final ImageParameters params) { + try { + final String urlQuery = "?apikey=" + this.configuration.getApiKey() + params.getUrlQuery(); + final URL url = new URL(buildBaseApiUrl() + "image/ImageGetRankedImageKeywords" + urlQuery); + + final HttpURLConnection handle = (HttpURLConnection) url.openConnection(); + handle.setDoOutput(true); + + final byte[] image = params.getImage(); + handle.addRequestProperty("Content-Length", Integer.toString(image.length)); + + final DataOutputStream outputStream = new DataOutputStream(handle.getOutputStream()); + outputStream.write(image); + outputStream.close(); + + return doRequest(handle, params.getOutputMode()); + + } catch(IOException e) { + throw new AlchemyApiException(e); + } + } + + private Document get(final String callName, final String callPrefix, final Parameters params) { + try { + final String urlQuery = "?apikey=" + configuration.getApiKey() + params.getUrlQuery(); + final URL url = new URL(buildBaseApiUrl() + callPrefix + "/" + callName + urlQuery); + + final HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); + httpURLConnection.setDoOutput(true); + + return doRequest(httpURLConnection, params.getOutputMode()); + + } catch(IOException e) { + throw new AlchemyApiException(e); + } + } + + private Document post(final String callName, final String callPrefix, final Parameters params) { + try { + final URL url = new URL(buildBaseApiUrl() + callPrefix + "/" + callName); + final String data = "apikey=" + configuration.getApiKey() + params.getUrlQuery(); + + final HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); + httpURLConnection.setDoOutput(true); + httpURLConnection.addRequestProperty("Content-Length", Integer.toString(data.length())); + + final DataOutputStream outputStream = new DataOutputStream(httpURLConnection.getOutputStream()); + outputStream.write(data.getBytes(Charset.forName("UTF-8"))); + outputStream.close(); + + return doRequest(httpURLConnection, params.getOutputMode()); + + } catch(IOException e) { + throw new AlchemyApiException(e); + } + } + + // TODO add json handling + // TODO return pojo with parsed field, but allow a "raw" xml/json getter to protect against api updates + private Document doRequest(final HttpURLConnection httpURLConnection, final String outputMode) { + try { + final DataInputStream inputStream = new DataInputStream(httpURLConnection.getInputStream()); + final Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(inputStream); + + inputStream.close(); + httpURLConnection.disconnect(); + + switch (outputMode) { + case Parameters.OUTPUT_XML: + return parseXml(document); + + case Parameters.OUTPUT_RDF: + return praseRdf(document); + + case Parameters.OUTPUT_JSON: + throw new AlchemyApiException("Json Response not supported yet"); + } + return document; + + } catch (SAXException | ParserConfigurationException | IOException e) { + throw new AlchemyApiException(e); + } + } + + private Document parseXml(final Document document) { + final XPathFactory factory = XPathFactory.newInstance(); + final String status = getNodeValue(factory, document, "/results/status/text()"); + if (isBlank(status) || !status.equals("OK")) { + final String statusInfo = getNodeValue(factory, document, "/results/statusInfo/text()"); + if (isNotBlank(statusInfo)) { + throw new AlchemyApiException("Error making API call: " + statusInfo); + } + throw new AlchemyApiException("Error making API call: " + status); + } + return document; + } + + private Document praseRdf(final Document document) { + final XPathFactory factory = XPathFactory.newInstance(); + final String status = getNodeValue(factory, document, "//RDF/Description/ResultStatus/text()"); + if (isBlank(status) || !status.equals("OK")) { + final String statusInfo = getNodeValue(factory, document, "//RDF/Description/ResultStatus/text()"); + if (isNotBlank(statusInfo)) { + throw new AlchemyApiException("Error making API call: " + statusInfo); + } + throw new AlchemyApiException("Error making API call: " + status); + } + return document; + } + + private String getNodeValue(XPathFactory factory, Document doc, String xpathStr) { + try { + final XPath xpath = factory.newXPath(); + final XPathExpression expr = xpath.compile(xpathStr); + final Object result = expr.evaluate(doc, XPathConstants.NODESET); + final NodeList results = (NodeList) result; + + if(results.getLength() == 0 || results.item(0) == null) { return null; } + return results.item(0).getNodeValue(); + + } catch (XPathExpressionException e) { + throw new AlchemyApiException(e); + } + } + + private String buildBaseApiUrl() { + return API_URL.replace("{SUB_DOMAIN}", configuration.getApiSubDomain()); + } + +} From 76b09401165746494f34bc4102d61532fda42282 Mon Sep 17 00:00:00 2001 From: Kenny Cason Date: Sun, 2 Aug 2015 05:01:51 -0500 Subject: [PATCH 07/11] #8 improve url usability + add parameter checks --- .../java/com/alchemyapi/api/AlchemyApi.java | 140 +++++++++--------- .../alchemyapi/api/parameters/Parameters.java | 68 ++++++--- src/test/java/com/alchemyapi/STestAuthor.java | 7 +- .../java/com/alchemyapi/STestSentiment.java | 11 +- 4 files changed, 126 insertions(+), 100 deletions(-) diff --git a/src/main/java/com/alchemyapi/api/AlchemyApi.java b/src/main/java/com/alchemyapi/api/AlchemyApi.java index 5780ee7..4a4e2d1 100644 --- a/src/main/java/com/alchemyapi/api/AlchemyApi.java +++ b/src/main/java/com/alchemyapi/api/AlchemyApi.java @@ -57,39 +57,39 @@ private static void valididateConfiguration(final AlchemyApiConfiguration config if(length(configuration.getApiKey()) < 5) { throw new AlchemyApiException("API key must be at least 5 characters"); } } - public Document urlGetAuthor(final URL url) { + public Document urlGetAuthor(final String url) { return urlGetAuthor(url, new Parameters()); } - public Document urlGetAuthor(final URL url, final Parameters params) { + public Document urlGetAuthor(final String url, final Parameters params) { params.setUrl(url); return get("URLGetAuthor", "url", params); } - public Document htmlGetAuthor(final String html, final URL url) { + public Document htmlGetAuthor(final String html, final String url) { return htmlGetAuthor(html, url, new Parameters()); } - public Document htmlGetAuthor(final String html, final URL url, final Parameters params) { + public Document htmlGetAuthor(final String html, final String url, final Parameters params) { params.setHtml(html); params.setUrl(url); return post("HTMLGetAuthor", "html", params); } - public Document urlGetRankedNamedEntities(final URL url) { + public Document urlGetRankedNamedEntities(final String url) { return urlGetRankedNamedEntities(url, new NamedEntityParameters()); } - public Document urlGetRankedNamedEntities(final URL url, final NamedEntityParameters params) { + public Document urlGetRankedNamedEntities(final String url, final NamedEntityParameters params) { params.setUrl(url); return get("URLGetRankedNamedEntities", "url", params); } - public Document htmlGetRankedNamedEntities(final String html, final URL url) { + public Document htmlGetRankedNamedEntities(final String html, final String url) { return htmlGetRankedNamedEntities(html, url, new NamedEntityParameters()); } - public Document htmlGetRankedNamedEntities(final String html, final URL url, final NamedEntityParameters params) { + public Document htmlGetRankedNamedEntities(final String html, final String url, final NamedEntityParameters params) { params.setUrl(url); params.setHtml(html); return post("HTMLGetRankedNamedEntities", "html", params); @@ -104,20 +104,20 @@ public Document textGetRankedNamedEntities(final String text, final NamedEntityP return post("TextGetRankedNamedEntities", "text", params); } - public Document urlGetRankedConcepts(final URL url) { + public Document urlGetRankedConcepts(final String url) { return urlGetRankedConcepts(url, new ConceptParameters()); } - public Document urlGetRankedConcepts(final URL url, final ConceptParameters params) { + public Document urlGetRankedConcepts(final String url, final ConceptParameters params) { params.setUrl(url); return get("URLGetRankedConcepts", "url", params); } - public Document htmlGetRankedConcepts(final String html, final URL url) { + public Document htmlGetRankedConcepts(final String html, final String url) { return htmlGetRankedConcepts(html, url, new ConceptParameters()); } - public Document htmlGetRankedConcepts(final String html, final URL url, final ConceptParameters params) { + public Document htmlGetRankedConcepts(final String html, final String url, final ConceptParameters params) { params.setUrl(url); params.setHtml(html); return post("HTMLGetRankedConcepts", "html", params); @@ -132,20 +132,20 @@ public Document textGetRankedConcepts(final String text, final ConceptParameters return post("TextGetRankedConcepts", "text", params); } - public Document urlGetRankedKeywords(final URL url) { + public Document urlGetRankedKeywords(final String url) { return urlGetRankedKeywords(url, new KeywordParameters()); } - public Document urlGetRankedKeywords(final URL url, final KeywordParameters params) { + public Document urlGetRankedKeywords(final String url, final KeywordParameters params) { params.setUrl(url); return get("URLGetRankedKeywords", "url", params); } - public Document htmlGetRankedKeywords(final String html, final URL url) { + public Document htmlGetRankedKeywords(final String html, final String url) { return htmlGetRankedKeywords(html, url, new KeywordParameters()); } - public Document htmlGetRankedKeywords(final String html, final URL url, final KeywordParameters params) { + public Document htmlGetRankedKeywords(final String html, final String url, final KeywordParameters params) { params.setUrl(url); params.setHtml(html); return post("HTMLGetRankedKeywords", "html", params); @@ -161,20 +161,20 @@ public Document textGetRankedKeywords(final String text, final KeywordParameters return post("TextGetRankedKeywords", "text", params); } - public Document urlGetLanguage(final URL url) { + public Document urlGetLanguage(final String url) { return urlGetLanguage(url, new LanguageParameters()); } - public Document urlGetLanguage(final URL url, final LanguageParameters params) { + public Document urlGetLanguage(final String url, final LanguageParameters params) { params.setUrl(url); return get("URLGetLanguage", "url", params); } - public Document htmlGetLanguage(final String html, final URL url) { + public Document htmlGetLanguage(final String html, final String url) { return htmlGetLanguage(html, url, new LanguageParameters()); } - public Document htmlGetLanguage(final String html, final URL url, final LanguageParameters params) { + public Document htmlGetLanguage(final String html, final String url, final LanguageParameters params) { params.setUrl(url); params.setHtml(html); return post("HTMLGetLanguage", "html", params); @@ -189,20 +189,20 @@ public Document textGetLanguage(final String text, final LanguageParameters para return post("TextGetLanguage", "text", params); } - public Document urlGetCategory(final URL url) { + public Document urlGetCategory(final String url) { return urlGetCategory(url, new CategoryParameters()); } - public Document urlGetCategory(final URL url, final CategoryParameters params) { + public Document urlGetCategory(final String url, final CategoryParameters params) { params.setUrl(url); return get("URLGetCategory", "url", params); } - public Document htmlGetCategory(final String html, URL url) { + public Document htmlGetCategory(final String html, final String url) { return htmlGetCategory(html, url, new CategoryParameters()); } - public Document htmlGetCategory(final String html, final URL url, final CategoryParameters params) { + public Document htmlGetCategory(final String html, final String url, final CategoryParameters params) { params.setUrl(url); params.setHtml(html); return post("HTMLGetCategory", "html", params); @@ -217,106 +217,106 @@ public Document textGetCategory(final String text, final TextParameters params) return post("TextGetCategory", "text", params); } - public Document urlGetText(final URL url) { + public Document urlGetText(final String url) { return urlGetText(url, new TextParameters()); } - public Document urlGetText(final URL url, final TextParameters params) { + public Document urlGetText(final String url, final TextParameters params) { params.setUrl(url); return get("URLGetText", "url", params); } - public Document htmlGetText(final String html, final URL url) { + public Document htmlGetText(final String html, final String url) { return htmlGetText(html, url, new TextParameters()); } - public Document htmlGetText(final String html, final URL url, final TextParameters params) { + public Document htmlGetText(final String html, final String url, final TextParameters params) { params.setUrl(url); params.setHtml(html); return post("HTMLGetText", "html", params); } - public Document urlGetRawText(final URL url) { + public Document urlGetRawText(final String url) { return urlGetRawText(url, new Parameters()); } - public Document urlGetRawText(final URL url, final Parameters params) { + public Document urlGetRawText(final String url, final Parameters params) { params.setUrl(url); return get("URLGetRawText", "url", params); } - public Document htmlGetRawText(final String html, final URL url) { + public Document htmlGetRawText(final String html, final String url) { return htmlGetRawText(html, url, new Parameters()); } - public Document htmlGetRawText(final String html, final URL url, final Parameters params) { + public Document htmlGetRawText(final String html, final String url, final Parameters params) { params.setUrl(url); params.setHtml(html); return post("HTMLGetRawText", "html", params); } - public Document urlGetTitle(final URL url) { + public Document urlGetTitle(final String url) { return urlGetTitle(url, new Parameters()); } - public Document urlGetTitle(final URL url, final Parameters params) { + public Document urlGetTitle(final String url, final Parameters params) { params.setUrl(url); return get("URLGetTitle", "url", params); } - public Document htmlGetTitle(final String html, final URL url) { + public Document htmlGetTitle(final String html, final String url) { return htmlGetTitle(html, url, new Parameters()); } - public Document htmlGetTitle(final String html, final URL url, final Parameters params) { + public Document htmlGetTitle(final String html, final String url, final Parameters params) { params.setUrl(url); params.setHtml(html); return post("HTMLGetTitle", "html", params); } - public Document urlGetFeedLinks(final URL url) { + public Document urlGetFeedLinks(final String url) { return urlGetFeedLinks(url, new Parameters()); } - public Document urlGetFeedLinks(final URL url, final Parameters params) { + public Document urlGetFeedLinks(final String url, final Parameters params) { params.setUrl(url); return get("URLGetFeedLinks", "url", params); } - public Document htmlGetFeedLinks(final String html, final URL url) { + public Document htmlGetFeedLinks(final String html, final String url) { return htmlGetFeedLinks(html, url, new Parameters()); } - public Document htmlGetFeedLinks(final String html, final URL url, final Parameters params) { + public Document htmlGetFeedLinks(final String html, final String url, final Parameters params) { params.setUrl(url); params.setHtml(html); return post("HTMLGetFeedLinks", "html", params); } - public Document urlGetMicroformats(final URL url) { + public Document urlGetMicroformats(final String url) { return urlGetMicroformats(url, new Parameters()); } - public Document urlGetMicroformats(final URL url, final Parameters params) { + public Document urlGetMicroformats(final String url, final Parameters params) { params.setUrl(url); return get("URLGetMicroformatData", "url", params); } - public Document htmlGetMicroformats(final String html, final URL url) { + public Document htmlGetMicroformats(final String html, final String url) { return htmlGetMicroformats(html, url, new Parameters()); } - public Document htmlGetMicroformats(final String html, final URL url, final Parameters params) { + public Document htmlGetMicroformats(final String html, final String url, final Parameters params) { params.setUrl(url); params.setHtml(html); return post("HTMLGetMicroformatData", "html", params); } - public Document urlGetConstraintQuery(final URL url, final String query) { + public Document urlGetConstraintQuery(final String url, final String query) { return urlGetConstraintQuery(url, query, new ConstraintQueryParameters()); } - public Document urlGetConstraintQuery(final URL url, final String query, final ConstraintQueryParameters params) { + public Document urlGetConstraintQuery(final String url, final String query, final ConstraintQueryParameters params) { if(trimToEmpty(query).length() < 2) { throw new AlchemyApiException("Constraint query must be at least 2 characters long"); } @@ -326,11 +326,11 @@ public Document urlGetConstraintQuery(final URL url, final String query, final C } - public Document htmlGetConstraintQuery(final String html, final URL url, final String query) { + public Document htmlGetConstraintQuery(final String html, final String url, final String query) { return htmlGetConstraintQuery(html, url, query, new ConstraintQueryParameters()); } - public Document htmlGetConstraintQuery(final String html, URL url, final String query, final ConstraintQueryParameters params) { + public Document htmlGetConstraintQuery(final String html, final String url, final String query, final ConstraintQueryParameters params) { if(trimToEmpty(query).length() < 2) { throw new AlchemyApiException("Constraint query must be at least 2 characters long"); } @@ -340,20 +340,20 @@ public Document htmlGetConstraintQuery(final String html, URL url, final String return post("HTMLGetConstraintQuery", "html", params); } - public Document urlGetTextSentiment(final URL url) { + public Document urlGetTextSentiment(final String url) { return urlGetTextSentiment(url, new Parameters()); } - public Document urlGetTextSentiment(final URL url, final Parameters params) { + public Document urlGetTextSentiment(final String url, final Parameters params) { params.setUrl(url); return get("URLGetTextSentiment", "url", params); } - public Document htmlGetTextSentiment(final String html, final URL url) { + public Document htmlGetTextSentiment(final String html, final String url) { return htmlGetTextSentiment(html, url, new Parameters()); } - public Document htmlGetTextSentiment(final String html, URL url, final Parameters params) { + public Document htmlGetTextSentiment(final String html, final String url, final Parameters params) { params.setUrl(url); params.setHtml(html); return post("HTMLGetTextSentiment", "html", params); @@ -368,21 +368,21 @@ public Document textGetTextSentiment(final String text, final Parameters params) return post("TextGetTextSentiment", "text", params); } - public Document urlGetTargetedSentiment(final URL url, final String target) { + public Document urlGetTargetedSentiment(final String url, final String target) { return urlGetTargetedSentiment(url, target, new TargetedSentimentParameters()); } - public Document urlGetTargetedSentiment(final URL url, final String target, final TargetedSentimentParameters params) { + public Document urlGetTargetedSentiment(final String url, final String target, final TargetedSentimentParameters params) { params.setUrl(url); params.setTarget(target); return get("URLGetTargetedSentiment", "url", params); } - public Document htmlGetTargetedSentiment(final String html, final URL url, final String target) { + public Document htmlGetTargetedSentiment(final String html, final String url, final String target) { return htmlGetTargetedSentiment(html, url, target, new TargetedSentimentParameters()); } - public Document htmlGetTargetedSentiment(final String html, final URL url, final String target, final TargetedSentimentParameters params) { + public Document htmlGetTargetedSentiment(final String html, final String url, final String target, final TargetedSentimentParameters params) { params.setHtml(html); params.setUrl(url); params.setTarget(target); @@ -399,20 +399,20 @@ public Document textGetTargetedSentiment(final String text, final String target, return post("TextGetTargetedSentiment", "text", params); } - public Document urlGetRelations(final URL url) { + public Document urlGetRelations(final String url) { return urlGetRelations(url, new RelationParameters()); } - public Document urlGetRelations(final URL url, final RelationParameters params) { + public Document urlGetRelations(final String url, final RelationParameters params) { params.setUrl(url); return get("URLGetRelations", "url", params); } - public Document HTMLGetRelations(final String html, final URL url) { + public Document HTMLGetRelations(final String html, final String url) { return HTMLGetRelations(html, url, new RelationParameters()); } - public Document HTMLGetRelations(final String html, final URL url, final RelationParameters params) { + public Document HTMLGetRelations(final String html, final String url, final RelationParameters params) { params.setUrl(url); params.setHtml(html); return post("HTMLGetRelations", "html", params); @@ -427,13 +427,13 @@ public Document textGetRelations(final String text, final RelationParameters par return post("TextGetRelations", "text", params); } - public Document urlGetCombined(final URL url) { + public Document urlGetCombined(final String url) { final CombinedParameters params = new CombinedParameters(); params.setExtractAll(); return urlGetCombined(url, params); } - public Document urlGetCombined(final URL url, final CombinedParameters params) { + public Document urlGetCombined(final String url, final CombinedParameters params) { params.setUrl(url); return get("URLGetCombinedData", "url", params); } @@ -449,20 +449,20 @@ public Document textGetCombined(final String text, final CombinedParameters para return post("TextGetCombinedData", "text", params); } - public Document urlGetTaxonomy(final URL url) { + public Document urlGetTaxonomy(final String url) { return urlGetTaxonomy(url, new TaxonomyParameters()); } - public Document urlGetTaxonomy(final URL url, final TaxonomyParameters params) { + public Document urlGetTaxonomy(final String url, final TaxonomyParameters params) { params.setUrl(url); return get("URLGetRankedTaxonomy", "url", params); } - public Document htmlGetTaxonomy(final String html, final URL url) { + public Document htmlGetTaxonomy(final String html, final String url) { return htmlGetTaxonomy(html, url, new TaxonomyParameters()); } - public Document htmlGetTaxonomy(final String html, final URL url, final TaxonomyParameters params) { + public Document htmlGetTaxonomy(final String html, final String url, final TaxonomyParameters params) { params.setUrl(url); params.setHtml(html); return post("HTMLGetRankedTaxonomy", "html", params); @@ -477,20 +477,20 @@ public Document textGetTaxonomy(final String text, final TaxonomyParameters para return post("TextGetRankedTaxonomy", "text", params); } - public Document urlGetImage(final URL url) { + public Document urlGetImage(final String url) { return urlGetImage(url, new ImageParameters()); } - public Document urlGetImage(final URL url, final ImageParameters params) { + public Document urlGetImage(final String url, final ImageParameters params) { params.setUrl(url); return get("URLGetImage", "url", params); } - public Document urlGetRankedImageKeywords(final URL url) { + public Document urlGetRankedImageKeywords(final String url) { return urlGetRankedImageKeywords(url, new ImageParameters()); } - public Document urlGetRankedImageKeywords(final URL url, final ImageParameters params) { + public Document urlGetRankedImageKeywords(final String url, final ImageParameters params) { params.setUrl(url); return get("URLGetRankedImageKeywords", "url", params); } diff --git a/src/main/java/com/alchemyapi/api/parameters/Parameters.java b/src/main/java/com/alchemyapi/api/parameters/Parameters.java index 3415cae..8004046 100644 --- a/src/main/java/com/alchemyapi/api/parameters/Parameters.java +++ b/src/main/java/com/alchemyapi/api/parameters/Parameters.java @@ -1,8 +1,14 @@ package com.alchemyapi.api.parameters; +import com.alchemyapi.api.exceptions.AlchemyApiException; + import java.io.UnsupportedEncodingException; +import java.net.MalformedURLException; import java.net.URL; import java.net.URLEncoder; +import java.util.Map; + +import static org.apache.commons.lang3.StringUtils.isBlank; public class Parameters { @@ -10,17 +16,18 @@ public class Parameters { public static final String OUTPUT_XML = "xml"; public static final String OUTPUT_RDF = "rdf"; - private URL url; + private String url; private String html; private String text; private String outputMode = OUTPUT_XML; // TODO make json default private String customParameters; - public URL getUrl() { + public String getUrl() { return url; } - public void setUrl(URL url) { + public void setUrl(String url) { + validateUrl(url); this.url = url; } @@ -29,6 +36,7 @@ public String getHtml() { } public void setHtml(String html) { + validateHtml(html); this.html = html; } @@ -37,6 +45,7 @@ public String getText() { } public void setText(String text) { + validateText(text); this.text = text; } @@ -55,32 +64,51 @@ public String getCustomParameters() { return customParameters; } - public void setCustomParameters(String... customParameters) { - StringBuilder data = new StringBuilder(); + public void setCustomParameters(Map customParameters) { + final StringBuilder data = new StringBuilder(); try { - for (int i = 0; i < customParameters.length; ++i) { - data.append('&').append(customParameters[i]); - if (++i < customParameters.length) - data.append('=').append(URLEncoder.encode(customParameters[i], "UTF8")); + for(Map.Entry parameter : customParameters.entrySet()) { + data.append('&') + .append(parameter.getKey()) + .append('=') + .append(URLEncoder.encode(parameter.getValue(), "UTF-8")); } + + this.customParameters = data.toString(); } catch (UnsupportedEncodingException e) { - this.customParameters = ""; - return; + throw new AlchemyApiException(e); } - this.customParameters = data.toString(); } public String getUrlQuery() { - String retString = ""; + final StringBuilder urlQuery = new StringBuilder(); try { - if (url != null) retString += "&url=" + URLEncoder.encode(url.toString(), "UTF-8"); - if (html != null) retString += "&html=" + URLEncoder.encode(html, "UTF-8"); - if (text != null) retString += "&text=" + URLEncoder.encode(text, "UTF-8"); - if (customParameters != null) retString += customParameters; - if (outputMode != null) retString += "&outputMode=" + outputMode; + if (url != null) { urlQuery.append("&url=").append(URLEncoder.encode(url, "UTF-8")); } + if (html != null) { urlQuery.append( "&html=").append(URLEncoder.encode(html, "UTF-8")); } + if (text != null) { urlQuery.append("&text=").append(URLEncoder.encode(text, "UTF-8")); } + if (customParameters != null) { urlQuery.append(customParameters); } + if (outputMode != null) { urlQuery.append("&outputMode=").append(outputMode); } + return urlQuery.toString(); + } catch (UnsupportedEncodingException e) { - retString = ""; + throw new AlchemyApiException(e); } - return retString; } + + private static void validateUrl(final String url) { + try { + new URL(url); + } catch (MalformedURLException e) { + throw new AlchemyApiException(e); + } + } + + private static void validateHtml(final String html) { + if(isBlank(html)) { throw new AlchemyApiException("Html must not be blank"); } + } + + private static void validateText(final String text) { + if(isBlank(text)) { throw new AlchemyApiException("Text must not be blank"); } + } + } diff --git a/src/test/java/com/alchemyapi/STestAuthor.java b/src/test/java/com/alchemyapi/STestAuthor.java index 21657c9..93de8ec 100644 --- a/src/test/java/com/alchemyapi/STestAuthor.java +++ b/src/test/java/com/alchemyapi/STestAuthor.java @@ -1,9 +1,9 @@ package com.alchemyapi; import com.alchemyapi.api.AlchemyApi; -import com.alchemyapi.helpers.TestApiFactory; import com.alchemyapi.helpers.DocumentUtils; import com.alchemyapi.helpers.ResourceUtils; +import com.alchemyapi.helpers.TestApiFactory; import org.junit.Test; import org.w3c.dom.Document; import org.xml.sax.SAXException; @@ -12,7 +12,6 @@ import javax.xml.xpath.XPathExpressionException; import java.io.File; import java.io.IOException; -import java.net.URL; public class STestAuthor { @@ -22,13 +21,13 @@ public class STestAuthor { public void parseFromTestData() throws SAXException, ParserConfigurationException, XPathExpressionException, IOException { final String html = ResourceUtils.toString("data/example.html"); - final Document document = alchemyApi.htmlGetAuthor(html, new URL("http://www.test.com/")); + final Document document = alchemyApi.htmlGetAuthor(html, "http://www.test.com/"); System.out.println(DocumentUtils.toString(document)); } @Test public void parseFromUrl() throws SAXException, ParserConfigurationException, XPathExpressionException, IOException { - final Document document = alchemyApi.urlGetAuthor(new URL("http://www.politico.com/blogs/media/2012/02/detroit-news-ed-upset-over-romney-edit-115247.html")); + final Document document = alchemyApi.urlGetAuthor("http://www.politico.com/blogs/media/2012/02/detroit-news-ed-upset-over-romney-edit-115247.html"); System.out.println(DocumentUtils.toString(document)); } diff --git a/src/test/java/com/alchemyapi/STestSentiment.java b/src/test/java/com/alchemyapi/STestSentiment.java index 544c95d..ad4b0ac 100644 --- a/src/test/java/com/alchemyapi/STestSentiment.java +++ b/src/test/java/com/alchemyapi/STestSentiment.java @@ -15,15 +15,14 @@ import javax.xml.xpath.XPathExpressionException; import java.io.File; import java.io.IOException; -import java.net.URL; public class STestSentiment { private final AlchemyApi alchemyApi = TestApiFactory.build(new File(System.getProperty("user.home"), ".alchemy/api.key")); @Test - public void url() throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { - final Document document = alchemyApi.urlGetTextSentiment(new URL("http://www.techcrunch.com/")); + public void url() { + final Document document = alchemyApi.urlGetTextSentiment("http://www.techcrunch.com/"); System.out.println(DocumentUtils.toString(document)); } @@ -36,7 +35,7 @@ public void text() throws SAXException, ParserConfigurationException, XPathExpre @Test public void file() throws SAXException, ParserConfigurationException, XPathExpressionException, IOException { final String html = ResourceUtils.toString("data/example.html"); - final Document document = alchemyApi.htmlGetTextSentiment(html, new URL("http://www.test.com/")); + final Document document = alchemyApi.htmlGetTextSentiment(html, "http://www.test.com/"); System.out.println(DocumentUtils.toString(document)); } @@ -64,7 +63,7 @@ public void entityTargetedSentimentUrl() throws SAXException, ParserConfiguratio sentimentParams.setShowSourceText(true); final Document document = alchemyApi.urlGetTargetedSentiment( - new URL("http://techcrunch.com/2012/03/01/keen-on-anand-rajaraman-how-walmart-wants-to-leapfrog-over-amazon-tctv/"), + "http://techcrunch.com/2012/03/01/keen-on-anand-rajaraman-how-walmart-wants-to-leapfrog-over-amazon-tctv/", "Walmart", sentimentParams); System.out.print(DocumentUtils.toString(document)); @@ -76,7 +75,7 @@ public void entityTargetedSentimentHtml() throws SAXException, ParserConfigurati sentimentParams.setShowSourceText(true); final String html = ResourceUtils.toString("data/example.html"); - final Document document2 = alchemyApi.htmlGetTargetedSentiment(html, new URL("http://www.test.com/"), "WujWuj", sentimentParams); + final Document document2 = alchemyApi.htmlGetTargetedSentiment(html, "http://www.test.com/", "WujWuj", sentimentParams); System.out.print(DocumentUtils.toString(document2)); } From 6d5396f0528dfbd09162cd81d2fc6c3eed89e314 Mon Sep 17 00:00:00 2001 From: Kenny Cason Date: Sun, 2 Aug 2015 05:34:31 -0500 Subject: [PATCH 08/11] #8 use jsoup over default java xml libs --- pom.xml | 6 ++ .../java/com/alchemyapi/api/AlchemyApi.java | 97 ++++++++----------- .../alchemyapi/api/parameters/Parameters.java | 9 ++ src/test/java/com/alchemyapi/STestAuthor.java | 16 +-- .../java/com/alchemyapi/STestSentiment.java | 33 +++---- .../com/alchemyapi/helpers/DocumentUtils.java | 35 ------- 6 files changed, 74 insertions(+), 122 deletions(-) delete mode 100644 src/test/java/com/alchemyapi/helpers/DocumentUtils.java diff --git a/pom.xml b/pom.xml index 4f12a06..4175ffd 100644 --- a/pom.xml +++ b/pom.xml @@ -24,6 +24,12 @@ commons-lang3 3.4 + + org.jsoup + jsoup + 1.8.2 + + log4j diff --git a/src/main/java/com/alchemyapi/api/AlchemyApi.java b/src/main/java/com/alchemyapi/api/AlchemyApi.java index 4a4e2d1..58d580a 100644 --- a/src/main/java/com/alchemyapi/api/AlchemyApi.java +++ b/src/main/java/com/alchemyapi/api/AlchemyApi.java @@ -14,27 +14,22 @@ import com.alchemyapi.api.parameters.TargetedSentimentParameters; import com.alchemyapi.api.parameters.TaxonomyParameters; import com.alchemyapi.api.parameters.TextParameters; +import org.apache.commons.io.IOUtils; import org.apache.log4j.Logger; -import org.w3c.dom.Document; -import org.w3c.dom.NodeList; +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; +import org.jsoup.nodes.Element; +import org.jsoup.parser.Parser; import org.xml.sax.SAXException; -import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; -import javax.xml.xpath.XPath; -import javax.xml.xpath.XPathConstants; -import javax.xml.xpath.XPathExpression; import javax.xml.xpath.XPathExpressionException; -import javax.xml.xpath.XPathFactory; -import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.HttpURLConnection; import java.net.URL; import java.nio.charset.Charset; -import static org.apache.commons.lang3.StringUtils.isBlank; -import static org.apache.commons.lang3.StringUtils.isNotBlank; import static org.apache.commons.lang3.StringUtils.length; import static org.apache.commons.lang3.StringUtils.trimToEmpty; @@ -510,32 +505,32 @@ public Document imageGetRankedImageKeywords(final ImageParameters params) { outputStream.write(image); outputStream.close(); - return doRequest(handle, params.getOutputMode()); + return doRequest(handle, params); } catch(IOException e) { throw new AlchemyApiException(e); } } - private Document get(final String callName, final String callPrefix, final Parameters params) { + private Document get(final String callName, final String callPrefix, final Parameters parameters) { try { - final String urlQuery = "?apikey=" + configuration.getApiKey() + params.getUrlQuery(); + final String urlQuery = "?apikey=" + configuration.getApiKey() + parameters.getUrlQuery(); final URL url = new URL(buildBaseApiUrl() + callPrefix + "/" + callName + urlQuery); final HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setDoOutput(true); - return doRequest(httpURLConnection, params.getOutputMode()); + return doRequest(httpURLConnection, parameters); } catch(IOException e) { throw new AlchemyApiException(e); } } - private Document post(final String callName, final String callPrefix, final Parameters params) { + private Document post(final String callName, final String callPrefix, final Parameters parameters) { try { final URL url = new URL(buildBaseApiUrl() + callPrefix + "/" + callName); - final String data = "apikey=" + configuration.getApiKey() + params.getUrlQuery(); + final String data = "apikey=" + configuration.getApiKey() + parameters.getUrlQuery(); final HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setDoOutput(true); @@ -545,7 +540,7 @@ private Document post(final String callName, final String callPrefix, final Para outputStream.write(data.getBytes(Charset.forName("UTF-8"))); outputStream.close(); - return doRequest(httpURLConnection, params.getOutputMode()); + return doRequest(httpURLConnection, parameters); } catch(IOException e) { throw new AlchemyApiException(e); @@ -554,50 +549,36 @@ private Document post(final String callName, final String callPrefix, final Para // TODO add json handling // TODO return pojo with parsed field, but allow a "raw" xml/json getter to protect against api updates - private Document doRequest(final HttpURLConnection httpURLConnection, final String outputMode) { + private Document doRequest(final HttpURLConnection httpURLConnection, final Parameters parameters) { try { - final DataInputStream inputStream = new DataInputStream(httpURLConnection.getInputStream()); - final Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(inputStream); - - inputStream.close(); + final String response = IOUtils.toString(httpURLConnection.getInputStream()); httpURLConnection.disconnect(); - switch (outputMode) { + switch (parameters.getOutputMode()) { case Parameters.OUTPUT_XML: - return parseXml(document); + return parseXml(response, parameters); case Parameters.OUTPUT_RDF: - return praseRdf(document); + return praseRdf(response, parameters); case Parameters.OUTPUT_JSON: throw new AlchemyApiException("Json Response not supported yet"); - } - return document; - } catch (SAXException | ParserConfigurationException | IOException e) { + default: + throw new AlchemyApiException("Unknown output mode, must be one of [xml,rdf,json]"); + } + } catch (IOException e) { throw new AlchemyApiException(e); } } - private Document parseXml(final Document document) { - final XPathFactory factory = XPathFactory.newInstance(); - final String status = getNodeValue(factory, document, "/results/status/text()"); - if (isBlank(status) || !status.equals("OK")) { - final String statusInfo = getNodeValue(factory, document, "/results/statusInfo/text()"); - if (isNotBlank(statusInfo)) { - throw new AlchemyApiException("Error making API call: " + statusInfo); - } - throw new AlchemyApiException("Error making API call: " + status); - } - return document; - } + private Document parseXml(final String response, final Parameters parameters) { + final Document document = Jsoup.parse(response, parameters.getEncoding(), Parser.xmlParser()); - private Document praseRdf(final Document document) { - final XPathFactory factory = XPathFactory.newInstance(); - final String status = getNodeValue(factory, document, "//RDF/Description/ResultStatus/text()"); - if (isBlank(status) || !status.equals("OK")) { - final String statusInfo = getNodeValue(factory, document, "//RDF/Description/ResultStatus/text()"); - if (isNotBlank(statusInfo)) { + final Element status = document.select("results > status").first(); + if (status == null || !status.text().equals("OK")) { + final Element statusInfo = document.select("results > statusInfo").first(); + if (statusInfo != null) { throw new AlchemyApiException("Error making API call: " + statusInfo); } throw new AlchemyApiException("Error making API call: " + status); @@ -605,23 +586,25 @@ private Document praseRdf(final Document document) { return document; } - private String getNodeValue(XPathFactory factory, Document doc, String xpathStr) { - try { - final XPath xpath = factory.newXPath(); - final XPathExpression expr = xpath.compile(xpathStr); - final Object result = expr.evaluate(doc, XPathConstants.NODESET); - final NodeList results = (NodeList) result; - - if(results.getLength() == 0 || results.item(0) == null) { return null; } - return results.item(0).getNodeValue(); + // TODO investigate rdf format + private Document praseRdf(final String response, final Parameters parameters) { + final Document document = Jsoup.parse(response, parameters.getEncoding(), Parser.xmlParser()); - } catch (XPathExpressionException e) { - throw new AlchemyApiException(e); + final Element status = document.select("RDF > Description > ResultStatus").first(); + if (status == null || !status.text().equals("OK")) { + throw new AlchemyApiException("Error making API call: " + status); } + return document; } private String buildBaseApiUrl() { return API_URL.replace("{SUB_DOMAIN}", configuration.getApiSubDomain()); } + private String parseBaseUrl(final HttpURLConnection httpURLConnection) { + final URL url = httpURLConnection.getURL(); + String path = url.getFile().substring(0, url.getFile().lastIndexOf('/')); + return url.getProtocol() + "://" + url.getHost() + path; + } + } diff --git a/src/main/java/com/alchemyapi/api/parameters/Parameters.java b/src/main/java/com/alchemyapi/api/parameters/Parameters.java index 8004046..cceaaf9 100644 --- a/src/main/java/com/alchemyapi/api/parameters/Parameters.java +++ b/src/main/java/com/alchemyapi/api/parameters/Parameters.java @@ -21,6 +21,7 @@ public class Parameters { private String text; private String outputMode = OUTPUT_XML; // TODO make json default private String customParameters; + private String encoding = "UTF-8"; public String getUrl() { return url; @@ -49,6 +50,14 @@ public void setText(String text) { this.text = text; } + public String getEncoding() { + return encoding; + } + + public void setEncoding(final String encoding) { + this.encoding = encoding; + } + public String getOutputMode() { return outputMode; } diff --git a/src/test/java/com/alchemyapi/STestAuthor.java b/src/test/java/com/alchemyapi/STestAuthor.java index 93de8ec..265788d 100644 --- a/src/test/java/com/alchemyapi/STestAuthor.java +++ b/src/test/java/com/alchemyapi/STestAuthor.java @@ -1,34 +1,28 @@ package com.alchemyapi; import com.alchemyapi.api.AlchemyApi; -import com.alchemyapi.helpers.DocumentUtils; import com.alchemyapi.helpers.ResourceUtils; import com.alchemyapi.helpers.TestApiFactory; +import org.jsoup.nodes.Document; import org.junit.Test; -import org.w3c.dom.Document; -import org.xml.sax.SAXException; -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.xpath.XPathExpressionException; import java.io.File; -import java.io.IOException; public class STestAuthor { private final AlchemyApi alchemyApi = TestApiFactory.build(new File(System.getProperty("user.home"), ".alchemy/api.key")); @Test - public void parseFromTestData() throws SAXException, ParserConfigurationException, XPathExpressionException, IOException { + public void parseFromTestData() { final String html = ResourceUtils.toString("data/example.html"); - final Document document = alchemyApi.htmlGetAuthor(html, "http://www.test.com/"); - System.out.println(DocumentUtils.toString(document)); + System.out.println(document); } @Test - public void parseFromUrl() throws SAXException, ParserConfigurationException, XPathExpressionException, IOException { + public void parseFromUrl() { final Document document = alchemyApi.urlGetAuthor("http://www.politico.com/blogs/media/2012/02/detroit-news-ed-upset-over-romney-edit-115247.html"); - System.out.println(DocumentUtils.toString(document)); + System.out.println(document); } } diff --git a/src/test/java/com/alchemyapi/STestSentiment.java b/src/test/java/com/alchemyapi/STestSentiment.java index ad4b0ac..e9f1dd2 100644 --- a/src/test/java/com/alchemyapi/STestSentiment.java +++ b/src/test/java/com/alchemyapi/STestSentiment.java @@ -4,17 +4,12 @@ import com.alchemyapi.api.parameters.KeywordParameters; import com.alchemyapi.api.parameters.NamedEntityParameters; import com.alchemyapi.api.parameters.TargetedSentimentParameters; -import com.alchemyapi.helpers.DocumentUtils; import com.alchemyapi.helpers.ResourceUtils; import com.alchemyapi.helpers.TestApiFactory; +import org.jsoup.nodes.Document; import org.junit.Test; -import org.w3c.dom.Document; -import org.xml.sax.SAXException; -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.xpath.XPathExpressionException; import java.io.File; -import java.io.IOException; public class STestSentiment { @@ -23,42 +18,42 @@ public class STestSentiment { @Test public void url() { final Document document = alchemyApi.urlGetTextSentiment("http://www.techcrunch.com/"); - System.out.println(DocumentUtils.toString(document)); + System.out.println(document); } @Test - public void text() throws SAXException, ParserConfigurationException, XPathExpressionException, IOException { + public void text() { final Document document = alchemyApi.textGetTextSentiment("That hat is ridiculous, Charles."); - System.out.println(DocumentUtils.toString(document)); + System.out.println(document); } @Test - public void file() throws SAXException, ParserConfigurationException, XPathExpressionException, IOException { + public void file() { final String html = ResourceUtils.toString("data/example.html"); final Document document = alchemyApi.htmlGetTextSentiment(html, "http://www.test.com/"); - System.out.println(DocumentUtils.toString(document)); + System.out.println(document); } @Test - public void entityTargetedSentimentText() throws SAXException, ParserConfigurationException, XPathExpressionException, IOException { + public void entityTargetedSentimentText() { final NamedEntityParameters entityParams = new NamedEntityParameters(); entityParams.setSentiment(true); final Document document = alchemyApi.textGetRankedNamedEntities("That Mike Tyson is such a sweetheart.", entityParams); - System.out.println(DocumentUtils.toString(document)); + System.out.println(document); final KeywordParameters keywordParams = new KeywordParameters(); keywordParams.setSentiment(true); final Document document2 = alchemyApi.textGetRankedKeywords("That Mike Tyson is such a sweetheart.", keywordParams); - System.out.println(DocumentUtils.toString(document2)); + System.out.println(document2); final TargetedSentimentParameters sentimentParams = new TargetedSentimentParameters(); sentimentParams.setShowSourceText(true); final Document document3 = alchemyApi.textGetTargetedSentiment("This car is terrible.", "car", sentimentParams); - System.out.print(DocumentUtils.toString(document3)); + System.out.print(document3); } @Test - public void entityTargetedSentimentUrl() throws SAXException, ParserConfigurationException, XPathExpressionException, IOException { + public void entityTargetedSentimentUrl() { final TargetedSentimentParameters sentimentParams = new TargetedSentimentParameters(); sentimentParams.setShowSourceText(true); @@ -66,17 +61,17 @@ public void entityTargetedSentimentUrl() throws SAXException, ParserConfiguratio "http://techcrunch.com/2012/03/01/keen-on-anand-rajaraman-how-walmart-wants-to-leapfrog-over-amazon-tctv/", "Walmart", sentimentParams); - System.out.print(DocumentUtils.toString(document)); + System.out.print(document); } @Test - public void entityTargetedSentimentHtml() throws SAXException, ParserConfigurationException, XPathExpressionException, IOException { + public void entityTargetedSentimentHtml() { final TargetedSentimentParameters sentimentParams = new TargetedSentimentParameters(); sentimentParams.setShowSourceText(true); final String html = ResourceUtils.toString("data/example.html"); final Document document2 = alchemyApi.htmlGetTargetedSentiment(html, "http://www.test.com/", "WujWuj", sentimentParams); - System.out.print(DocumentUtils.toString(document2)); + System.out.print(document2); } } diff --git a/src/test/java/com/alchemyapi/helpers/DocumentUtils.java b/src/test/java/com/alchemyapi/helpers/DocumentUtils.java deleted file mode 100644 index 0493f7b..0000000 --- a/src/test/java/com/alchemyapi/helpers/DocumentUtils.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.alchemyapi.helpers; - -import org.w3c.dom.Document; - -import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerException; -import javax.xml.transform.TransformerFactory; -import javax.xml.transform.dom.DOMSource; -import javax.xml.transform.stream.StreamResult; -import java.io.StringWriter; - -/** - * Created by kenny - */ -// replace with HtmlSoup -public class DocumentUtils { - - private static final TransformerFactory TRANSFORMER_FACTORY = TransformerFactory.newInstance(); - - public static String toString(final Document document) { - try { - final DOMSource domSource = new DOMSource(document); - final StringWriter writer = new StringWriter(); - final StreamResult result = new StreamResult(writer); - final Transformer transformer = TRANSFORMER_FACTORY.newTransformer(); - transformer.transform(domSource, result); - - return writer.toString(); - } catch (TransformerException e) { - e.printStackTrace(); - return null; - } - } - -} From a655d0594c6be9c66cea4c9b59c2ad755f3e21da Mon Sep 17 00:00:00 2001 From: Kenny Cason Date: Sun, 2 Aug 2015 18:22:24 -0500 Subject: [PATCH 09/11] #8 update all tests + general refactoring --- pom.xml | 17 +++- .../java/com/alchemyapi/api/AlchemyApi.java | 47 +++++---- src/test/java/com/alchemyapi/STestAuthor.java | 7 +- .../java/com/alchemyapi/STestCategory.java | 48 +++++++++ .../java/com/alchemyapi/STestCombined.java | 39 ++++++++ .../java/com/alchemyapi/STestConcept.java | 38 ++++++++ .../com/alchemyapi/STestConstraintQuery.java | 31 ++++++ src/test/java/com/alchemyapi/STestEntity.java | 38 ++++++++ .../java/com/alchemyapi/STestFeedLinks.java | 31 ++++++ src/test/java/com/alchemyapi/STestImage.java | 41 ++++++++ .../java/com/alchemyapi/STestKeyword.java | 38 ++++++++ .../java/com/alchemyapi/STestLanguage.java | 49 ++++++++++ .../com/alchemyapi/STestMicroformats.java | 31 ++++++ .../java/com/alchemyapi/STestRelations.java | 57 +++++++++++ .../java/com/alchemyapi/STestSentiment.java | 5 +- .../java/com/alchemyapi/STestTaxonomy.java | 38 ++++++++ .../java/com/alchemyapi/STestTextExtract.java | 75 ++++++++++++++ .../com/alchemyapi/helpers/ResourceUtils.java | 9 ++ src/test/resources/to_fix/CategoryTest.java | 92 ------------------ src/test/resources/to_fix/CombinedTest.java | 84 ---------------- src/test/resources/to_fix/ConceptTest.java | 79 --------------- .../resources/to_fix/ConstraintQueryTest.java | 75 -------------- src/test/resources/to_fix/EntityTest.java | 82 ---------------- src/test/resources/to_fix/FeedLinksTest.java | 76 --------------- src/test/resources/to_fix/ImageTest.java | 78 --------------- src/test/resources/to_fix/KeywordTest.java | 80 --------------- src/test/resources/to_fix/LanguageTest.java | 81 ---------------- .../resources/to_fix/MicroformatsTest.java | 81 ---------------- src/test/resources/to_fix/ParameterTest.java | 87 ----------------- src/test/resources/to_fix/RelationsTest.java | 97 ------------------- src/test/resources/to_fix/TaxonomyTest.java | 82 ---------------- .../resources/to_fix/TextExtractTest.java | 96 ------------------ 32 files changed, 612 insertions(+), 1197 deletions(-) create mode 100644 src/test/java/com/alchemyapi/STestCategory.java create mode 100644 src/test/java/com/alchemyapi/STestCombined.java create mode 100644 src/test/java/com/alchemyapi/STestConcept.java create mode 100644 src/test/java/com/alchemyapi/STestConstraintQuery.java create mode 100644 src/test/java/com/alchemyapi/STestEntity.java create mode 100644 src/test/java/com/alchemyapi/STestFeedLinks.java create mode 100644 src/test/java/com/alchemyapi/STestImage.java create mode 100644 src/test/java/com/alchemyapi/STestKeyword.java create mode 100644 src/test/java/com/alchemyapi/STestLanguage.java create mode 100644 src/test/java/com/alchemyapi/STestMicroformats.java create mode 100644 src/test/java/com/alchemyapi/STestRelations.java create mode 100644 src/test/java/com/alchemyapi/STestTaxonomy.java create mode 100644 src/test/java/com/alchemyapi/STestTextExtract.java delete mode 100644 src/test/resources/to_fix/CategoryTest.java delete mode 100644 src/test/resources/to_fix/CombinedTest.java delete mode 100644 src/test/resources/to_fix/ConceptTest.java delete mode 100644 src/test/resources/to_fix/ConstraintQueryTest.java delete mode 100644 src/test/resources/to_fix/EntityTest.java delete mode 100644 src/test/resources/to_fix/FeedLinksTest.java delete mode 100644 src/test/resources/to_fix/ImageTest.java delete mode 100644 src/test/resources/to_fix/KeywordTest.java delete mode 100644 src/test/resources/to_fix/LanguageTest.java delete mode 100644 src/test/resources/to_fix/MicroformatsTest.java delete mode 100644 src/test/resources/to_fix/ParameterTest.java delete mode 100644 src/test/resources/to_fix/RelationsTest.java delete mode 100644 src/test/resources/to_fix/TaxonomyTest.java delete mode 100644 src/test/resources/to_fix/TextExtractTest.java diff --git a/pom.xml b/pom.xml index 4175ffd..8f2396d 100644 --- a/pom.xml +++ b/pom.xml @@ -14,6 +14,11 @@ + + log4j + log4j + 1.2.17 + commons-io commons-io @@ -24,19 +29,21 @@ commons-lang3 3.4 + + org.jsoup jsoup 1.8.2 - - - log4j - log4j - 1.2.17 + org.json + json + 20141113 + + junit junit diff --git a/src/main/java/com/alchemyapi/api/AlchemyApi.java b/src/main/java/com/alchemyapi/api/AlchemyApi.java index 58d580a..8e13447 100644 --- a/src/main/java/com/alchemyapi/api/AlchemyApi.java +++ b/src/main/java/com/alchemyapi/api/AlchemyApi.java @@ -15,15 +15,14 @@ import com.alchemyapi.api.parameters.TaxonomyParameters; import com.alchemyapi.api.parameters.TextParameters; import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.StringUtils; import org.apache.log4j.Logger; +import org.json.JSONObject; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.parser.Parser; -import org.xml.sax.SAXException; -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.xpath.XPathExpressionException; import java.io.DataOutputStream; import java.io.IOException; import java.net.HttpURLConnection; @@ -33,6 +32,9 @@ import static org.apache.commons.lang3.StringUtils.length; import static org.apache.commons.lang3.StringUtils.trimToEmpty; +/** + * Created by kenny + */ public class AlchemyApi { private static final Logger LOGGER = Logger.getLogger(AlchemyApi.class); @@ -146,8 +148,7 @@ public Document htmlGetRankedKeywords(final String html, final String url, final return post("HTMLGetRankedKeywords", "html", params); } - public Document textGetRankedKeywords(final String text) throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException { + public Document textGetRankedKeywords(final String text) { return textGetRankedKeywords(text, new KeywordParameters()); } @@ -403,11 +404,11 @@ public Document urlGetRelations(final String url, final RelationParameters param return get("URLGetRelations", "url", params); } - public Document HTMLGetRelations(final String html, final String url) { - return HTMLGetRelations(html, url, new RelationParameters()); + public Document htmlGetRelations(final String html, final String url) { + return htmlGetRelations(html, url, new RelationParameters()); } - public Document HTMLGetRelations(final String html, final String url, final RelationParameters params) { + public Document htmlGetRelations(final String html, final String url, final RelationParameters params) { params.setUrl(url); params.setHtml(html); return post("HTMLGetRelations", "html", params); @@ -547,7 +548,7 @@ private Document post(final String callName, final String callPrefix, final Para } } - // TODO add json handling + // TODO support json, by default // TODO return pojo with parsed field, but allow a "raw" xml/json getter to protect against api updates private Document doRequest(final HttpURLConnection httpURLConnection, final Parameters parameters) { try { @@ -562,7 +563,8 @@ private Document doRequest(final HttpURLConnection httpURLConnection, final Para return praseRdf(response, parameters); case Parameters.OUTPUT_JSON: - throw new AlchemyApiException("Json Response not supported yet"); + // return parseJson(response, parameters); + throw new AlchemyApiException("Json responses are not currently supported"); default: throw new AlchemyApiException("Unknown output mode, must be one of [xml,rdf,json]"); @@ -572,6 +574,20 @@ private Document doRequest(final HttpURLConnection httpURLConnection, final Para } } + private JSONObject parseJson(final String response, final Parameters parameters) { + final JSONObject json = new JSONObject(response); + if(json.has("results")) { + final JSONObject results = json.getJSONObject("results"); + if(!StringUtils.equals(results.optString("status"), "OK")) { + if(results.has("statusInfo")) { + throw new AlchemyApiException("Error making API call: " + results.optString("statusInfo")); + } + throw new AlchemyApiException("Error making API call: " + results.optString("status")); + } + } + return json; + } + private Document parseXml(final String response, final Parameters parameters) { final Document document = Jsoup.parse(response, parameters.getEncoding(), Parser.xmlParser()); @@ -586,11 +602,10 @@ private Document parseXml(final String response, final Parameters parameters) { return document; } - // TODO investigate rdf format private Document praseRdf(final String response, final Parameters parameters) { final Document document = Jsoup.parse(response, parameters.getEncoding(), Parser.xmlParser()); - - final Element status = document.select("RDF > Description > ResultStatus").first(); + System.out.println("RAW: " + response); + final Element status = document.select("rdf|RDF > rdf|Description > aapi|ResultStatus").first(); if (status == null || !status.text().equals("OK")) { throw new AlchemyApiException("Error making API call: " + status); } @@ -601,10 +616,4 @@ private String buildBaseApiUrl() { return API_URL.replace("{SUB_DOMAIN}", configuration.getApiSubDomain()); } - private String parseBaseUrl(final HttpURLConnection httpURLConnection) { - final URL url = httpURLConnection.getURL(); - String path = url.getFile().substring(0, url.getFile().lastIndexOf('/')); - return url.getProtocol() + "://" + url.getHost() + path; - } - } diff --git a/src/test/java/com/alchemyapi/STestAuthor.java b/src/test/java/com/alchemyapi/STestAuthor.java index 265788d..da12350 100644 --- a/src/test/java/com/alchemyapi/STestAuthor.java +++ b/src/test/java/com/alchemyapi/STestAuthor.java @@ -8,19 +8,22 @@ import java.io.File; +/** + * Created by kenny + */ public class STestAuthor { private final AlchemyApi alchemyApi = TestApiFactory.build(new File(System.getProperty("user.home"), ".alchemy/api.key")); @Test - public void parseFromTestData() { + public void html() { final String html = ResourceUtils.toString("data/example.html"); final Document document = alchemyApi.htmlGetAuthor(html, "http://www.test.com/"); System.out.println(document); } @Test - public void parseFromUrl() { + public void url() { final Document document = alchemyApi.urlGetAuthor("http://www.politico.com/blogs/media/2012/02/detroit-news-ed-upset-over-romney-edit-115247.html"); System.out.println(document); } diff --git a/src/test/java/com/alchemyapi/STestCategory.java b/src/test/java/com/alchemyapi/STestCategory.java new file mode 100644 index 0000000..3fca07b --- /dev/null +++ b/src/test/java/com/alchemyapi/STestCategory.java @@ -0,0 +1,48 @@ +package com.alchemyapi; + +import com.alchemyapi.api.AlchemyApi; +import com.alchemyapi.api.parameters.CategoryParameters; +import com.alchemyapi.api.parameters.Parameters; +import com.alchemyapi.helpers.ResourceUtils; +import com.alchemyapi.helpers.TestApiFactory; +import org.jsoup.nodes.Document; +import org.junit.Test; + +import java.io.File; + +/** + * Created by kenny + */ +public class STestCategory { + + private final AlchemyApi alchemyApi = TestApiFactory.build(new File(System.getProperty("user.home"), ".alchemy/api.key")); + + @Test + public void url() { + final Document document = alchemyApi.urlGetCategory("http://www.techcrunch.com/"); + System.out.println(document); + } + + @Test + public void text() { + final Document document = alchemyApi.textGetCategory("Latest on the War in Iraq."); + System.out.println(document); + } + + @Test + public void html() { + final String html = ResourceUtils.toString("data/example.html"); + final Document document = alchemyApi.htmlGetCategory(html, "http://www.test.com/"); + System.out.println(document); + } + + @Test + public void htmlRdfFormat() { + final String html = ResourceUtils.toString("data/example.html"); + final CategoryParameters categoryParameters = new CategoryParameters(); + categoryParameters.setOutputMode(Parameters.OUTPUT_RDF); + final Document document2 = alchemyApi.htmlGetCategory(html, "http://www.test.com/", categoryParameters); + System.out.println(document2); + } + +} diff --git a/src/test/java/com/alchemyapi/STestCombined.java b/src/test/java/com/alchemyapi/STestCombined.java new file mode 100644 index 0000000..79f1c54 --- /dev/null +++ b/src/test/java/com/alchemyapi/STestCombined.java @@ -0,0 +1,39 @@ +package com.alchemyapi; + +import com.alchemyapi.api.AlchemyApi; +import com.alchemyapi.api.parameters.CombinedParameters; +import com.alchemyapi.helpers.TestApiFactory; +import org.jsoup.nodes.Document; +import org.junit.Test; + +import java.io.File; + +/** + * Created by kenny + */ +public class STestCombined { + + private final AlchemyApi alchemyApi = TestApiFactory.build(new File(System.getProperty("user.home"), ".alchemy/api.key")); + + @Test + public void url() { + final Document document = alchemyApi.urlGetCombined("http://www.techcrunch.com/"); + System.out.println(document); + } + + @Test + public void text() { + final Document document = alchemyApi.textGetCombined( + "Hello there, my name is Bob Jones. I live in the United States of America. Where do you live, Fred?"); + System.out.println(document); + + // Only extract entities & keywords + final CombinedParameters combinedParams = new CombinedParameters(); + combinedParams.setSentiment(true); + combinedParams.setExtract("entity"); + combinedParams.setExtract("keyword"); + final Document document2 = alchemyApi.textGetCombined("Madonna enjoys tasty Pepsi. I love her style.", combinedParams); + System.out.println(document2); + } + +} diff --git a/src/test/java/com/alchemyapi/STestConcept.java b/src/test/java/com/alchemyapi/STestConcept.java new file mode 100644 index 0000000..a2e3b16 --- /dev/null +++ b/src/test/java/com/alchemyapi/STestConcept.java @@ -0,0 +1,38 @@ +package com.alchemyapi; + +import com.alchemyapi.api.AlchemyApi; +import com.alchemyapi.helpers.ResourceUtils; +import com.alchemyapi.helpers.TestApiFactory; +import org.jsoup.nodes.Document; +import org.junit.Test; + +import java.io.File; + +/** + * Created by kenny + */ +public class STestConcept { + + private final AlchemyApi alchemyApi = TestApiFactory.build(new File(System.getProperty("user.home"), ".alchemy/api.key")); + + @Test + public void url() { + final Document document = alchemyApi.urlGetRankedConcepts("http://www.techcrunch.com/"); + System.out.println(document); + } + + @Test + public void text() { + final Document document = alchemyApi.textGetRankedConcepts( + "This thing has a steering wheel, tires, and an engine. Do you know what it is?"); + System.out.println(document); + } + + @Test + public void html() { + final String html = ResourceUtils.toString("data/example.html"); + final Document document = alchemyApi.htmlGetRankedConcepts(html, "http://www.test.com/"); + System.out.println(document); + } + +} diff --git a/src/test/java/com/alchemyapi/STestConstraintQuery.java b/src/test/java/com/alchemyapi/STestConstraintQuery.java new file mode 100644 index 0000000..cf2b406 --- /dev/null +++ b/src/test/java/com/alchemyapi/STestConstraintQuery.java @@ -0,0 +1,31 @@ +package com.alchemyapi; + +import com.alchemyapi.api.AlchemyApi; +import com.alchemyapi.helpers.ResourceUtils; +import com.alchemyapi.helpers.TestApiFactory; +import org.jsoup.nodes.Document; +import org.junit.Test; + +import java.io.File; + +/** + * Created by kenny + */ +public class STestConstraintQuery { + + private final AlchemyApi alchemyApi = TestApiFactory.build(new File(System.getProperty("user.home"), ".alchemy/api.key")); + + @Test + public void url() { + final Document document = alchemyApi.urlGetConstraintQuery("http://microformats.org/wiki/hcard", "1st link"); + System.out.println(document); + } + + @Test + public void html() { + final String html = ResourceUtils.toString("data/example.html"); + final Document document = alchemyApi.htmlGetConstraintQuery(html, "http://www.test.com/", "1st link"); + System.out.println(document); + } + +} diff --git a/src/test/java/com/alchemyapi/STestEntity.java b/src/test/java/com/alchemyapi/STestEntity.java new file mode 100644 index 0000000..749aaa8 --- /dev/null +++ b/src/test/java/com/alchemyapi/STestEntity.java @@ -0,0 +1,38 @@ +package com.alchemyapi; + +import com.alchemyapi.api.AlchemyApi; +import com.alchemyapi.helpers.ResourceUtils; +import com.alchemyapi.helpers.TestApiFactory; +import org.jsoup.nodes.Document; +import org.junit.Test; + +import java.io.File; + +/** + * Created by kenny + */ +public class STestEntity { + + private final AlchemyApi alchemyApi = TestApiFactory.build(new File(System.getProperty("user.home"), ".alchemy/api.key")); + + @Test + public void url() { + final Document document = alchemyApi.urlGetRankedNamedEntities("http://www.techcrunch.com/"); + System.out.println(document); + } + + @Test + public void text() { + final Document document = alchemyApi.textGetRankedNamedEntities( + "Hello there, my name is Bob Jones. I live in the United States of America. Where do you live, Fred?"); + System.out.println(document); + } + + @Test + public void html() { + final String html = ResourceUtils.toString("data/example.html"); + final Document document = alchemyApi.htmlGetRankedNamedEntities(html, "http://www.test.com/"); + System.out.println(document); + } + +} diff --git a/src/test/java/com/alchemyapi/STestFeedLinks.java b/src/test/java/com/alchemyapi/STestFeedLinks.java new file mode 100644 index 0000000..27d6e4b --- /dev/null +++ b/src/test/java/com/alchemyapi/STestFeedLinks.java @@ -0,0 +1,31 @@ +package com.alchemyapi; + +import com.alchemyapi.api.AlchemyApi; +import com.alchemyapi.helpers.ResourceUtils; +import com.alchemyapi.helpers.TestApiFactory; +import org.jsoup.nodes.Document; +import org.junit.Test; + +import java.io.File; + +/** + * Created by kenny + */ +public class STestFeedLinks { + + private final AlchemyApi alchemyApi = TestApiFactory.build(new File(System.getProperty("user.home"), ".alchemy/api.key")); + + @Test + public void url() { + final Document document = alchemyApi.urlGetFeedLinks("http://www.techcrunch.com/"); + System.out.println(document); + } + + @Test + public void html() { + final String html = ResourceUtils.toString("data/example.html"); + final Document document = alchemyApi.htmlGetFeedLinks(html, "http://www.test.com/"); + System.out.println(document); + } + +} diff --git a/src/test/java/com/alchemyapi/STestImage.java b/src/test/java/com/alchemyapi/STestImage.java new file mode 100644 index 0000000..9ca8b25 --- /dev/null +++ b/src/test/java/com/alchemyapi/STestImage.java @@ -0,0 +1,41 @@ +package com.alchemyapi; + +import com.alchemyapi.api.AlchemyApi; +import com.alchemyapi.api.parameters.ImageParameters; +import com.alchemyapi.helpers.ResourceUtils; +import com.alchemyapi.helpers.TestApiFactory; +import org.jsoup.nodes.Document; +import org.junit.Test; + +import java.io.File; + +/** + * Created by kenny + */ +public class STestImage { + + private final AlchemyApi alchemyApi = TestApiFactory.build(new File(System.getProperty("user.home"), ".alchemy/api.key")); + + @Test + public void url() { + final Document document = alchemyApi.urlGetImage("http://www.techcrunch.com/"); + System.out.println(document); + } + + @Test + public void imageUrl() { + final Document document = alchemyApi.urlGetRankedImageKeywords("http://farm4.staticflickr.com/3726/11043305726_fdcb7785ec_m.jpg"); + System.out.println(document); + } + + @Test + public void imageFile() { + final byte[] imageBytes = ResourceUtils.toBytes("data/cat.jpg"); + final ImageParameters imageParams = new ImageParameters(); + imageParams.setImage(imageBytes); + imageParams.setImagePostMode(ImageParameters.RAW); + final Document document = alchemyApi.imageGetRankedImageKeywords(imageParams); + System.out.println(document); + } + +} diff --git a/src/test/java/com/alchemyapi/STestKeyword.java b/src/test/java/com/alchemyapi/STestKeyword.java new file mode 100644 index 0000000..49e7cd4 --- /dev/null +++ b/src/test/java/com/alchemyapi/STestKeyword.java @@ -0,0 +1,38 @@ +package com.alchemyapi; + +import com.alchemyapi.api.AlchemyApi; +import com.alchemyapi.helpers.ResourceUtils; +import com.alchemyapi.helpers.TestApiFactory; +import org.jsoup.nodes.Document; +import org.junit.Test; + +import java.io.File; + +/** + * Created by kenny + */ +public class STestKeyword { + + private final AlchemyApi alchemyApi = TestApiFactory.build(new File(System.getProperty("user.home"), ".alchemy/api.key")); + + @Test + public void url() { + final Document document = alchemyApi.urlGetRankedKeywords("http://www.techcrunch.com/"); + System.out.println(document); + } + + @Test + public void text() { + final Document document = alchemyApi.textGetRankedKeywords( + "Hello there, my name is Bob Jones. I live in the United States of America. Where do you live, Fred?"); + System.out.println(document); + } + + @Test + public void html() { + final String html = ResourceUtils.toString("data/example.html"); + final Document document = alchemyApi.htmlGetRankedKeywords(html, "http://www.test.com/"); + System.out.println(document); + } + +} diff --git a/src/test/java/com/alchemyapi/STestLanguage.java b/src/test/java/com/alchemyapi/STestLanguage.java new file mode 100644 index 0000000..8dd86fd --- /dev/null +++ b/src/test/java/com/alchemyapi/STestLanguage.java @@ -0,0 +1,49 @@ +package com.alchemyapi; + +import com.alchemyapi.api.AlchemyApi; +import com.alchemyapi.helpers.ResourceUtils; +import com.alchemyapi.helpers.TestApiFactory; +import org.jsoup.nodes.Document; +import org.junit.Test; + +import java.io.File; + +/** + * Created by kenny + */ +public class STestLanguage { + + private final AlchemyApi alchemyApi = TestApiFactory.build(new File(System.getProperty("user.home"), ".alchemy/api.key")); + + @Test + public void url() { + final Document document = alchemyApi.urlGetLanguage("http://news.google.fr/"); + System.out.println(document); + } + + @Test + public void textEnglish() { + final Document document = alchemyApi.textGetLanguage("This is some english language text. What language do you speak?"); + System.out.println(document); + } + + @Test + public void textJapanese() { + final Document document = alchemyApi.textGetLanguage("私の名前はケニー・ケーソンです。プログラミングすることが大好きです。"); + System.out.println(document); + } + + @Test + public void textChinese() { + final Document document = alchemyApi.textGetLanguage("你好,我名字叫肯尼。我很喜欢学外语。"); + System.out.println(document); + } + + @Test + public void html() { + final String html = ResourceUtils.toString("data/example.html"); + final Document document = alchemyApi.htmlGetLanguage(html, "http://www.test.com/"); + System.out.println(document); + } + +} diff --git a/src/test/java/com/alchemyapi/STestMicroformats.java b/src/test/java/com/alchemyapi/STestMicroformats.java new file mode 100644 index 0000000..06985fe --- /dev/null +++ b/src/test/java/com/alchemyapi/STestMicroformats.java @@ -0,0 +1,31 @@ +package com.alchemyapi; + +import com.alchemyapi.api.AlchemyApi; +import com.alchemyapi.helpers.ResourceUtils; +import com.alchemyapi.helpers.TestApiFactory; +import org.jsoup.nodes.Document; +import org.junit.Test; + +import java.io.File; + +/** + * Created by kenny + */ +public class STestMicroformats { + + private final AlchemyApi alchemyApi = TestApiFactory.build(new File(System.getProperty("user.home"), ".alchemy/api.key")); + + @Test + public void url() { + final Document document = alchemyApi.urlGetMicroformats("http://microformats.org/wiki/hcard"); + System.out.println(document); + } + + @Test + public void html() { + final String html = ResourceUtils.toString("data/microformats.html"); + final Document document = alchemyApi.htmlGetMicroformats(html, "http://www.test.com/"); + System.out.println(document); + } + +} diff --git a/src/test/java/com/alchemyapi/STestRelations.java b/src/test/java/com/alchemyapi/STestRelations.java new file mode 100644 index 0000000..c610754 --- /dev/null +++ b/src/test/java/com/alchemyapi/STestRelations.java @@ -0,0 +1,57 @@ +package com.alchemyapi; + +import com.alchemyapi.api.AlchemyApi; +import com.alchemyapi.api.parameters.RelationParameters; +import com.alchemyapi.helpers.ResourceUtils; +import com.alchemyapi.helpers.TestApiFactory; +import org.jsoup.nodes.Document; +import org.junit.Test; + +import java.io.File; + +/** + * Created by kenny + */ +public class STestRelations { + + private final AlchemyApi alchemyApi = TestApiFactory.build(new File(System.getProperty("user.home"), ".alchemy/api.key")); + + @Test + public void url() { + final Document document = alchemyApi.urlGetRelations("http://www.techcrunch.com/"); + System.out.println(document); + } + + @Test + public void text() { + // Extract a ranked list of relations from a text string. + final Document document = alchemyApi.textGetRelations( + "Hello there, my name is Bob Jones. I live in the United States of America. Where do you live, Fred?"); + System.out.println(document); + } + + @Test + public void html() { + final String html = ResourceUtils.toString("data/example.html"); + final Document document = alchemyApi.htmlGetRelations(html, "http://www.test.com/"); + System.out.println(document); + } + + @Test + public void parameterizedText() { + final RelationParameters relationParams = new RelationParameters(); + relationParams.setSentiment(true); + relationParams.setEntities(true); + relationParams.setDisambiguate(true); + relationParams.setSentimentExcludeEntities(true); + final Document document = alchemyApi.textGetRelations("Madonna enjoys tasty Pepsi. I love her style.", relationParams); + System.out.println(document); + + relationParams.setSentiment(true); + relationParams.setRequireEntities(true); + relationParams.setSentimentExcludeEntities(true); + final Document document2 = alchemyApi.textGetRelations("Madonna enjoys tasty Pepsi. I love her style.", relationParams); + System.out.println(document2); + } + +} diff --git a/src/test/java/com/alchemyapi/STestSentiment.java b/src/test/java/com/alchemyapi/STestSentiment.java index e9f1dd2..935b641 100644 --- a/src/test/java/com/alchemyapi/STestSentiment.java +++ b/src/test/java/com/alchemyapi/STestSentiment.java @@ -11,6 +11,9 @@ import java.io.File; +/** + * Created by kenny + */ public class STestSentiment { private final AlchemyApi alchemyApi = TestApiFactory.build(new File(System.getProperty("user.home"), ".alchemy/api.key")); @@ -28,7 +31,7 @@ public void text() { } @Test - public void file() { + public void html() { final String html = ResourceUtils.toString("data/example.html"); final Document document = alchemyApi.htmlGetTextSentiment(html, "http://www.test.com/"); System.out.println(document); diff --git a/src/test/java/com/alchemyapi/STestTaxonomy.java b/src/test/java/com/alchemyapi/STestTaxonomy.java new file mode 100644 index 0000000..3391d59 --- /dev/null +++ b/src/test/java/com/alchemyapi/STestTaxonomy.java @@ -0,0 +1,38 @@ +package com.alchemyapi; + +import com.alchemyapi.api.AlchemyApi; +import com.alchemyapi.helpers.ResourceUtils; +import com.alchemyapi.helpers.TestApiFactory; +import org.jsoup.nodes.Document; +import org.junit.Test; + +import java.io.File; + +/** + * Created by kenny + */ +public class STestTaxonomy { + + private final AlchemyApi alchemyApi = TestApiFactory.build(new File(System.getProperty("user.home"), ".alchemy/api.key")); + + @Test + public void url() { + final Document document = alchemyApi.urlGetTaxonomy("http://www.techcrunch.com/"); + System.out.println(document); + } + + @Test + public void text() { + final Document document = alchemyApi.textGetTaxonomy( + "Hello there, my name is Bob Jones. I live in the United States of America. Where do you live, Fred?"); + System.out.println(document); + } + + @Test + public void html() { + final String html = ResourceUtils.toString("data/example.html"); + final Document document = alchemyApi.htmlGetTaxonomy(html, "http://www.test.com/"); + System.out.println(document); + } + +} diff --git a/src/test/java/com/alchemyapi/STestTextExtract.java b/src/test/java/com/alchemyapi/STestTextExtract.java new file mode 100644 index 0000000..fe9eca4 --- /dev/null +++ b/src/test/java/com/alchemyapi/STestTextExtract.java @@ -0,0 +1,75 @@ +package com.alchemyapi; + +import com.alchemyapi.api.AlchemyApi; +import com.alchemyapi.helpers.ResourceUtils; +import com.alchemyapi.helpers.TestApiFactory; +import org.jsoup.nodes.Document; +import org.junit.Test; + +import java.io.File; + +/** + * Created by kenny + */ +public class STestTextExtract { + + private final AlchemyApi alchemyApi = TestApiFactory.build(new File(System.getProperty("user.home"), ".alchemy/api.key")); + + /** + * Extract page text from a web URL. Ignoring ads, navigation links,and other content. + */ + @Test + public void urlClean() { + final Document document = alchemyApi.urlGetText("http://www.techcrunch.com/"); + System.out.println(document); + } + + /** + * Extract raw page text from a web URL. Including ads, navigation, and other content. + */ + @Test + public void urlRaw() { + final Document document = alchemyApi.urlGetRawText("http://www.techcrunch.com/"); + System.out.println(document); + } + + /** + * Extract a title from a web URL. + */ + @Test + public void urlTitle() { + final Document document = alchemyApi.urlGetTitle("http://www.techcrunch.com/"); + System.out.println(document); + } + + /** + * Extract page text from html. Ignoring ads, navigation links,and other content. + */ + @Test + public void htmlClean() { + final String html = ResourceUtils.toString("data/example.html"); + final Document document = alchemyApi.htmlGetText(html, "http://www.test.com/"); + System.out.println(document); + } + + /** + * Extract page text from html. Including ads, navigation links,and other content. + */ + @Test + public void htmlRaw() { + final String html = ResourceUtils.toString("data/example.html"); + final Document document = alchemyApi.htmlGetRawText(html, "http://www.test.com/"); + System.out.println(document); + } + + /** + * EExtract a title from html. + */ + @Test + public void htmlTitle() { + final String html = ResourceUtils.toString("data/example.html"); + final Document document = alchemyApi.htmlGetTitle(html, "http://www.test.com/"); + System.out.println(document); + } + +} diff --git a/src/test/java/com/alchemyapi/helpers/ResourceUtils.java b/src/test/java/com/alchemyapi/helpers/ResourceUtils.java index 2aff5ef..d1e0dea 100644 --- a/src/test/java/com/alchemyapi/helpers/ResourceUtils.java +++ b/src/test/java/com/alchemyapi/helpers/ResourceUtils.java @@ -23,4 +23,13 @@ public static InputStream toInputStream(final String resource) { return Thread.currentThread().getContextClassLoader().getResourceAsStream(resource); } + public static byte[] toBytes(final String resource) { + try { + return IOUtils.toByteArray(toInputStream(resource)); + } catch (IOException e) { + e.printStackTrace(); + return null; + } + } + } diff --git a/src/test/resources/to_fix/CategoryTest.java b/src/test/resources/to_fix/CategoryTest.java deleted file mode 100644 index b7b3ff1..0000000 --- a/src/test/resources/to_fix/CategoryTest.java +++ /dev/null @@ -1,92 +0,0 @@ -package to_fix; - -import com.alchemyapi.api.AlchemyApi; -import com.alchemyapi.api.parameters.CategoryParameters; -import com.alchemyapi.api.parameters.Parameters; -import org.w3c.dom.Document; -import org.xml.sax.SAXException; - -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerException; -import javax.xml.transform.TransformerFactory; -import javax.xml.transform.dom.DOMSource; -import javax.xml.transform.stream.StreamResult; -import javax.xml.xpath.XPathExpressionException; -import java.io.BufferedReader; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.IOException; -import java.io.StringWriter; - -class CategoryTest { - public static void main(String[] args) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - // Create an AlchemyAPI object. - 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)); - - // Categorize some text. - doc = alchemyObj.TextGetCategory("Latest on the War in Iraq."); - System.out.println(getStringFromDocument(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)); - - CategoryParameters categoryParameters = new CategoryParameters(); - categoryParameters.setOutputMode(Parameters.OUTPUT_RDF); - doc = alchemyObj.HTMLGetCategory(htmlDoc, "http://www.test.com/", categoryParameters); - System.out.println(getStringFromDocument(doc)); - } - - // utility function - private static String getFileContents(String filename) - throws IOException, FileNotFoundException - { - File file = new File(filename); - StringBuilder contents = new StringBuilder(); - - BufferedReader input = new BufferedReader(new FileReader(file)); - - try { - String line = null; - - while ((line = input.readLine()) != null) { - contents.append(line); - contents.append(System.getProperty("line.separator")); - } - } finally { - input.close(); - } - - return contents.toString(); - } - - // utility method - private static String getStringFromDocument(Document doc) { - try { - DOMSource domSource = new DOMSource(doc); - StringWriter writer = new StringWriter(); - StreamResult result = new StreamResult(writer); - - TransformerFactory tf = TransformerFactory.newInstance(); - Transformer transformer = tf.newTransformer(); - transformer.transform(domSource, result); - - return writer.toString(); - } catch (TransformerException ex) { - ex.printStackTrace(); - return null; - } - } -} diff --git a/src/test/resources/to_fix/CombinedTest.java b/src/test/resources/to_fix/CombinedTest.java deleted file mode 100644 index d2e274e..0000000 --- a/src/test/resources/to_fix/CombinedTest.java +++ /dev/null @@ -1,84 +0,0 @@ -package to_fix; - -import com.alchemyapi.api.*; - -import com.alchemyapi.api.parameters.CombinedParameters; -import org.xml.sax.SAXException; -import org.w3c.dom.Document; -import java.io.*; -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.xpath.XPathExpressionException; -import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerException; -import javax.xml.transform.TransformerFactory; -import javax.xml.transform.dom.DOMSource; -import javax.xml.transform.stream.StreamResult; - -class CombinedTest { - public static void main(String[] args) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - // Create an AlchemyAPI object. - 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)); - - // 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)); - - // Only extract entities & keywords - CombinedParameters combinedParams = new CombinedParameters(); - combinedParams.setSentiment(true); - combinedParams.setExtract("entity"); - combinedParams.setExtract("keyword"); - doc = alchemyObj.TextGetCombined("Madonna enjoys tasty Pepsi. I love her style.", combinedParams); - System.out.println(getStringFromDocument(doc)); - } - - // utility function - private static String getFileContents(String filename) - throws IOException, FileNotFoundException - { - File file = new File(filename); - StringBuilder contents = new StringBuilder(); - - BufferedReader input = new BufferedReader(new FileReader(file)); - - try { - String line = null; - - while ((line = input.readLine()) != null) { - contents.append(line); - contents.append(System.getProperty("line.separator")); - } - } finally { - input.close(); - } - - return contents.toString(); - } - - // utility method - private static String getStringFromDocument(Document doc) { - try { - DOMSource domSource = new DOMSource(doc); - StringWriter writer = new StringWriter(); - StreamResult result = new StreamResult(writer); - - TransformerFactory tf = TransformerFactory.newInstance(); - Transformer transformer = tf.newTransformer(); - transformer.transform(domSource, result); - - return writer.toString(); - } catch (TransformerException ex) { - ex.printStackTrace(); - return null; - } - } -} diff --git a/src/test/resources/to_fix/ConceptTest.java b/src/test/resources/to_fix/ConceptTest.java deleted file mode 100644 index 1ad6a05..0000000 --- a/src/test/resources/to_fix/ConceptTest.java +++ /dev/null @@ -1,79 +0,0 @@ -package to_fix; - -import com.alchemyapi.api.AlchemyApi; - -import org.xml.sax.SAXException; -import org.w3c.dom.Document; -import java.io.*; -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.xpath.XPathExpressionException; -import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerException; -import javax.xml.transform.TransformerFactory; -import javax.xml.transform.dom.DOMSource; -import javax.xml.transform.stream.StreamResult; - -class ConceptTest { - public static void main(String[] args) throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException { - // Create an AlchemyAPI object. - 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)); - - // 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)); - - // 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)); - } - - // utility function - private static String getFileContents(String filename) - throws IOException, FileNotFoundException - { - File file = new File(filename); - StringBuilder contents = new StringBuilder(); - - BufferedReader input = new BufferedReader(new FileReader(file)); - - try { - String line = null; - - while ((line = input.readLine()) != null) { - contents.append(line); - contents.append(System.getProperty("line.separator")); - } - } finally { - input.close(); - } - - return contents.toString(); - } - - // utility method - private static String getStringFromDocument(Document doc) { - try { - DOMSource domSource = new DOMSource(doc); - StringWriter writer = new StringWriter(); - StreamResult result = new StreamResult(writer); - - TransformerFactory tf = TransformerFactory.newInstance(); - Transformer transformer = tf.newTransformer(); - transformer.transform(domSource, result); - - return writer.toString(); - } catch (TransformerException ex) { - ex.printStackTrace(); - return null; - } - } -} diff --git a/src/test/resources/to_fix/ConstraintQueryTest.java b/src/test/resources/to_fix/ConstraintQueryTest.java deleted file mode 100644 index 6d2a9ed..0000000 --- a/src/test/resources/to_fix/ConstraintQueryTest.java +++ /dev/null @@ -1,75 +0,0 @@ -package to_fix; - -import com.alchemyapi.api.AlchemyApi; - -import org.xml.sax.SAXException; -import org.w3c.dom.Document; -import java.io.*; -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.xpath.XPathExpressionException; -import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerException; -import javax.xml.transform.TransformerFactory; -import javax.xml.transform.dom.DOMSource; -import javax.xml.transform.stream.StreamResult; - -class ConstraintQueryTest { - public static void main(String[] args) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - // Create an AlchemyAPI object. - AlchemyApi alchemyObj = AlchemyApi.GetInstanceFromFile("api_key.txt"); - - // Extract first link from an URL. - Document doc = alchemyObj.URLGetConstraintQuery("http://microformats.org/wiki/hcard", - "1st link"); - System.out.println(getStringFromDocument(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)); - } - - // utility function - private static String getFileContents(String filename) - throws IOException, FileNotFoundException - { - File file = new File(filename); - StringBuilder contents = new StringBuilder(); - - BufferedReader input = new BufferedReader(new FileReader(file)); - - try { - String line = null; - - while ((line = input.readLine()) != null) { - contents.append(line); - contents.append(System.getProperty("line.separator")); - } - } finally { - input.close(); - } - - return contents.toString(); - } - - // utility method - private static String getStringFromDocument(Document doc) { - try { - DOMSource domSource = new DOMSource(doc); - StringWriter writer = new StringWriter(); - StreamResult result = new StreamResult(writer); - - TransformerFactory tf = TransformerFactory.newInstance(); - Transformer transformer = tf.newTransformer(); - transformer.transform(domSource, result); - - return writer.toString(); - } catch (TransformerException ex) { - ex.printStackTrace(); - return null; - } - } -} diff --git a/src/test/resources/to_fix/EntityTest.java b/src/test/resources/to_fix/EntityTest.java deleted file mode 100644 index 0ba3975..0000000 --- a/src/test/resources/to_fix/EntityTest.java +++ /dev/null @@ -1,82 +0,0 @@ -package to_fix; - -import com.alchemyapi.api.AlchemyApi; - -import org.xml.sax.SAXException; -import org.w3c.dom.Document; -import java.io.*; -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.xpath.XPathExpressionException; -import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerException; -import javax.xml.transform.TransformerFactory; -import javax.xml.transform.dom.DOMSource; -import javax.xml.transform.stream.StreamResult; - -class EntityTest { - public static void main(String[] args) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - // Create an AlchemyAPI object. - AlchemyApi alchemyObj = AlchemyApi.GetInstanceFromFile("api_key.txt"); - - // Extract a ranked list of named entities for a web URL. - Document doc = alchemyObj.URLGetRankedNamedEntities("http://www.techcrunch.com/"); - System.out.println(getStringFromDocument(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)); - - // 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)); - } - - // utility function - private static String getFileContents(String filename) - throws IOException, FileNotFoundException - { - File file = new File(filename); - StringBuilder contents = new StringBuilder(); - - BufferedReader input = new BufferedReader(new FileReader(file)); - - try { - String line = null; - - while ((line = input.readLine()) != null) { - contents.append(line); - contents.append(System.getProperty("line.separator")); - } - } finally { - input.close(); - } - - return contents.toString(); - } - - // utility method - private static String getStringFromDocument(Document doc) { - try { - DOMSource domSource = new DOMSource(doc); - StringWriter writer = new StringWriter(); - StreamResult result = new StreamResult(writer); - - TransformerFactory tf = TransformerFactory.newInstance(); - Transformer transformer = tf.newTransformer(); - transformer.transform(domSource, result); - - return writer.toString(); - } catch (TransformerException ex) { - ex.printStackTrace(); - return null; - } - } -} diff --git a/src/test/resources/to_fix/FeedLinksTest.java b/src/test/resources/to_fix/FeedLinksTest.java deleted file mode 100644 index 8409b87..0000000 --- a/src/test/resources/to_fix/FeedLinksTest.java +++ /dev/null @@ -1,76 +0,0 @@ -package to_fix; - -import com.alchemyapi.api.AlchemyApi; - -import org.xml.sax.SAXException; -import org.w3c.dom.Document; -import java.io.*; -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.xpath.XPathExpressionException; -import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerException; -import javax.xml.transform.TransformerFactory; -import javax.xml.transform.dom.DOMSource; -import javax.xml.transform.stream.StreamResult; - -class FeedLinksTest { - public static void main(String[] args) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - // Create an AlchemyAPI object. - 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)); - - // 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)); - } - - // utility function - private static String getFileContents(String filename) - throws IOException, FileNotFoundException - { - File file = new File(filename); - StringBuilder contents = new StringBuilder(); - - BufferedReader input = new BufferedReader(new FileReader(file)); - - try { - String line = null; - - while ((line = input.readLine()) != null) { - contents.append(line); - contents.append(System.getProperty("line.separator")); - } - } finally { - input.close(); - } - - return contents.toString(); - } - - // utility method - private static String getStringFromDocument(Document doc) { - try { - DOMSource domSource = new DOMSource(doc); - StringWriter writer = new StringWriter(); - StreamResult result = new StreamResult(writer); - - TransformerFactory tf = TransformerFactory.newInstance(); - Transformer transformer = tf.newTransformer(); - transformer.transform(domSource, result); - - return writer.toString(); - } catch (TransformerException ex) { - ex.printStackTrace(); - return null; - } - } -} diff --git a/src/test/resources/to_fix/ImageTest.java b/src/test/resources/to_fix/ImageTest.java deleted file mode 100644 index d098753..0000000 --- a/src/test/resources/to_fix/ImageTest.java +++ /dev/null @@ -1,78 +0,0 @@ -package to_fix; - -import com.alchemyapi.api.*; - -import com.alchemyapi.api.parameters.ImageParameters; -import org.xml.sax.SAXException; -import org.w3c.dom.Document; -import java.io.*; -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.xpath.XPathExpressionException; -import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerException; -import javax.xml.transform.TransformerFactory; -import javax.xml.transform.dom.DOMSource; -import javax.xml.transform.stream.StreamResult; - -class ImageTest { - public static void main(String[] args) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - // Create an AlchemyAPI object. - 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)); - - doc = alchemyObj.URLGetRankedImageKeywords( - "http://farm4.staticflickr.com/3726/11043305726_fdcb7785ec_m.jpg"); - System.out.println(getStringFromDocument(doc)); - - byte[] imageByteArray = readFile("data/cat.jpg"); - - ImageParameters imageParams = new ImageParameters(); - imageParams.setImage(imageByteArray); - imageParams.setImagePostMode(ImageParameters.RAW); - doc = alchemyObj.ImageGetRankedImageKeywords(imageParams); - System.out.println(getStringFromDocument(doc)); - } - - // utility function - private static byte[] readFile(String file) throws IOException { - // Open file - RandomAccessFile f = new RandomAccessFile(new File(file), "r"); - try { - // Get and check length - long longlength = f.length(); - int length = (int) longlength; - if (length != longlength) - throw new IOException("File size >= 2 GB"); - // Read file and return data - byte[] data = new byte[length]; - f.readFully(data); - return data; - } finally { - f.close(); - } - } - - // utility method - private static String getStringFromDocument(Document doc) { - try { - DOMSource domSource = new DOMSource(doc); - StringWriter writer = new StringWriter(); - StreamResult result = new StreamResult(writer); - - TransformerFactory tf = TransformerFactory.newInstance(); - Transformer transformer = tf.newTransformer(); - transformer.transform(domSource, result); - - return writer.toString(); - } catch (TransformerException ex) { - ex.printStackTrace(); - return null; - } - } -} diff --git a/src/test/resources/to_fix/KeywordTest.java b/src/test/resources/to_fix/KeywordTest.java deleted file mode 100644 index de96e0e..0000000 --- a/src/test/resources/to_fix/KeywordTest.java +++ /dev/null @@ -1,80 +0,0 @@ -package to_fix; - -import com.alchemyapi.api.AlchemyApi; - -import org.xml.sax.SAXException; -import org.w3c.dom.Document; -import java.io.*; -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.xpath.XPathExpressionException; -import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerException; -import javax.xml.transform.TransformerFactory; -import javax.xml.transform.dom.DOMSource; -import javax.xml.transform.stream.StreamResult; - -class KeywordTest { - public static void main(String[] args) throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException { - // Create an AlchemyAPI object. - 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)); - - // 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)); - - // 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)); - } - - // utility function - private static String getFileContents(String filename) - throws IOException, FileNotFoundException - { - File file = new File(filename); - StringBuilder contents = new StringBuilder(); - - BufferedReader input = new BufferedReader(new FileReader(file)); - - try { - String line = null; - - while ((line = input.readLine()) != null) { - contents.append(line); - contents.append(System.getProperty("line.separator")); - } - } finally { - input.close(); - } - - return contents.toString(); - } - - // utility method - private static String getStringFromDocument(Document doc) { - try { - DOMSource domSource = new DOMSource(doc); - StringWriter writer = new StringWriter(); - StreamResult result = new StreamResult(writer); - - TransformerFactory tf = TransformerFactory.newInstance(); - Transformer transformer = tf.newTransformer(); - transformer.transform(domSource, result); - - return writer.toString(); - } catch (TransformerException ex) { - ex.printStackTrace(); - return null; - } - } -} diff --git a/src/test/resources/to_fix/LanguageTest.java b/src/test/resources/to_fix/LanguageTest.java deleted file mode 100644 index c3c4b8c..0000000 --- a/src/test/resources/to_fix/LanguageTest.java +++ /dev/null @@ -1,81 +0,0 @@ -package to_fix; - -import com.alchemyapi.api.AlchemyApi; - -import org.xml.sax.SAXException; -import org.w3c.dom.Document; -import java.io.*; -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.xpath.XPathExpressionException; -import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerException; -import javax.xml.transform.TransformerFactory; -import javax.xml.transform.dom.DOMSource; -import javax.xml.transform.stream.StreamResult; - -class LanguageTest { - public static void main(String[] args) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - // Create an AlchemyAPI object. - 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)); - - // 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)); - - // 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)); - } - - // utility function - private static String getFileContents(String filename) throws IOException, - FileNotFoundException { - File file = new File(filename); - StringBuilder contents = new StringBuilder(); - - BufferedReader input = new BufferedReader(new FileReader(file)); - - try { - String line = null; - - while ((line = input.readLine()) != null) { - contents.append(line); - contents.append(System.getProperty("line.separator")); - } - } finally { - input.close(); - } - - return contents.toString(); - } - - // utility method - private static String getStringFromDocument(Document doc) { - try { - DOMSource domSource = new DOMSource(doc); - StringWriter writer = new StringWriter(); - StreamResult result = new StreamResult(writer); - - TransformerFactory tf = TransformerFactory.newInstance(); - Transformer transformer = tf.newTransformer(); - transformer.transform(domSource, result); - - return writer.toString(); - } catch (TransformerException ex) { - ex.printStackTrace(); - return null; - } - } -} diff --git a/src/test/resources/to_fix/MicroformatsTest.java b/src/test/resources/to_fix/MicroformatsTest.java deleted file mode 100644 index 93666e0..0000000 --- a/src/test/resources/to_fix/MicroformatsTest.java +++ /dev/null @@ -1,81 +0,0 @@ -package to_fix; - -import com.alchemyapi.api.AlchemyApi; -import org.w3c.dom.Document; -import org.xml.sax.SAXException; - -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerException; -import javax.xml.transform.TransformerFactory; -import javax.xml.transform.dom.DOMSource; -import javax.xml.transform.stream.StreamResult; -import javax.xml.xpath.XPathExpressionException; -import java.io.BufferedReader; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.IOException; -import java.io.StringWriter; - -public class MicroformatsTest { - public static void main(String[] args) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - // Create an AlchemyAPI object. - 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)); - - // 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)); - } - - // utility function - private static String getFileContents(String filename) - throws IOException, FileNotFoundException - { - File file = new File(filename); - StringBuilder contents = new StringBuilder(); - - BufferedReader input = new BufferedReader(new FileReader(file)); - - try { - String line = null; - - while ((line = input.readLine()) != null) { - contents.append(line); - contents.append(System.getProperty("line.separator")); - } - } finally { - input.close(); - } - - return contents.toString(); - } - - // utility method - private static String getStringFromDocument(Document doc) { - try { - DOMSource domSource = new DOMSource(doc); - StringWriter writer = new StringWriter(); - StreamResult result = new StreamResult(writer); - - TransformerFactory tf = TransformerFactory.newInstance(); - Transformer transformer = tf.newTransformer(); - transformer.transform(domSource, result); - - return writer.toString(); - } catch (TransformerException ex) { - ex.printStackTrace(); - return null; - } - } -} diff --git a/src/test/resources/to_fix/ParameterTest.java b/src/test/resources/to_fix/ParameterTest.java deleted file mode 100644 index 55f6e33..0000000 --- a/src/test/resources/to_fix/ParameterTest.java +++ /dev/null @@ -1,87 +0,0 @@ -package to_fix; - -import com.alchemyapi.api.*; - -import com.alchemyapi.api.parameters.NamedEntityParameters; -import org.xml.sax.SAXException; -import org.w3c.dom.Document; -import java.io.*; -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.xpath.XPathExpressionException; -import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerException; -import javax.xml.transform.TransformerFactory; -import javax.xml.transform.dom.DOMSource; -import javax.xml.transform.stream.StreamResult; - -class ParameterTest { - public static void main(String[] args) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - // Create an AlchemyAPI object. - AlchemyApi alchemyObj = AlchemyApi.GetInstanceFromFile("api_key.txt"); - - // Extract a ranked list of named entities for a web URL. Turn off disambiguation - NamedEntityParameters entityParams = new NamedEntityParameters(); - entityParams.setDisambiguate(false); - entityParams.setSentiment(true); - Document doc = alchemyObj.URLGetRankedNamedEntities("http://www.techcrunch.com/", entityParams); - System.out.println(getStringFromDocument(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)); - - // 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)); - - } - - // utility function - private static String getFileContents(String filename) - throws IOException, FileNotFoundException - { - File file = new File(filename); - StringBuilder contents = new StringBuilder(); - - BufferedReader input = new BufferedReader(new FileReader(file)); - - try { - String line = null; - - while ((line = input.readLine()) != null) { - contents.append(line); - contents.append(System.getProperty("line.separator")); - } - } finally { - input.close(); - } - - return contents.toString(); - } - - // utility method - private static String getStringFromDocument(Document doc) { - try { - DOMSource domSource = new DOMSource(doc); - StringWriter writer = new StringWriter(); - StreamResult result = new StreamResult(writer); - - TransformerFactory tf = TransformerFactory.newInstance(); - Transformer transformer = tf.newTransformer(); - transformer.transform(domSource, result); - - return writer.toString(); - } catch (TransformerException ex) { - ex.printStackTrace(); - return null; - } - } -} diff --git a/src/test/resources/to_fix/RelationsTest.java b/src/test/resources/to_fix/RelationsTest.java deleted file mode 100644 index 627b23d..0000000 --- a/src/test/resources/to_fix/RelationsTest.java +++ /dev/null @@ -1,97 +0,0 @@ -package to_fix; - -import com.alchemyapi.api.*; - -import com.alchemyapi.api.parameters.RelationParameters; -import org.xml.sax.SAXException; -import org.w3c.dom.Document; -import java.io.*; -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.xpath.XPathExpressionException; -import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerException; -import javax.xml.transform.TransformerFactory; -import javax.xml.transform.dom.DOMSource; -import javax.xml.transform.stream.StreamResult; - -class RelationsTest { - public static void main(String[] args) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - // Create an AlchemyAPI object. - 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)); - - // 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)); - - // 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)); - - RelationParameters relationParams = new RelationParameters(); - relationParams.setSentiment(true); - relationParams.setEntities(true); - relationParams.setDisambiguate(true); - relationParams.setSentimentExcludeEntities(true); - doc = alchemyObj.TextGetRelations("Madonna enjoys tasty Pepsi. I love her style.", relationParams); - System.out.println(getStringFromDocument(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)); - } - - // utility function - private static String getFileContents(String filename) - throws IOException, FileNotFoundException - { - File file = new File(filename); - StringBuilder contents = new StringBuilder(); - - BufferedReader input = new BufferedReader(new FileReader(file)); - - try { - String line = null; - - while ((line = input.readLine()) != null) { - contents.append(line); - contents.append(System.getProperty("line.separator")); - } - } finally { - input.close(); - } - - return contents.toString(); - } - - // utility method - private static String getStringFromDocument(Document doc) { - try { - DOMSource domSource = new DOMSource(doc); - StringWriter writer = new StringWriter(); - StreamResult result = new StreamResult(writer); - - TransformerFactory tf = TransformerFactory.newInstance(); - Transformer transformer = tf.newTransformer(); - transformer.transform(domSource, result); - - return writer.toString(); - } catch (TransformerException ex) { - ex.printStackTrace(); - return null; - } - } -} diff --git a/src/test/resources/to_fix/TaxonomyTest.java b/src/test/resources/to_fix/TaxonomyTest.java deleted file mode 100644 index 6b4d8e7..0000000 --- a/src/test/resources/to_fix/TaxonomyTest.java +++ /dev/null @@ -1,82 +0,0 @@ -package to_fix; - -import com.alchemyapi.api.*; - -import org.xml.sax.SAXException; -import org.w3c.dom.Document; -import java.io.*; -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.xpath.XPathExpressionException; -import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerException; -import javax.xml.transform.TransformerFactory; -import javax.xml.transform.dom.DOMSource; -import javax.xml.transform.stream.StreamResult; - -class TaxonomyTest { - public static void main(String[] args) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - // Create an AlchemyAPI object. - 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)); - - // 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)); - - // 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)); - } - - // utility function - private static String getFileContents(String filename) - throws IOException, FileNotFoundException - { - File file = new File(filename); - StringBuilder contents = new StringBuilder(); - - BufferedReader input = new BufferedReader(new FileReader(file)); - - try { - String line = null; - - while ((line = input.readLine()) != null) { - contents.append(line); - contents.append(System.getProperty("line.separator")); - } - } finally { - input.close(); - } - - return contents.toString(); - } - - // utility method - private static String getStringFromDocument(Document doc) { - try { - DOMSource domSource = new DOMSource(doc); - StringWriter writer = new StringWriter(); - StreamResult result = new StreamResult(writer); - - TransformerFactory tf = TransformerFactory.newInstance(); - Transformer transformer = tf.newTransformer(); - transformer.transform(domSource, result); - - return writer.toString(); - } catch (TransformerException ex) { - ex.printStackTrace(); - return null; - } - } -} diff --git a/src/test/resources/to_fix/TextExtractTest.java b/src/test/resources/to_fix/TextExtractTest.java deleted file mode 100644 index 1fede46..0000000 --- a/src/test/resources/to_fix/TextExtractTest.java +++ /dev/null @@ -1,96 +0,0 @@ -package to_fix; - -import com.alchemyapi.api.AlchemyApi; - -import org.xml.sax.SAXException; -import org.w3c.dom.Document; -import java.io.*; -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.xpath.XPathExpressionException; -import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerException; -import javax.xml.transform.TransformerFactory; -import javax.xml.transform.dom.DOMSource; -import javax.xml.transform.stream.StreamResult; - -class TextExtractTest { - public static void main(String[] args) - throws IOException, SAXException, - ParserConfigurationException, XPathExpressionException - { - // Create an AlchemyAPI object. - AlchemyApi alchemyObj = AlchemyApi.GetInstanceFromFile("api_key.txt"); - - // 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)); - - // 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)); - - // Extract a title from a web URL. - doc = alchemyObj.URLGetTitle("http://www.techcrunch.com/"); - System.out.println(getStringFromDocument(doc)); - - // Load a HTML document to analyze. - String htmlDoc = getFileContents("data/example.html"); - - // 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)); - - // 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)); - - // Extract a title from a HTML document. - doc = alchemyObj.HTMLGetTitle(htmlDoc, "http://www.test.com/"); - System.out.println(getStringFromDocument(doc)); - } - - // utility function - private static String getFileContents(String filename) - throws IOException, FileNotFoundException - { - File file = new File(filename); - StringBuilder contents = new StringBuilder(); - - BufferedReader input = new BufferedReader(new FileReader(file)); - - try { - String line = null; - - while ((line = input.readLine()) != null) { - contents.append(line); - contents.append(System.getProperty("line.separator")); - } - } finally { - input.close(); - } - - return contents.toString(); - } - - // utility method - private static String getStringFromDocument(Document doc) { - try { - DOMSource domSource = new DOMSource(doc); - StringWriter writer = new StringWriter(); - StreamResult result = new StreamResult(writer); - - TransformerFactory tf = TransformerFactory.newInstance(); - Transformer transformer = tf.newTransformer(); - transformer.transform(domSource, result); - - return writer.toString(); - } catch (TransformerException ex) { - ex.printStackTrace(); - return null; - } - } -} From f77c1991e206fd8f6fb7550443591ea72f5bfc70 Mon Sep 17 00:00:00 2001 From: Kenny Cason Date: Sun, 2 Aug 2015 18:39:48 -0500 Subject: [PATCH 10/11] #8 replace System.out.printlns with logger --- src/main/java/com/alchemyapi/api/AlchemyApi.java | 2 +- src/test/java/com/alchemyapi/STestAuthor.java | 7 +++++-- src/test/java/com/alchemyapi/STestCategory.java | 11 +++++++---- src/test/java/com/alchemyapi/STestCombined.java | 9 ++++++--- src/test/java/com/alchemyapi/STestConcept.java | 9 ++++++--- .../java/com/alchemyapi/STestConstraintQuery.java | 7 +++++-- src/test/java/com/alchemyapi/STestEntity.java | 9 ++++++--- src/test/java/com/alchemyapi/STestFeedLinks.java | 7 +++++-- src/test/java/com/alchemyapi/STestImage.java | 9 ++++++--- src/test/java/com/alchemyapi/STestKeyword.java | 9 ++++++--- src/test/java/com/alchemyapi/STestLanguage.java | 13 ++++++++----- .../java/com/alchemyapi/STestMicroformats.java | 7 +++++-- src/test/java/com/alchemyapi/STestRelations.java | 13 ++++++++----- src/test/java/com/alchemyapi/STestSentiment.java | 13 ++++++++----- src/test/java/com/alchemyapi/STestTaxonomy.java | 9 ++++++--- .../java/com/alchemyapi/STestTextExtract.java | 15 +++++++++------ 16 files changed, 97 insertions(+), 52 deletions(-) diff --git a/src/main/java/com/alchemyapi/api/AlchemyApi.java b/src/main/java/com/alchemyapi/api/AlchemyApi.java index 8e13447..77c277f 100644 --- a/src/main/java/com/alchemyapi/api/AlchemyApi.java +++ b/src/main/java/com/alchemyapi/api/AlchemyApi.java @@ -604,7 +604,7 @@ private Document parseXml(final String response, final Parameters parameters) { private Document praseRdf(final String response, final Parameters parameters) { final Document document = Jsoup.parse(response, parameters.getEncoding(), Parser.xmlParser()); - System.out.println("RAW: " + response); + LOGGER.info("RAW: " + response); final Element status = document.select("rdf|RDF > rdf|Description > aapi|ResultStatus").first(); if (status == null || !status.text().equals("OK")) { throw new AlchemyApiException("Error making API call: " + status); diff --git a/src/test/java/com/alchemyapi/STestAuthor.java b/src/test/java/com/alchemyapi/STestAuthor.java index da12350..1077a90 100644 --- a/src/test/java/com/alchemyapi/STestAuthor.java +++ b/src/test/java/com/alchemyapi/STestAuthor.java @@ -3,6 +3,7 @@ import com.alchemyapi.api.AlchemyApi; import com.alchemyapi.helpers.ResourceUtils; import com.alchemyapi.helpers.TestApiFactory; +import org.apache.log4j.Logger; import org.jsoup.nodes.Document; import org.junit.Test; @@ -13,19 +14,21 @@ */ public class STestAuthor { + private static final Logger LOGGER = Logger.getLogger(STestAuthor.class); + private final AlchemyApi alchemyApi = TestApiFactory.build(new File(System.getProperty("user.home"), ".alchemy/api.key")); @Test public void html() { final String html = ResourceUtils.toString("data/example.html"); final Document document = alchemyApi.htmlGetAuthor(html, "http://www.test.com/"); - System.out.println(document); + LOGGER.info(document); } @Test public void url() { final Document document = alchemyApi.urlGetAuthor("http://www.politico.com/blogs/media/2012/02/detroit-news-ed-upset-over-romney-edit-115247.html"); - System.out.println(document); + LOGGER.info(document); } } diff --git a/src/test/java/com/alchemyapi/STestCategory.java b/src/test/java/com/alchemyapi/STestCategory.java index 3fca07b..b4dfbf1 100644 --- a/src/test/java/com/alchemyapi/STestCategory.java +++ b/src/test/java/com/alchemyapi/STestCategory.java @@ -5,6 +5,7 @@ import com.alchemyapi.api.parameters.Parameters; import com.alchemyapi.helpers.ResourceUtils; import com.alchemyapi.helpers.TestApiFactory; +import org.apache.log4j.Logger; import org.jsoup.nodes.Document; import org.junit.Test; @@ -15,25 +16,27 @@ */ public class STestCategory { + private static final Logger LOGGER = Logger.getLogger(STestCategory.class); + private final AlchemyApi alchemyApi = TestApiFactory.build(new File(System.getProperty("user.home"), ".alchemy/api.key")); @Test public void url() { final Document document = alchemyApi.urlGetCategory("http://www.techcrunch.com/"); - System.out.println(document); + LOGGER.info(document); } @Test public void text() { final Document document = alchemyApi.textGetCategory("Latest on the War in Iraq."); - System.out.println(document); + LOGGER.info(document); } @Test public void html() { final String html = ResourceUtils.toString("data/example.html"); final Document document = alchemyApi.htmlGetCategory(html, "http://www.test.com/"); - System.out.println(document); + LOGGER.info(document); } @Test @@ -42,7 +45,7 @@ public void htmlRdfFormat() { final CategoryParameters categoryParameters = new CategoryParameters(); categoryParameters.setOutputMode(Parameters.OUTPUT_RDF); final Document document2 = alchemyApi.htmlGetCategory(html, "http://www.test.com/", categoryParameters); - System.out.println(document2); + LOGGER.info(document2); } } diff --git a/src/test/java/com/alchemyapi/STestCombined.java b/src/test/java/com/alchemyapi/STestCombined.java index 79f1c54..33bc132 100644 --- a/src/test/java/com/alchemyapi/STestCombined.java +++ b/src/test/java/com/alchemyapi/STestCombined.java @@ -3,6 +3,7 @@ import com.alchemyapi.api.AlchemyApi; import com.alchemyapi.api.parameters.CombinedParameters; import com.alchemyapi.helpers.TestApiFactory; +import org.apache.log4j.Logger; import org.jsoup.nodes.Document; import org.junit.Test; @@ -13,19 +14,21 @@ */ public class STestCombined { + private static final Logger LOGGER = Logger.getLogger(STestCombined.class); + private final AlchemyApi alchemyApi = TestApiFactory.build(new File(System.getProperty("user.home"), ".alchemy/api.key")); @Test public void url() { final Document document = alchemyApi.urlGetCombined("http://www.techcrunch.com/"); - System.out.println(document); + LOGGER.info(document); } @Test public void text() { final Document document = alchemyApi.textGetCombined( "Hello there, my name is Bob Jones. I live in the United States of America. Where do you live, Fred?"); - System.out.println(document); + LOGGER.info(document); // Only extract entities & keywords final CombinedParameters combinedParams = new CombinedParameters(); @@ -33,7 +36,7 @@ public void text() { combinedParams.setExtract("entity"); combinedParams.setExtract("keyword"); final Document document2 = alchemyApi.textGetCombined("Madonna enjoys tasty Pepsi. I love her style.", combinedParams); - System.out.println(document2); + LOGGER.info(document2); } } diff --git a/src/test/java/com/alchemyapi/STestConcept.java b/src/test/java/com/alchemyapi/STestConcept.java index a2e3b16..436f9c0 100644 --- a/src/test/java/com/alchemyapi/STestConcept.java +++ b/src/test/java/com/alchemyapi/STestConcept.java @@ -3,6 +3,7 @@ import com.alchemyapi.api.AlchemyApi; import com.alchemyapi.helpers.ResourceUtils; import com.alchemyapi.helpers.TestApiFactory; +import org.apache.log4j.Logger; import org.jsoup.nodes.Document; import org.junit.Test; @@ -13,26 +14,28 @@ */ public class STestConcept { + private static final Logger LOGGER = Logger.getLogger(STestConcept.class); + private final AlchemyApi alchemyApi = TestApiFactory.build(new File(System.getProperty("user.home"), ".alchemy/api.key")); @Test public void url() { final Document document = alchemyApi.urlGetRankedConcepts("http://www.techcrunch.com/"); - System.out.println(document); + LOGGER.info(document); } @Test public void text() { final Document document = alchemyApi.textGetRankedConcepts( "This thing has a steering wheel, tires, and an engine. Do you know what it is?"); - System.out.println(document); + LOGGER.info(document); } @Test public void html() { final String html = ResourceUtils.toString("data/example.html"); final Document document = alchemyApi.htmlGetRankedConcepts(html, "http://www.test.com/"); - System.out.println(document); + LOGGER.info(document); } } diff --git a/src/test/java/com/alchemyapi/STestConstraintQuery.java b/src/test/java/com/alchemyapi/STestConstraintQuery.java index cf2b406..42df3af 100644 --- a/src/test/java/com/alchemyapi/STestConstraintQuery.java +++ b/src/test/java/com/alchemyapi/STestConstraintQuery.java @@ -3,6 +3,7 @@ import com.alchemyapi.api.AlchemyApi; import com.alchemyapi.helpers.ResourceUtils; import com.alchemyapi.helpers.TestApiFactory; +import org.apache.log4j.Logger; import org.jsoup.nodes.Document; import org.junit.Test; @@ -13,19 +14,21 @@ */ public class STestConstraintQuery { + private static final Logger LOGGER = Logger.getLogger(STestConstraintQuery.class); + private final AlchemyApi alchemyApi = TestApiFactory.build(new File(System.getProperty("user.home"), ".alchemy/api.key")); @Test public void url() { final Document document = alchemyApi.urlGetConstraintQuery("http://microformats.org/wiki/hcard", "1st link"); - System.out.println(document); + LOGGER.info(document); } @Test public void html() { final String html = ResourceUtils.toString("data/example.html"); final Document document = alchemyApi.htmlGetConstraintQuery(html, "http://www.test.com/", "1st link"); - System.out.println(document); + LOGGER.info(document); } } diff --git a/src/test/java/com/alchemyapi/STestEntity.java b/src/test/java/com/alchemyapi/STestEntity.java index 749aaa8..b4bf021 100644 --- a/src/test/java/com/alchemyapi/STestEntity.java +++ b/src/test/java/com/alchemyapi/STestEntity.java @@ -3,6 +3,7 @@ import com.alchemyapi.api.AlchemyApi; import com.alchemyapi.helpers.ResourceUtils; import com.alchemyapi.helpers.TestApiFactory; +import org.apache.log4j.Logger; import org.jsoup.nodes.Document; import org.junit.Test; @@ -13,26 +14,28 @@ */ public class STestEntity { + private static final Logger LOGGER = Logger.getLogger(STestEntity.class); + private final AlchemyApi alchemyApi = TestApiFactory.build(new File(System.getProperty("user.home"), ".alchemy/api.key")); @Test public void url() { final Document document = alchemyApi.urlGetRankedNamedEntities("http://www.techcrunch.com/"); - System.out.println(document); + LOGGER.info(document); } @Test public void text() { final Document document = alchemyApi.textGetRankedNamedEntities( "Hello there, my name is Bob Jones. I live in the United States of America. Where do you live, Fred?"); - System.out.println(document); + LOGGER.info(document); } @Test public void html() { final String html = ResourceUtils.toString("data/example.html"); final Document document = alchemyApi.htmlGetRankedNamedEntities(html, "http://www.test.com/"); - System.out.println(document); + LOGGER.info(document); } } diff --git a/src/test/java/com/alchemyapi/STestFeedLinks.java b/src/test/java/com/alchemyapi/STestFeedLinks.java index 27d6e4b..9ce0b25 100644 --- a/src/test/java/com/alchemyapi/STestFeedLinks.java +++ b/src/test/java/com/alchemyapi/STestFeedLinks.java @@ -3,6 +3,7 @@ import com.alchemyapi.api.AlchemyApi; import com.alchemyapi.helpers.ResourceUtils; import com.alchemyapi.helpers.TestApiFactory; +import org.apache.log4j.Logger; import org.jsoup.nodes.Document; import org.junit.Test; @@ -13,19 +14,21 @@ */ public class STestFeedLinks { + private static final Logger LOGGER = Logger.getLogger(STestFeedLinks.class); + private final AlchemyApi alchemyApi = TestApiFactory.build(new File(System.getProperty("user.home"), ".alchemy/api.key")); @Test public void url() { final Document document = alchemyApi.urlGetFeedLinks("http://www.techcrunch.com/"); - System.out.println(document); + LOGGER.info(document); } @Test public void html() { final String html = ResourceUtils.toString("data/example.html"); final Document document = alchemyApi.htmlGetFeedLinks(html, "http://www.test.com/"); - System.out.println(document); + LOGGER.info(document); } } diff --git a/src/test/java/com/alchemyapi/STestImage.java b/src/test/java/com/alchemyapi/STestImage.java index 9ca8b25..c0c842a 100644 --- a/src/test/java/com/alchemyapi/STestImage.java +++ b/src/test/java/com/alchemyapi/STestImage.java @@ -4,6 +4,7 @@ import com.alchemyapi.api.parameters.ImageParameters; import com.alchemyapi.helpers.ResourceUtils; import com.alchemyapi.helpers.TestApiFactory; +import org.apache.log4j.Logger; import org.jsoup.nodes.Document; import org.junit.Test; @@ -14,18 +15,20 @@ */ public class STestImage { + private static final Logger LOGGER = Logger.getLogger(STestImage.class); + private final AlchemyApi alchemyApi = TestApiFactory.build(new File(System.getProperty("user.home"), ".alchemy/api.key")); @Test public void url() { final Document document = alchemyApi.urlGetImage("http://www.techcrunch.com/"); - System.out.println(document); + LOGGER.info(document); } @Test public void imageUrl() { final Document document = alchemyApi.urlGetRankedImageKeywords("http://farm4.staticflickr.com/3726/11043305726_fdcb7785ec_m.jpg"); - System.out.println(document); + LOGGER.info(document); } @Test @@ -35,7 +38,7 @@ public void imageFile() { imageParams.setImage(imageBytes); imageParams.setImagePostMode(ImageParameters.RAW); final Document document = alchemyApi.imageGetRankedImageKeywords(imageParams); - System.out.println(document); + LOGGER.info(document); } } diff --git a/src/test/java/com/alchemyapi/STestKeyword.java b/src/test/java/com/alchemyapi/STestKeyword.java index 49e7cd4..272790b 100644 --- a/src/test/java/com/alchemyapi/STestKeyword.java +++ b/src/test/java/com/alchemyapi/STestKeyword.java @@ -3,6 +3,7 @@ import com.alchemyapi.api.AlchemyApi; import com.alchemyapi.helpers.ResourceUtils; import com.alchemyapi.helpers.TestApiFactory; +import org.apache.log4j.Logger; import org.jsoup.nodes.Document; import org.junit.Test; @@ -13,26 +14,28 @@ */ public class STestKeyword { + private static final Logger LOGGER = Logger.getLogger(STestKeyword.class); + private final AlchemyApi alchemyApi = TestApiFactory.build(new File(System.getProperty("user.home"), ".alchemy/api.key")); @Test public void url() { final Document document = alchemyApi.urlGetRankedKeywords("http://www.techcrunch.com/"); - System.out.println(document); + LOGGER.info(document); } @Test public void text() { final Document document = alchemyApi.textGetRankedKeywords( "Hello there, my name is Bob Jones. I live in the United States of America. Where do you live, Fred?"); - System.out.println(document); + LOGGER.info(document); } @Test public void html() { final String html = ResourceUtils.toString("data/example.html"); final Document document = alchemyApi.htmlGetRankedKeywords(html, "http://www.test.com/"); - System.out.println(document); + LOGGER.info(document); } } diff --git a/src/test/java/com/alchemyapi/STestLanguage.java b/src/test/java/com/alchemyapi/STestLanguage.java index 8dd86fd..ced8e9b 100644 --- a/src/test/java/com/alchemyapi/STestLanguage.java +++ b/src/test/java/com/alchemyapi/STestLanguage.java @@ -3,6 +3,7 @@ import com.alchemyapi.api.AlchemyApi; import com.alchemyapi.helpers.ResourceUtils; import com.alchemyapi.helpers.TestApiFactory; +import org.apache.log4j.Logger; import org.jsoup.nodes.Document; import org.junit.Test; @@ -13,37 +14,39 @@ */ public class STestLanguage { + private static final Logger LOGGER = Logger.getLogger(STestLanguage.class); + private final AlchemyApi alchemyApi = TestApiFactory.build(new File(System.getProperty("user.home"), ".alchemy/api.key")); @Test public void url() { final Document document = alchemyApi.urlGetLanguage("http://news.google.fr/"); - System.out.println(document); + LOGGER.info(document); } @Test public void textEnglish() { final Document document = alchemyApi.textGetLanguage("This is some english language text. What language do you speak?"); - System.out.println(document); + LOGGER.info(document); } @Test public void textJapanese() { final Document document = alchemyApi.textGetLanguage("私の名前はケニー・ケーソンです。プログラミングすることが大好きです。"); - System.out.println(document); + LOGGER.info(document); } @Test public void textChinese() { final Document document = alchemyApi.textGetLanguage("你好,我名字叫肯尼。我很喜欢学外语。"); - System.out.println(document); + LOGGER.info(document); } @Test public void html() { final String html = ResourceUtils.toString("data/example.html"); final Document document = alchemyApi.htmlGetLanguage(html, "http://www.test.com/"); - System.out.println(document); + LOGGER.info(document); } } diff --git a/src/test/java/com/alchemyapi/STestMicroformats.java b/src/test/java/com/alchemyapi/STestMicroformats.java index 06985fe..5e6248b 100644 --- a/src/test/java/com/alchemyapi/STestMicroformats.java +++ b/src/test/java/com/alchemyapi/STestMicroformats.java @@ -3,6 +3,7 @@ import com.alchemyapi.api.AlchemyApi; import com.alchemyapi.helpers.ResourceUtils; import com.alchemyapi.helpers.TestApiFactory; +import org.apache.log4j.Logger; import org.jsoup.nodes.Document; import org.junit.Test; @@ -13,19 +14,21 @@ */ public class STestMicroformats { + private static final Logger LOGGER = Logger.getLogger(STestMicroformats.class); + private final AlchemyApi alchemyApi = TestApiFactory.build(new File(System.getProperty("user.home"), ".alchemy/api.key")); @Test public void url() { final Document document = alchemyApi.urlGetMicroformats("http://microformats.org/wiki/hcard"); - System.out.println(document); + LOGGER.info(document); } @Test public void html() { final String html = ResourceUtils.toString("data/microformats.html"); final Document document = alchemyApi.htmlGetMicroformats(html, "http://www.test.com/"); - System.out.println(document); + LOGGER.info(document); } } diff --git a/src/test/java/com/alchemyapi/STestRelations.java b/src/test/java/com/alchemyapi/STestRelations.java index c610754..0f6b85d 100644 --- a/src/test/java/com/alchemyapi/STestRelations.java +++ b/src/test/java/com/alchemyapi/STestRelations.java @@ -4,6 +4,7 @@ import com.alchemyapi.api.parameters.RelationParameters; import com.alchemyapi.helpers.ResourceUtils; import com.alchemyapi.helpers.TestApiFactory; +import org.apache.log4j.Logger; import org.jsoup.nodes.Document; import org.junit.Test; @@ -14,12 +15,14 @@ */ public class STestRelations { + private static final Logger LOGGER = Logger.getLogger(STestRelations.class); + private final AlchemyApi alchemyApi = TestApiFactory.build(new File(System.getProperty("user.home"), ".alchemy/api.key")); @Test public void url() { final Document document = alchemyApi.urlGetRelations("http://www.techcrunch.com/"); - System.out.println(document); + LOGGER.info(document); } @Test @@ -27,14 +30,14 @@ public void text() { // Extract a ranked list of relations from a text string. final Document document = alchemyApi.textGetRelations( "Hello there, my name is Bob Jones. I live in the United States of America. Where do you live, Fred?"); - System.out.println(document); + LOGGER.info(document); } @Test public void html() { final String html = ResourceUtils.toString("data/example.html"); final Document document = alchemyApi.htmlGetRelations(html, "http://www.test.com/"); - System.out.println(document); + LOGGER.info(document); } @Test @@ -45,13 +48,13 @@ public void parameterizedText() { relationParams.setDisambiguate(true); relationParams.setSentimentExcludeEntities(true); final Document document = alchemyApi.textGetRelations("Madonna enjoys tasty Pepsi. I love her style.", relationParams); - System.out.println(document); + LOGGER.info(document); relationParams.setSentiment(true); relationParams.setRequireEntities(true); relationParams.setSentimentExcludeEntities(true); final Document document2 = alchemyApi.textGetRelations("Madonna enjoys tasty Pepsi. I love her style.", relationParams); - System.out.println(document2); + LOGGER.info(document2); } } diff --git a/src/test/java/com/alchemyapi/STestSentiment.java b/src/test/java/com/alchemyapi/STestSentiment.java index 935b641..d1176f5 100644 --- a/src/test/java/com/alchemyapi/STestSentiment.java +++ b/src/test/java/com/alchemyapi/STestSentiment.java @@ -6,6 +6,7 @@ import com.alchemyapi.api.parameters.TargetedSentimentParameters; import com.alchemyapi.helpers.ResourceUtils; import com.alchemyapi.helpers.TestApiFactory; +import org.apache.log4j.Logger; import org.jsoup.nodes.Document; import org.junit.Test; @@ -16,25 +17,27 @@ */ public class STestSentiment { + private static final Logger LOGGER = Logger.getLogger(STestSentiment.class); + private final AlchemyApi alchemyApi = TestApiFactory.build(new File(System.getProperty("user.home"), ".alchemy/api.key")); @Test public void url() { final Document document = alchemyApi.urlGetTextSentiment("http://www.techcrunch.com/"); - System.out.println(document); + LOGGER.info(document); } @Test public void text() { final Document document = alchemyApi.textGetTextSentiment("That hat is ridiculous, Charles."); - System.out.println(document); + LOGGER.info(document); } @Test public void html() { final String html = ResourceUtils.toString("data/example.html"); final Document document = alchemyApi.htmlGetTextSentiment(html, "http://www.test.com/"); - System.out.println(document); + LOGGER.info(document); } @Test @@ -42,12 +45,12 @@ public void entityTargetedSentimentText() { final NamedEntityParameters entityParams = new NamedEntityParameters(); entityParams.setSentiment(true); final Document document = alchemyApi.textGetRankedNamedEntities("That Mike Tyson is such a sweetheart.", entityParams); - System.out.println(document); + LOGGER.info(document); final KeywordParameters keywordParams = new KeywordParameters(); keywordParams.setSentiment(true); final Document document2 = alchemyApi.textGetRankedKeywords("That Mike Tyson is such a sweetheart.", keywordParams); - System.out.println(document2); + LOGGER.info(document2); final TargetedSentimentParameters sentimentParams = new TargetedSentimentParameters(); sentimentParams.setShowSourceText(true); diff --git a/src/test/java/com/alchemyapi/STestTaxonomy.java b/src/test/java/com/alchemyapi/STestTaxonomy.java index 3391d59..1253a48 100644 --- a/src/test/java/com/alchemyapi/STestTaxonomy.java +++ b/src/test/java/com/alchemyapi/STestTaxonomy.java @@ -3,6 +3,7 @@ import com.alchemyapi.api.AlchemyApi; import com.alchemyapi.helpers.ResourceUtils; import com.alchemyapi.helpers.TestApiFactory; +import org.apache.log4j.Logger; import org.jsoup.nodes.Document; import org.junit.Test; @@ -13,26 +14,28 @@ */ public class STestTaxonomy { + private static final Logger LOGGER = Logger.getLogger(STestTaxonomy.class); + private final AlchemyApi alchemyApi = TestApiFactory.build(new File(System.getProperty("user.home"), ".alchemy/api.key")); @Test public void url() { final Document document = alchemyApi.urlGetTaxonomy("http://www.techcrunch.com/"); - System.out.println(document); + LOGGER.info(document); } @Test public void text() { final Document document = alchemyApi.textGetTaxonomy( "Hello there, my name is Bob Jones. I live in the United States of America. Where do you live, Fred?"); - System.out.println(document); + LOGGER.info(document); } @Test public void html() { final String html = ResourceUtils.toString("data/example.html"); final Document document = alchemyApi.htmlGetTaxonomy(html, "http://www.test.com/"); - System.out.println(document); + LOGGER.info(document); } } diff --git a/src/test/java/com/alchemyapi/STestTextExtract.java b/src/test/java/com/alchemyapi/STestTextExtract.java index fe9eca4..d20085c 100644 --- a/src/test/java/com/alchemyapi/STestTextExtract.java +++ b/src/test/java/com/alchemyapi/STestTextExtract.java @@ -3,6 +3,7 @@ import com.alchemyapi.api.AlchemyApi; import com.alchemyapi.helpers.ResourceUtils; import com.alchemyapi.helpers.TestApiFactory; +import org.apache.log4j.Logger; import org.jsoup.nodes.Document; import org.junit.Test; @@ -13,6 +14,8 @@ */ public class STestTextExtract { + private static final Logger LOGGER = Logger.getLogger(STestTextExtract.class); + private final AlchemyApi alchemyApi = TestApiFactory.build(new File(System.getProperty("user.home"), ".alchemy/api.key")); /** @@ -21,7 +24,7 @@ public class STestTextExtract { @Test public void urlClean() { final Document document = alchemyApi.urlGetText("http://www.techcrunch.com/"); - System.out.println(document); + LOGGER.info(document); } /** @@ -30,7 +33,7 @@ public void urlClean() { @Test public void urlRaw() { final Document document = alchemyApi.urlGetRawText("http://www.techcrunch.com/"); - System.out.println(document); + LOGGER.info(document); } /** @@ -39,7 +42,7 @@ public void urlRaw() { @Test public void urlTitle() { final Document document = alchemyApi.urlGetTitle("http://www.techcrunch.com/"); - System.out.println(document); + LOGGER.info(document); } /** @@ -49,7 +52,7 @@ public void urlTitle() { public void htmlClean() { final String html = ResourceUtils.toString("data/example.html"); final Document document = alchemyApi.htmlGetText(html, "http://www.test.com/"); - System.out.println(document); + LOGGER.info(document); } /** @@ -59,7 +62,7 @@ public void htmlClean() { public void htmlRaw() { final String html = ResourceUtils.toString("data/example.html"); final Document document = alchemyApi.htmlGetRawText(html, "http://www.test.com/"); - System.out.println(document); + LOGGER.info(document); } /** @@ -69,7 +72,7 @@ public void htmlRaw() { public void htmlTitle() { final String html = ResourceUtils.toString("data/example.html"); final Document document = alchemyApi.htmlGetTitle(html, "http://www.test.com/"); - System.out.println(document); + LOGGER.info(document); } } From f489f986aed5c13790868ea85ed199d66065f7e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Heims=C3=A6ter?= Date: Tue, 23 Feb 2016 12:36:59 +0100 Subject: [PATCH 11/11] Added support for Emotions --- .../java/com/alchemyapi/api/AlchemyApi.java | 41 ++++++++---- .../api/parameters/CombinedParameters.java | 2 + .../api/parameters/EmotionParameters.java | 67 +++++++++++++++++++ .../java/com/alchemyapi/STestEmotion.java | 39 +++++++++++ 4 files changed, 136 insertions(+), 13 deletions(-) create mode 100644 src/main/java/com/alchemyapi/api/parameters/EmotionParameters.java create mode 100644 src/test/java/com/alchemyapi/STestEmotion.java diff --git a/src/main/java/com/alchemyapi/api/AlchemyApi.java b/src/main/java/com/alchemyapi/api/AlchemyApi.java index 77c277f..2960076 100644 --- a/src/main/java/com/alchemyapi/api/AlchemyApi.java +++ b/src/main/java/com/alchemyapi/api/AlchemyApi.java @@ -1,19 +1,7 @@ package com.alchemyapi.api; import com.alchemyapi.api.exceptions.AlchemyApiException; -import com.alchemyapi.api.parameters.CategoryParameters; -import com.alchemyapi.api.parameters.CombinedParameters; -import com.alchemyapi.api.parameters.ConceptParameters; -import com.alchemyapi.api.parameters.ConstraintQueryParameters; -import com.alchemyapi.api.parameters.ImageParameters; -import com.alchemyapi.api.parameters.KeywordParameters; -import com.alchemyapi.api.parameters.LanguageParameters; -import com.alchemyapi.api.parameters.NamedEntityParameters; -import com.alchemyapi.api.parameters.Parameters; -import com.alchemyapi.api.parameters.RelationParameters; -import com.alchemyapi.api.parameters.TargetedSentimentParameters; -import com.alchemyapi.api.parameters.TaxonomyParameters; -import com.alchemyapi.api.parameters.TextParameters; +import com.alchemyapi.api.parameters.*; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; import org.apache.log4j.Logger; @@ -72,6 +60,33 @@ public Document htmlGetAuthor(final String html, final String url, final Paramet params.setUrl(url); return post("HTMLGetAuthor", "html", params); } + public Document urlGetEmotion(final String url) { + return urlGetEmotion(url, new EmotionParameters()); + } + + public Document urlGetEmotion(final String url, final EmotionParameters params) { + params.setUrl(url); + return get("URLGetEmotion", "url", params); + } + + public Document htmlGetEmotion(final String html, final String url) { + return htmlGetEmotion(html, url, new EmotionParameters()); + } + + public Document htmlGetEmotion(final String html, final String url, final EmotionParameters params) { + params.setUrl(url); + params.setHtml(html); + return post("HTMLGetEmotion", "html", params); + } + + public Document textGetEmotion(final String text) { + return textGetEmotion(text, new EmotionParameters()); + } + + public Document textGetEmotion(final String text, final EmotionParameters params) { + params.setText(text); + return post("TextGetEmotion", "text", params); + } public Document urlGetRankedNamedEntities(final String url) { return urlGetRankedNamedEntities(url, new NamedEntityParameters()); diff --git a/src/main/java/com/alchemyapi/api/parameters/CombinedParameters.java b/src/main/java/com/alchemyapi/api/parameters/CombinedParameters.java index d63f4e5..1367d38 100644 --- a/src/main/java/com/alchemyapi/api/parameters/CombinedParameters.java +++ b/src/main/java/com/alchemyapi/api/parameters/CombinedParameters.java @@ -13,6 +13,7 @@ public class CombinedParameters extends Parameters { public static final String EXTRACT_MODE_TRUST = "trust-metadata"; public static final String EXTRACT_MODE_INFER = "always-infer"; public static final String EXTRACT_PAGE_IMAGE = "page-image"; + public static final String EXTRACT_EMOTION = "emotion"; public static final String EXTRACT_ENTITY = "entity"; public static final String EXTRACT_KEYWORD = "keyword"; public static final String EXTRACT_TITLE = "title"; @@ -144,6 +145,7 @@ public void setExtractAll() { public void setExtract(String extractArg) { if (!extractArg.equals(EXTRACT_PAGE_IMAGE) + && !extractArg.equals(EXTRACT_EMOTION) && !extractArg.equals(EXTRACT_ENTITY) && !extractArg.equals(EXTRACT_KEYWORD) && !extractArg.equals(EXTRACT_TITLE) diff --git a/src/main/java/com/alchemyapi/api/parameters/EmotionParameters.java b/src/main/java/com/alchemyapi/api/parameters/EmotionParameters.java new file mode 100644 index 0000000..587f2ec --- /dev/null +++ b/src/main/java/com/alchemyapi/api/parameters/EmotionParameters.java @@ -0,0 +1,67 @@ +package com.alchemyapi.api.parameters; + +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; + +public class EmotionParameters extends Parameters { + public static final String CLEANED_OR_RAW = "cleaned_or_raw"; + public static final String CQUERY = "cquery"; + public static final String XPATH = "xpath"; + + private String sourceText; + private Boolean showSourceText; + private String cQuery; + private String xPath; + + public String getSourceText() { + return sourceText; + } + + public void setSourceText(String sourceText) { + if (!sourceText.equals(EmotionParameters.CLEANED_OR_RAW) + && !sourceText.equals(EmotionParameters.CQUERY) + && !sourceText.equals(EmotionParameters.XPATH)) { + throw new RuntimeException("Invalid setting " + sourceText + " for parameter sourceText"); + } + this.sourceText = sourceText; + } + + public boolean isShowSourceText() { + return showSourceText; + } + + public void setShowSourceText(boolean showSourceText) { + this.showSourceText = showSourceText; + } + + public String getCQuery() { + return cQuery; + } + + public void setCQuery(String cQuery) { + this.cQuery = cQuery; + } + + public String getXPath() { + return xPath; + } + + public void setXPath(String xPath) { + this.xPath = xPath; + } + + + public String getUrlQuery() { + String retString = super.getUrlQuery(); + try { + if (sourceText != null) retString += "&sourceText=" + sourceText; + if (showSourceText != null) retString += "&showSourceText=" + (showSourceText ? "1" : "0"); + if (cQuery != null) retString += "&cquery=" + URLEncoder.encode(cQuery, "UTF-8"); + if (xPath != null) retString += "&xpath=" + URLEncoder.encode(xPath, "UTF-8"); + } catch (UnsupportedEncodingException e) { + retString = ""; + } + return retString; + } + +} diff --git a/src/test/java/com/alchemyapi/STestEmotion.java b/src/test/java/com/alchemyapi/STestEmotion.java new file mode 100644 index 0000000..5e75573 --- /dev/null +++ b/src/test/java/com/alchemyapi/STestEmotion.java @@ -0,0 +1,39 @@ +package com.alchemyapi; + +import com.alchemyapi.api.AlchemyApi; +import com.alchemyapi.helpers.ResourceUtils; +import com.alchemyapi.helpers.TestApiFactory; +import org.apache.log4j.Logger; +import org.jsoup.nodes.Document; +import org.junit.Test; + +import java.io.File; + + +public class STestEmotion { + + private static final Logger LOGGER = Logger.getLogger(STestEmotion.class); + + private final AlchemyApi alchemyApi = TestApiFactory.build(new File(System.getProperty("user.home"), ".alchemy/api.key")); + + @Test + public void url() { + final Document document = alchemyApi.urlGetEmotion("http://www.techcrunch.com/"); + LOGGER.info(document); + } + + @Test + public void text() { + final Document document = alchemyApi.textGetEmotion( + "This thing has a steering wheel, tires, and an engine. Do you know what it is?"); + LOGGER.info(document); + } + + @Test + public void html() { + final String html = ResourceUtils.toString("data/example.html"); + final Document document = alchemyApi.htmlGetEmotion(html, "http://www.test.com/"); + LOGGER.info(document); + } + +}