Skip to main content

对Google的langdetect进行了修改,使其单独作用于中文文本并获得更好的表现

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):

zh-cn, zh-tw

Basic usage

可以参考下列调用:

>>> from langdetect_zh import detect
>>> detect("这是一段中文文本")
'zh-cn'

To find out the probabilities for the top languages:

>>> from langdetect_zh import detect_langs
>>> detect_langs("这是一段中文文本")

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_zh 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_zh-1.0.3.tar.gz (61.0 kB view details)

Uploaded Source

Built Distribution

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

langdetect_zh-1.0.3-py3-none-any.whl (63.3 kB view details)

Uploaded Python 3

File details

Details for the file langdetect_zh-1.0.3.tar.gz.

File metadata

  • Download URL: langdetect_zh-1.0.3.tar.gz
  • Upload date:
  • Size: 61.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.16

File hashes

Hashes for langdetect_zh-1.0.3.tar.gz
Algorithm Hash digest
SHA256 7ded924395d7f2c3f5ebb6423802396d047454ba9b7b85745007aba137809eb2
MD5 39faff9ee852a8510101b67c9086f726
BLAKE2b-256 1466a5256cebe674fe0f1c396b76d21976136daae66c2bd78ba10a5f9f980b44

See more details on using hashes here.

File details

Details for the file langdetect_zh-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: langdetect_zh-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 63.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.16

File hashes

Hashes for langdetect_zh-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 1f42b5972617c2f7541172d676f84aa3e94e6b5f24645759085a245f7ac2c3a5
MD5 ed6e3afd5b9219cd7c8a41df7cd9e72e
BLAKE2b-256 465bf130ff21d8bef9e45123ac3e4a55840c8930188bc2c0803bdfc5cfe8b0a8

See more details on using hashes here.

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