Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added support for spelling correction API
- Loading branch information
1 parent
5a6bc25
commit bce76e0
Showing
11 changed files
with
100 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |