Browse code

make ChatterBot.createSession accept an optional array of Locale, if the bot supports it (ie: Cleverbot), this will be use to control the language preference of the bot

Pierre-David BeĢlanger authored on 21/03/2016 17:03:56
Showing 5 changed files
... ...
@@ -7,7 +7,7 @@
7 7
 
8 8
     <groupId>ca.pjer</groupId>
9 9
     <artifactId>chatter-bot-api</artifactId>
10
-    <version>1.3.3</version>
10
+    <version>1.4.0-SNAPSHOT</version>
11 11
 
12 12
     <name>Chatter Bot API</name>
13 13
     <description>
... ...
@@ -18,7 +18,7 @@
18 18
 
19 19
     <properties>
20 20
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
21
-        <java.version>1.5</java.version>
21
+        <java.version>1.6</java.version>
22 22
     </properties>
23 23
 
24 24
     <licenses>
... ...
@@ -61,8 +61,8 @@
61 61
                 <artifactId>maven-compiler-plugin</artifactId>
62 62
                 <version>3.1</version>
63 63
                 <configuration>
64
-                    <source>1.5</source>
65
-                    <target>1.5</target>
64
+                    <source>1.6</source>
65
+                    <target>1.6</target>
66 66
                 </configuration>
67 67
             </plugin>
68 68
             <plugin>
... ...
@@ -1,5 +1,7 @@
1 1
 package com.google.code.chatterbotapi;
2 2
 
3
+import java.util.Locale;
4
+
3 5
 /*
4 6
     chatter-bot-api
5 7
     Copyright (C) 2011 pierredavidbelanger@gmail.com
... ...
@@ -19,5 +21,5 @@ package com.google.code.chatterbotapi;
19 21
 */
20 22
 public interface ChatterBot {
21 23
     
22
-    ChatterBotSession createSession();
24
+    ChatterBotSession createSession(Locale... locales);
23 25
 }
24 26
\ No newline at end of file
... ...
@@ -1,6 +1,7 @@
1 1
 package com.google.code.chatterbotapi;
2 2
 
3 3
 import java.util.LinkedHashMap;
4
+import java.util.Locale;
4 5
 import java.util.Map;
5 6
 
6 7
 /*
... ...
@@ -31,15 +32,17 @@ class Cleverbot implements ChatterBot {
31 32
         this.endIndex = endIndex;
32 33
     }
33 34
 
34
-    public ChatterBotSession createSession() {
35
-        return new Session();
35
+    @Override
36
+    public ChatterBotSession createSession(Locale... locales) {
37
+        return new Session(locales);
36 38
     }
37
-    
39
+
38 40
     private class Session implements ChatterBotSession {
39 41
         private final Map<String, String> vars;
42
+        private final Map<String, String> headers;
40 43
         private final Map<String, String> cookies;
41 44
 
42
-        public Session() {
45
+        public Session(Locale... locales) {
43 46
             vars = new LinkedHashMap<String, String>();
44 47
             vars.put("start", "y");
45 48
             vars.put("icognoid", "wsf");
... ...
@@ -47,9 +50,12 @@ class Cleverbot implements ChatterBot {
47 50
             vars.put("sub", "Say");
48 51
             vars.put("islearning", "1");
49 52
             vars.put("cleanslate", "false");
53
+            headers = new LinkedHashMap<String, String>();
54
+            if (locales.length > 0)
55
+                headers.put("Accept-Language", Utils.toAcceptLanguageTags(locales));
50 56
             cookies = new LinkedHashMap<String, String>();
51 57
             try {
52
-                Utils.request(baseUrl, cookies, null);
58
+                Utils.request(baseUrl, headers, cookies, null);
53 59
             } catch (Exception e) {
54 60
                 throw new RuntimeException(e);
55 61
             }
... ...
@@ -63,7 +69,7 @@ class Cleverbot implements ChatterBot {
63 69
             String formDataDigest = Utils.md5(formDataToDigest);
64 70
             vars.put("icognocheck", formDataDigest);
65 71
 
66
-            String response = Utils.request(serviceUrl, cookies, vars);
72
+            String response = Utils.request(serviceUrl, headers, cookies, vars);
67 73
             
68 74
             String[] responseValues = response.split("\r");
69 75
             
... ...
@@ -1,6 +1,7 @@
1 1
 package com.google.code.chatterbotapi;
2 2
 
3 3
 import java.util.LinkedHashMap;
4
+import java.util.Locale;
4 5
 import java.util.Map;
5 6
 import java.util.UUID;
6 7
 
... ...
@@ -28,7 +29,8 @@ class Pandorabots implements ChatterBot {
28 29
         this.botid = botid;
29 30
     }
30 31
 
31
-    public ChatterBotSession createSession() {
32
+    @Override
33
+    public ChatterBotSession createSession(Locale... locales) {
32 34
         return new Session();
33 35
     }
34 36
 
... ...
@@ -44,7 +46,7 @@ class Pandorabots implements ChatterBot {
44 46
         public ChatterBotThought think(ChatterBotThought thought) throws Exception {
45 47
             vars.put("input", thought.getText());
46 48
 
47
-            String response = Utils.request("http://www.pandorabots.com/pandora/talk-xml", null, vars);
49
+            String response = Utils.request("http://www.pandorabots.com/pandora/talk-xml", null, null, vars);
48 50
 
49 51
             ChatterBotThought responseThought = new ChatterBotThought();
50 52
 
... ...
@@ -16,6 +16,7 @@ import java.net.URL;
16 16
 import java.net.URLEncoder;
17 17
 import java.security.MessageDigest;
18 18
 import java.util.List;
19
+import java.util.Locale;
19 20
 import java.util.Map;
20 21
 
21 22
 /*
... ...
@@ -57,9 +58,44 @@ class Utils {
57 58
         return String.format("%1$032X", hash);
58 59
     }
59 60
 
60
-    public static String request(String url, Map<String, String> cookies, Map<String, String> parameters) throws Exception {
61
+    public static String toAcceptLanguageTags(Locale... locales) {
62
+        // https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4
63
+        //
64
+        // for example, if user ask for: Locale.CANADA_FRENCH, Locale.ENGLISH
65
+        // then this method will render: fr-CA;q=1.0, fr;q=0.99, en;q=0.5
66
+        //
67
+        if (locales.length == 0)
68
+            return "";
69
+        float qf = 1f / (float) locales.length;
70
+        float q = 1f;
71
+        StringBuilder s = new StringBuilder();
72
+        for (int i = 0; i < locales.length; i++) {
73
+            Locale locale = locales[i];
74
+            if (s.length() > 0)
75
+                s.append(", ");
76
+            if (!locale.getCountry().equals("")) {
77
+                s.append(locale.getLanguage()).append("-").append(locale.getCountry());
78
+                s.append(";q=" + q);
79
+                s.append(", ");
80
+                s.append(locale.getLanguage());
81
+                s.append(";q=" + (q - 0.01));
82
+            } else {
83
+                s.append(locale.getLanguage());
84
+                s.append(";q=" + q);
85
+            }
86
+            q -= qf;
87
+        }
88
+        return s.toString();
89
+    }
90
+
91
+    public static String request(String url, Map<String, String> headers, Map<String, String> cookies, Map<String, String> parameters) throws Exception {
61 92
         HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
62 93
         connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36");
94
+        if (headers != null) {
95
+            for (Map.Entry<String, String> header : headers.entrySet()) {
96
+                connection.setRequestProperty(header.getKey(), header.getValue());
97
+            }
98
+        }
63 99
         if (cookies != null && !cookies.isEmpty()) {
64 100
             StringBuilder cookieHeader = new StringBuilder();
65 101
             for (String cookie : cookies.values()) {