Skip to content

Commit

Permalink
added support for spelling correction API
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-watson committed May 24, 2013
1 parent 5a6bc25 commit bce76e0
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 24 deletions.
6 changes: 6 additions & 0 deletions Makefile
Expand Up @@ -3,3 +3,9 @@ jar:
(cd out/production/bing_search_java; jar xvf ../../../lib/microsoft-translator-java-api-0.6.1-jar-with-dependencies.jar)
(cd out/production/bing_search_java; jar cvf ../../../bing.jar *)

local_maven_install:
lein localrepo install bing.jar local/bing 0.1.0

local_maven_list:
lein localrepo list | grep bing

16 changes: 12 additions & 4 deletions README.md
@@ -1,10 +1,11 @@
A wrapper for the new (2012) Microsoft Bing Web Search and Translate APIs
=========================================================================
A wrapper for the new (2012) Microsoft Bing Web Search, Translate and Spelling Correction APIs
==============================================================================================


You will need an auth token from Microsoft for search. Currently, 100 searches/day are free.
--------------------------------------------------------------------------------------------

I set the following in my environment:
I set the following in my environment for search and spelling correction:

BING_API_KEY=AQi3....kHAyAQ

Expand All @@ -16,7 +17,7 @@ BING_API_APP=e12 ..... 413

You can get an App API key at https://datamarket.azure.com/developer/applications/

The free tranlation tier is currently 2 megabytes of translated text per month.
The free translation tier is currently 2 megabytes of translated text per month.

I also use this wrapper library in Clojure:
-------------------------------------------
Expand Down Expand Up @@ -61,4 +62,11 @@ For lein1 based projects, if I am not making frequent changes to Java code that
and copy my Java code into PROJECT_DIR/src/java/


## notes for local maven install:

➜ bing_search_java git:(master) ✗ lein localrepo install bing.jar local/bing 0.1.0
➜ bing_search_java git:(master) ✗ lein localrepo list | grep bing
local/bing (0.1.0)



Binary file modified bing.jar
Binary file not shown.
Binary file added lib/commons-cli-1.2.jar
Binary file not shown.
Expand Up @@ -10,7 +10,9 @@
/**
* author: Mark Watson
*/
public class Bing {
public class BingSearch {
//https://api.datamarket.azure.com/Bing/Search/Web?Query=%27Xbox%27&Options=%27DisableLocationDetection%2BEnableHighlighting%27

public static String search(String query) throws Exception {
String bingUrl = "https://api.datamarket.azure.com/Bing/Search/Web?Query=%27" + java.net.URLEncoder.encode(query) + "%27&$format=JSON";

Expand All @@ -27,8 +29,7 @@ public static String search(String query) throws Exception {
String inputLine;
StringBuffer sb = new StringBuffer();
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
sb.append(inputLine);
sb.append(inputLine);
in.close();
return sb.toString();
}
Expand Down
42 changes: 42 additions & 0 deletions src/com/markwatson/search/BingSpelling.java
@@ -0,0 +1,42 @@
package com.markwatson.search;

import org.apache.commons.codec.binary.Base64;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

/**
* author: Mark Watson
*/

public class BingSpelling {
public static String spelling(String query) throws Exception {
String bingUrl = "https://api.datamarket.azure.com/Bing/Search/Composite?Sources=%27Web%2BSpell%27&Query=%27" + java.net.URLEncoder.encode(query) + "%27&$format=JSON";

String accountKey = System.getenv("BING_API_KEY");
byte[] accountKeyBytes = Base64.encodeBase64((accountKey + ":" + accountKey).getBytes()); // code for encoding found on stackoverflow
String accountKeyEnc = new String(accountKeyBytes);

URL url = new URL(bingUrl);
URLConnection urlConnection = url.openConnection();
String s1 = "Basic " + accountKeyEnc;
urlConnection.setRequestProperty("Authorization", s1);
BufferedReader in = new BufferedReader(new InputStreamReader(
urlConnection.getInputStream()));
String inputLine;
StringBuffer sb = new StringBuffer();
while ((inputLine = in.readLine()) != null)
sb.append(inputLine);
in.close();

String r = sb.toString();
int index = r.lastIndexOf("\"SpellResult\"");
if (index == -1) return "";
int index2 = r.indexOf("\"Value\"", index);
if (index2 == -1) return "";
int index3 = index2 + 9;
int index4 = r.indexOf("\"", index3);
return r.substring(index3, index4);
}}
Expand Up @@ -7,7 +7,7 @@
/**
* author: Mark Watson
*/
public class Translate {
public class BingTranslate {
public static String search(String text, String from_language, String to_language) throws Exception {
// Set your Windows Azure Marketplace client info - See http://msdn.microsoft.com/en-us/library/hh454950.aspx
// register application at: https://datamarket.azure.com/developer/applications/
Expand All @@ -27,6 +27,11 @@ public static String search(String text, String from_language, String to_languag
if (to_language.equals("japanese")) to = Language.JAPANESE;
if (to_language.equals("hungarian")) to = Language.HUNGARIAN;
if (to_language.equals("spanish")) to = Language.SPANISH;
if (to_language.equals("thai")) to = Language.THAI;
if (to_language.equals("greek")) to = Language.GREEK;
if (to_language.equals("vietnamese")) to = Language.VIETNAMESE;
if (to_language.equals("chinese_traditional")) to = Language.CHINESE_TRADITIONAL;
if (to_language.equals("portuguese")) to = Language.PORTUGUESE;
String translatedText = com.memetix.mst.translate.Translate.execute(text, from, to);

System.out.println(translatedText);
Expand Down
12 changes: 0 additions & 12 deletions test/Test.java

This file was deleted.

12 changes: 12 additions & 0 deletions test/TestSearch.java
@@ -0,0 +1,12 @@
import com.markwatson.search.BingSearch;

/**
* author: Mark Watson
*/
public class TestSearch {
static public void main(String[] args) throws Exception {
String query = "the parot talks a lot";
String json = BingSearch.search(query);
System.out.println(json);
}
}
12 changes: 12 additions & 0 deletions test/TestSpelling.java
@@ -0,0 +1,12 @@
import com.markwatson.search.BingSpelling;

/**
* author: Mark Watson
*/
public class TestSpelling {
static public void main(String[] args) throws Exception {
String query = "the parot talks a lot";
String json = BingSpelling.spelling(query);
System.out.println(json);
}
}
10 changes: 6 additions & 4 deletions test/TestTranslate.java
@@ -1,18 +1,20 @@
import com.markwatson.search.Translate;
import com.markwatson.search.BingTranslate;

/**
* author: Mark Watson
*/
public class TestTranslate {
static public void main(String[] args) throws Exception {
String query = "parrot";
String json = Translate.search(query, "english", "spanish");
String json = BingTranslate.search(query, "english", "spanish");
System.out.println(json);
query = "parrot";
json = Translate.search(query, "english", "chinese");
json = BingTranslate.search(query, "english", "chinese");
System.out.println(json);
query = "good morning";
json = Translate.search(query, "english", "hungarian");
json = BingTranslate.search(query, "english", "hungarian");
System.out.println(json);
json = BingTranslate.search("cat", "english", "portuguese");
System.out.println(json);
}
}

0 comments on commit bce76e0

Please sign in to comment.