Demografix (Demografix v0.1.0)

Copy Markdown View Source

Official Elixir client for the Demografix APIs: genderize, agify, and nationalize.

One client covers all three services. Each call returns the prediction fields plus a Demografix.Quota read from the response rate-limit headers. Batch calls return a Demografix.Batch of per-name predictions in input order plus one quota for the whole response.

Construction

client = Demografix.new("YOUR_API_KEY")
client = Demografix.new("YOUR_API_KEY", timeout: 5_000)

An api_key is required. A nil or blank key raises ArgumentError.

Results and errors

Each function returns {:ok, result} or {:error, %Demografix.Error{}}. The bang variants (genderize!/3 and so on) return the result or raise the error.

{:ok, result} = Demografix.genderize(client, "peter")
result.gender          # => "male"
result.quota.remaining # => 24987

Aggregate over a list

The services are built for summarizing a group of names, not labeling one person.

{:ok, batch} = Demografix.genderize_batch(client, ["peter", "lois", "kim"])

batch.results
|> Enum.frequencies_by(& &1.gender)
# => %{"male" => 1, "female" => 2}

Summary

Functions

Predict the age of one name. Accepts the same :country_id option as genderize/3.

Like agify/3, but returns the result or raises Demografix.Error.

Predict the age of up to 10 names. More than 10 names raises a client-side validation error before any request.

Predict the gender of one name.

Like genderize/3, but returns the result or raises Demografix.Error.

Predict the gender of up to 10 names.

Predict the nationality of one name. Nationalize takes no country_id.

Predict the nationality of up to 10 names. More than 10 names raises a client-side validation error before any request.

Build a client. api_key is required; a nil or blank key raises ArgumentError before any request. The same key works across all three services.

Types

t()

@type t() :: Demografix.Client.t()

Functions

agify(client, name, opts \\ [])

@spec agify(t(), String.t(), keyword()) ::
  {:ok, Demografix.Agify.t()} | {:error, Demografix.Error.t()}

Predict the age of one name. Accepts the same :country_id option as genderize/3.

agify!(client, name, opts \\ [])

@spec agify!(t(), String.t(), keyword()) :: Demografix.Agify.t()

Like agify/3, but returns the result or raises Demografix.Error.

agify_batch(client, names, opts \\ [])

@spec agify_batch(t(), [String.t()], keyword()) ::
  {:ok, Demografix.Batch.t()} | {:error, Demografix.Error.t()}

Predict the age of up to 10 names. More than 10 names raises a client-side validation error before any request.

agify_batch!(client, names, opts \\ [])

@spec agify_batch!(t(), [String.t()], keyword()) :: Demografix.Batch.t()

Like agify_batch/3, but returns the result or raises Demografix.Error.

genderize(client, name, opts \\ [])

@spec genderize(t(), String.t(), keyword()) ::
  {:ok, Demografix.Genderize.t()} | {:error, Demografix.Error.t()}

Predict the gender of one name.

Options

  • :country_id — ISO 3166-1 alpha-2 code to scope the prediction. Echoed back uppercase on the result.

genderize!(client, name, opts \\ [])

@spec genderize!(t(), String.t(), keyword()) :: Demografix.Genderize.t()

Like genderize/3, but returns the result or raises Demografix.Error.

genderize_batch(client, names, opts \\ [])

@spec genderize_batch(t(), [String.t()], keyword()) ::
  {:ok, Demografix.Batch.t()} | {:error, Demografix.Error.t()}

Predict the gender of up to 10 names.

More than 10 names raises a client-side Demografix.Error of kind :validation before any request. Accepts the same :country_id option as genderize/3.

genderize_batch!(client, names, opts \\ [])

@spec genderize_batch!(t(), [String.t()], keyword()) :: Demografix.Batch.t()

Like genderize_batch/3, but returns the result or raises Demografix.Error.

nationalize(client, name, opts \\ [])

@spec nationalize(t(), String.t(), keyword()) ::
  {:ok, Demografix.Nationalize.t()} | {:error, Demografix.Error.t()}

Predict the nationality of one name. Nationalize takes no country_id.

nationalize!(client, name, opts \\ [])

@spec nationalize!(t(), String.t(), keyword()) :: Demografix.Nationalize.t()

Like nationalize/3, but returns the result or raises Demografix.Error.

nationalize_batch(client, names, opts \\ [])

@spec nationalize_batch(t(), [String.t()], keyword()) ::
  {:ok, Demografix.Batch.t()} | {:error, Demografix.Error.t()}

Predict the nationality of up to 10 names. More than 10 names raises a client-side validation error before any request.

nationalize_batch!(client, names, opts \\ [])

@spec nationalize_batch!(t(), [String.t()], keyword()) :: Demografix.Batch.t()

Like nationalize_batch/3, but returns the result or raises Demografix.Error.

new(api_key, opts \\ [])

@spec new(
  String.t(),
  keyword()
) :: t()

Build a client. api_key is required; a nil or blank key raises ArgumentError before any request. The same key works across all three services.

Options

  • :timeout — receive timeout in milliseconds. Defaults to 10_000.