Last active
August 29, 2015 13:57
-
-
Save namsor/9676522 to your computer and use it in GitHub Desktop.
API call to predict gender from a personal name (Gendre API sample)
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
import java.net.URLEncoder; | |
import org.apache.http.HttpResponse; | |
import org.apache.http.client.methods.HttpGet; | |
import org.apache.http.impl.client.DefaultHttpClient; | |
/** | |
* Predict Gender from a Personal name | |
* @param firstName The given name | |
* @param lastName The family name | |
* @return Double in range -1 (male) .. +1 (female) | |
*/ | |
public Double genderize(String firstName, String lastName) { | |
try { | |
DefaultHttpClient httpclient = new DefaultHttpClient(); | |
String url = "http://api.onomatic.com/onomastics/api/gendre/" | |
+ URLEncoder.encode(firstName, "UTF-8") + "/" | |
+ URLEncoder.encode(lastName, "UTF-8"); | |
HttpGet httpget = new HttpGet(url); | |
HttpResponse response = httpclient.execute(httpget); | |
BufferedReader in = new BufferedReader(new InputStreamReader( | |
response.getEntity().getContent())); | |
String line = in.readLine(); | |
if (line != null && !line.trim().equals("")) { | |
result = Double.parseDouble(line); | |
} | |
in.close(); | |
return result; | |
} catch (Exception e) { | |
e.printStackTrace(); | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment