Skip to main content

Language detection library ported from Google's language-detection.

Project description

langdetect

Build Status

Port of Nakatani Shuyo's language-detection library (version from 03/03/2014) to Python.

Installation

$ pip install langdetect

Supported Python versions 2.7, 3.4+.

Languages

langdetect supports 55 languages out of the box (ISO 639-1 codes):

af, ar, bg, bn, ca, cs, cy, da, de, el, en, es, et, fa, fi, fr, gu, he,
hi, hr, hu, id, it, ja, kn, ko, lt, lv, mk, ml, mr, ne, nl, no, pa, pl,
pt, ro, ru, sk, sl, so, sq, sv, sw, ta, te, th, tl, tr, uk, ur, vi, zh-cn, zh-tw

Basic usage

To detect the language of the text:

>>> from langdetect import detect
>>> detect("War doesn't show who's right, just who's left.")
'en'
>>> detect("Ein, zwei, drei, vier")
'de'

To find out the probabilities for the top languages:

>>> from langdetect import detect_langs
>>> detect_langs("Otec matka syn.")
[sk:0.572770823327, pl:0.292872522702, cs:0.134356653968]

NOTE

Language detection algorithm is non-deterministic, which means that if you try to run it on a text which is either too short or too ambiguous, you might get different results everytime you run it.

To enforce consistent results, call following code before the first language detection:

from langdetect import DetectorFactory
DetectorFactory.seed = 0

How to add new language?

You need to create a new language profile. The easiest way to do it is to use the langdetect.jar tool, which can generate language profiles from Wikipedia abstract database files or plain text.

Wikipedia abstract database files can be retrieved from "Wikipedia Downloads" (http://download.wikimedia.org/). They form '(language code)wiki-(version)-abstract.xml' (e.g. 'enwiki-20101004-abstract.xml' ).

usage: java -jar langdetect.jar --genprofile -d [directory path] [language codes]

  • Specify the directory which has abstract databases by -d option.
  • This tool can handle gzip compressed file.

Remark: The database filename in Chinese is like 'zhwiki-(version)-abstract-zh-cn.xml' or zhwiki-(version)-abstract-zh-tw.xml', so that it must be modified 'zh-cnwiki-(version)-abstract.xml' or 'zh-twwiki-(version)-abstract.xml'.

To generate language profile from a plain text, use the genprofile-text command.

usage: java -jar langdetect.jar --genprofile-text -l [language code] [text file path]

For more details see language-detection Wiki.

Original project

This library is a direct port of Google's language-detection library from Java to Python. All the classes and methods are unchanged, so for more information see the project's website or wiki.

Presentation of the language detection algorithm: http://www.slideshare.net/shuyo/language-detection-library-for-java.

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

langdetect-1.0.8.tar.gz (981.5 kB view details)

Uploaded Source

Built Distribution

langdetect-1.0.8-py2-none-any.whl (993.2 kB view details)

Uploaded Python 2

File details

Details for the file langdetect-1.0.8.tar.gz.

File metadata

  • Download URL: langdetect-1.0.8.tar.gz
  • Upload date:
  • Size: 981.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/2.7.16

File hashes

Hashes for langdetect-1.0.8.tar.gz
Algorithm Hash digest
SHA256 363795ea005f1243c958e953245dac5d814fabdc025c9afa91588c5fa6b2fa83
MD5 930077845ed76e0ff6e47bdf85aab96e
BLAKE2b-256 56a38407c1e62d5980188b4acc45ef3d94b933d14a2ebc9ef3505f22cf772570

See more details on using hashes here.

File details

Details for the file langdetect-1.0.8-py2-none-any.whl.

File metadata

  • Download URL: langdetect-1.0.8-py2-none-any.whl
  • Upload date:
  • Size: 993.2 kB
  • Tags: Python 2
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/2.7.16

File hashes

Hashes for langdetect-1.0.8-py2-none-any.whl
Algorithm Hash digest
SHA256 f37495e63607865e47deed08d78f7f8e58172658216ff954b2f14671bcd87740
MD5 66253576765aec6e5dee83d1e6d62a08
BLAKE2b-256 8537bca101599caa3b915cc0735ac3bdc8a61c38b3d8b26a13c0f4d91e05bada

See more details on using hashes here.

Supported by

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