Skip to content

Instantly share code, notes, and snippets.

@namsor
Last active August 29, 2015 13:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save namsor/9676522 to your computer and use it in GitHub Desktop.
Save namsor/9676522 to your computer and use it in GitHub Desktop.
API call to predict gender from a personal name (Gendre API sample)
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