<commit_before>defmodule Shippex.ISO do
  @moduledoc """
  This module contains data and functions for obtaining geographic data in
  compliance with the ISO-3166-2 standard.
  """

  @iso Shippex.json_library().decode!(File.read!(:code.priv_dir(:shippex) ++ '/iso-3166-2.json'))

  @doc """
  Returns all ISO-3166-2 data.
  """
  def data() do
    @iso
  end

  @doc """
  Returns a map of country codes and their full names.

      iex> countries = ISO.countries()
      ...> match? %{"US" => "United States"}, countries
      true
  """
  @spec countries() :: %{String.t() => String.t()}
  def countries() do
    Enum.reduce(@iso, %{}, fn {code, %{"name" => name}}, acc ->
      Map.put(acc, code, name)
    end)
  end

  @doc """
  Returns a map of state codes and full names for the given 2-letter country
  code.

      iex> states = ISO.states("US")
      ...> match? %{"TX" => "Texas", "PR" => "Puerto Rico"}, states
      true
      iex> states = ISO.states("MX")
      ...> match? %{"AGU" => "Aguascalientes"}, states
      true
      iex> ISO.states("Not a country.")
      %{}
  """
  @spec states(String.t()) :: %{String.t() => String.t()}
  def states(country \\ "US") do
    states = @iso[country] || %{}

    case states["divisions"] do
      nil ->
        %{}

      divisions ->
        divisions
        |> Enum.map(fn {code, name} ->
          code = String.replace_prefix(code, "#{country}-", "")
          {code, name}
        end)
        |> Map.new()
    end
  end

  @doc """
  Converts a country's 2-letter code to its full name.

      iex> ISO.country_code_to_name("US")
      "United States"
      iex> ISO.country_code_to_name("TN")
      "Tunisia"
      iex> ISO.country_code_to_name("TX")
      nil
  """
  @spec country_code_to_name(String.t()) :: nil | String.t()
  def country_code_to_name(abbr) when is_bitstring(abbr) do
    countries()[abbr]
  end
end
<commit_msg>Updated doctest to use official US name<commit_after>defmodule Shippex.ISO do
  @moduledoc """
  This module contains data and functions for obtaining geographic data in
  compliance with the ISO-3166-2 standard.
  """

  @iso Shippex.json_library().decode!(File.read!(:code.priv_dir(:shippex) ++ '/iso-3166-2.json'))

  @doc """
  Returns all ISO-3166-2 data.
  """
  def data() do
    @iso
  end

  @doc """
  Returns a map of country codes and their full names.

      iex> countries = ISO.countries()
      ...> match? %{"US" => "United States of America (the)"}, countries
      true
  """
  @spec countries() :: %{String.t() => String.t()}
  def countries() do
    Enum.reduce(@iso, %{}, fn {code, %{"name" => name}}, acc ->
      Map.put(acc, code, name)
    end)
  end

  @doc """
  Returns a map of state codes and full names for the given 2-letter country
  code.

      iex> states = ISO.states("US")
      ...> match? %{"TX" => "Texas", "PR" => "Puerto Rico"}, states
      true
      iex> states = ISO.states("MX")
      ...> match? %{"AGU" => "Aguascalientes"}, states
      true
      iex> ISO.states("Not a country.")
      %{}
  """
  @spec states(String.t()) :: %{String.t() => String.t()}
  def states(country \\ "US") do
    states = @iso[country] || %{}

    case states["divisions"] do
      nil ->
        %{}

      divisions ->
        divisions
        |> Enum.map(fn {code, name} ->
          code = String.replace_prefix(code, "#{country}-", "")
          {code, name}
        end)
        |> Map.new()
    end
  end

  @doc """
  Converts a country's 2-letter code to its full name.

      iex> ISO.country_code_to_name("US")
      "United States of America (the)"
      iex> ISO.country_code_to_name("TN")
      "Tunisia"
      iex> ISO.country_code_to_name("TX")
      nil
  """
  @spec country_code_to_name(String.t()) :: nil | String.t()
  def country_code_to_name(abbr) when is_bitstring(abbr) do
    countries()[abbr]
  end
end

{'fraction_non_alphanumeric': 0.16864976720124158, 'fraction_numerical': 0.008794619762027936, 'mean_word_length': 2.5058930190389845, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4695093', 'n_tokens_mistral': 1358, 'n_tokens_neox': 1300, 'n_words': 352}