Skip to main content

Resolve ISO country codes from profile locations and biographies, fast and offline.

Project description

country-resolver

Fast, offline country resolution for social media profiles.

country-resolver is a Python package for inferring a user's country from social media profile information. It is designed for noisy, real-world profile data from platforms such as X (formerly Twitter), GitHub, Mastodon, Bluesky, and similar services.

The package consists of two complementary modules:

Module Purpose
Country Resolver Resolves the dedicated location field into an ISO 3166-1 alpha-2 country code.
Bio Resolver Infers a user's current country of residence from profile biographies using natural language processing.

Together they provide a fast, explainable solution for country inference without relying on online geocoding services for most lookups.


Why Two Resolvers?

Profile location fields and biographies require different approaches.

A location field is usually short and structured:

Lagos, Nigeria
๐Ÿ‡ณ๐Ÿ‡ฌ
Naija
Berlin

A biography is free-form natural language:

Originally from Nigeria.
Based in Berlin.

AI Engineer โ€ข Living in Canada ๐Ÿ‡จ๐Ÿ‡ฆ

Building software.
Born in Ghana.

Trying to process both using the same algorithm either misses valid signals or produces unnecessary false positives.

country-resolver therefore provides two specialized resolvers that can be used independently or together.


Features

  • โšก Offline-first resolution
  • ๐ŸŒ ISO 3166-1 alpha-2 country codes
  • ๐Ÿ™๏ธ City-to-country lookup
  • ๐Ÿšฉ Flag emoji support
  • ๐Ÿ”ค Country aliases and abbreviations
  • โœ๏ธ Fuzzy matching for misspellings
  • ๐Ÿง  spaCy-powered biography analysis
  • ๐Ÿ“Š Confidence scoring and evidence tracking
  • ๐Ÿ” Explainable predictions
  • ๐Ÿงช Comprehensive unit tests
  • ๐ŸŒ Optional online geocoder fallback for address-like locations

Installation

pip install country-resolver

For Bio Resolver, also install the English spaCy model:

python -m spacy download en_core_web_sm

Quick Start

Country Resolver

from country_resolver.location import CountryResolver

resolver = CountryResolver()

resolver.resolve("Lagos, Nigeria")
'NG'

Bio Resolver

from country_resolver.bio import BioResolver

bio = BioResolver(location)

bio.resolve(
    "Originally from Nigeria. Based in Berlin."
)
'DE'

Which Resolver Should I Use?

Your data Recommended module
Profile location field Country Resolver
Profile biography Bio Resolver
Both Use both together for the highest accuracy

Example Inputs

Input Module Output
Nigeria Location NG
๐Ÿ‡ณ๐Ÿ‡ฌ Location NG
Naija Location NG
Lagos Location NG
Caneda Location CA
Kora Nort Location KP
Earth Location None
Based in Berlin. Bio DE
Living in Canada ๐Ÿ‡จ๐Ÿ‡ฆ Bio CA
Originally from Nigeria. Living in Germany. Bio DE

Package Architecture

country_resolver/
โ”‚
โ”œโ”€โ”€ location/
โ”‚   โ”œโ”€โ”€ resolver.py
โ”‚   โ”œโ”€โ”€ lookup.py
โ”‚   โ”œโ”€โ”€ normalize.py
โ”‚   โ”œโ”€โ”€ geocoder.py
โ”‚   โ””โ”€โ”€ ...
โ”‚
โ””โ”€โ”€ bio/
    โ”œโ”€โ”€ resolver.py
    โ”œโ”€โ”€ parser.py
    โ”œโ”€โ”€ scoring.py
    โ”œโ”€โ”€ extractors.py
    โ””โ”€โ”€ ...


Design Philosophy

Offline First

The package performs all primary lookups locally.

Network requests are made only when the optional geocoder is enabled for address-like inputs.


Deterministic

The same input always produces the same output.


Conservative

Returning None is preferred over making an incorrect prediction.


Explainable

Both resolvers expose the reasoning behind their predictions, making results suitable for debugging, analytics, and machine learning pipelines.


Modular

Each resolver can be used independently, while sharing the same country resolution infrastructure.


Documentation

Detailed module documentation is available in:

  • location/README.md
  • bio/README.md

These documents describe each resolver's internal workflow, API, limitations, examples, and implementation details.


Testing

Run the full test suite with:

pytest

Roadmap

Future development may include:

  • Additional language support
  • Combined profile resolver
  • More profile signal resolvers

License

MIT License. See the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

country_resolver-0.2.0.tar.gz (21.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

country_resolver-0.2.0-py3-none-any.whl (28.7 kB view details)

Uploaded Python 3

File details

Details for the file country_resolver-0.2.0.tar.gz.

File metadata

  • Download URL: country_resolver-0.2.0.tar.gz
  • Upload date:
  • Size: 21.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for country_resolver-0.2.0.tar.gz
Algorithm Hash digest
SHA256 c24aa9b03f8237834f952846b51cc7768b625d3825c4a000c6dd3c6e52e1564d
MD5 09b60c68c0cc1c9d387b922c9b9c8913
BLAKE2b-256 52b388a72351e0e249b64590b16c79e3477bbad8030137fd7154c78fb0d5b44b

See more details on using hashes here.

Provenance

The following attestation bundles were made for country_resolver-0.2.0.tar.gz:

Publisher: publish.yml on dinyelum/country-resolver

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file country_resolver-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for country_resolver-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 08377d71b2be1121b4c3dde9a6024eab4d72e1cc6c816a32eca386ef0c08df9d
MD5 ed8f453bfbb8558a21d68d48231ae168
BLAKE2b-256 9d6ebfd0bdb982949ffe80b770cc162b2ab4fb9bd67c0de39af5c2407d348369

See more details on using hashes here.

Provenance

The following attestation bundles were made for country_resolver-0.2.0-py3-none-any.whl:

Publisher: publish.yml on dinyelum/country-resolver

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page