PageRenderTime 53ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/src/getDDG.java

https://github.com/lipka/Iris-Voice-Automation
Java | 138 lines | 71 code | 23 blank | 44 comment | 11 complexity | b8db97a5f7ebd6fb44ff0a4d37e5d5f9 MD5 | raw file
  1. package com.samir_ahmed.Iris;
  2. import java.net.URLEncoder;
  3. import java.util.concurrent.Callable;
  4. import org.apache.commons.lang.StringEscapeUtils;
  5. import com.google.gson.JsonObject;
  6. import com.google.gson.JsonParser;
  7. /**
  8. * getDDG : Callable class for querying duckduckgo.com
  9. *
  10. * Time: Ranges from 100ms to 400ms
  11. *
  12. * Description: This is a callable class. Any external references are static to ensure safe concurrency
  13. * getDDG is intended to be run as a threaded task and has only one public facing function: String call()
  14. *
  15. * Only use this class with an executorService.
  16. */
  17. public class getDDG implements Callable<String> {
  18. /**
  19. public static void main(String[] args) {
  20. // TODO Auto-generated method stub
  21. //Toolkit.getDefaultToolkit().beep();
  22. getDDG ddg = new getDDG("Port Moresby");
  23. System.out.println(ddg.call());
  24. }
  25. */
  26. /**
  27. * ALL PRIVATE DATA MEMBERS
  28. **/
  29. private String ddgJSON;
  30. private String xQuery;
  31. private String ddgURL;
  32. private String Answer;
  33. private String Query;
  34. /**Constructor : Takes in a query string **/
  35. public getDDG(String Query){
  36. // This Constructor does not check if the query is not null of empty
  37. // Ensure this check is done before hand
  38. this.Query = Query;
  39. }
  40. /** extraction: Uses GSON to extract Abstract Text **/
  41. private void extraction(){
  42. // Create a new jSON object and get the type
  43. JsonObject ddgJO = new JsonParser().parse(ddgJSON).getAsJsonObject();
  44. String ddgType = ddgJO.get("Type").toString();
  45. // Case 1: It is ambiguous
  46. if(ddgType.contains("\"D\"")){
  47. Answer = whoWhat.isAmbiguous;
  48. }
  49. // Case 2: It is not ambiguous
  50. else if(ddgType.contains("\"A\"")){
  51. // Extract answer
  52. Answer = ddgJO.get("AbstractText").toString();
  53. // If we have a definition in the answer, discard the answer
  54. if(Answer=="" || Answer.contains("definition:")){
  55. Answer = whoWhat.noAnswer;
  56. }
  57. // If the source is not wikipedia, we discard the answer
  58. else if(!ddgJO.get("AbstractSource").toString().equalsIgnoreCase("\"Wikipedia\"")){
  59. Answer = whoWhat.noAnswer;
  60. }
  61. // Straight forward escaping of any html residue
  62. else{
  63. Answer = StringEscapeUtils.unescapeHtml(Answer); //Remove html escape characters
  64. Answer = Answer.replaceAll("\\<.*?>",""); //Remove any html tags remaining
  65. Answer = Answer.replaceAll("\"",""); //Remove enclosing "" marks
  66. Answer = Answer.replaceAll("\\(.*?\\)",""); //Remove anything in parenthesis
  67. }
  68. // Any other case we assume that we have no answer
  69. }
  70. else{
  71. Answer = whoWhat.noAnswer;
  72. }
  73. }
  74. /** duckduckget : Method for downloading ddg JSON **/
  75. private String duckduckGet() {
  76. // Uncomment for timing
  77. //long start = System.currentTimeMillis();
  78. try {
  79. // Safely encode the query as a URL
  80. xQuery = URLEncoder.encode(Query,"UTF-8");
  81. ddgURL = "http://api.duckduckgo.com/?q="+xQuery+"&format=json";
  82. // Use Blocking IO with httpGET class
  83. try{
  84. ddgJSON = httpGET.download(ddgURL,this);
  85. }
  86. catch(InterruptedException InterE){
  87. return whoWhat.cancelled;
  88. }
  89. // If we have null or empty string, httpGET failed
  90. if (ddgJSON== null || ddgJSON.equals("")){
  91. Answer = whoWhat.failed;
  92. }
  93. // If we dont have any type; Download has failed
  94. else if (ddgJSON.contains("\"Type\":\"\"")){
  95. Answer = whoWhat.failed;
  96. }
  97. // Assume true and pass to extraction method
  98. else{
  99. extraction();
  100. }
  101. //Uncomment for timing
  102. //System.out.println("Duck-Duck-Go Response Time: " + (System.currentTimeMillis()-start));
  103. }
  104. catch (Exception Ee){Ee.printStackTrace(); return whoWhat.failed ; }
  105. return Answer;
  106. }
  107. /** call : Method implementing Callable interface, returns a future <string> **/
  108. public String call(){
  109. try{
  110. duckduckGet();
  111. return Answer;
  112. }
  113. catch (Exception ee){ee.printStackTrace(); return whoWhat.failed;}
  114. }
  115. }