fast langdetect
pypi_0.2.2 Support offlilne usage
Fast-LangDetect基于FackText(由Facebook开发的库)提供超快速且高度准确的语言检测。该软件包比传统方法快80倍,并且具有95%的精度。
它支持Python版本3.9至3.12。
支持离线用法。
该项目建立在Zafercavdar/fastText-langdetect上,具有增强包装。
有关基础FastText模型的更多信息,请参阅官方文档:FastText语言标识。
笔记
该库需要超过200MB的内存才能在低内存模式下使用。
要安装Fast-LangDetect,您可以使用pip或pdm :
pip install fast-langdetectpdm add fast-langdetect为了获得最佳的语言检测性能和准确性,请使用detect(text, low_memory=False)加载较大的模型。
首次使用后,该模型将下载到
/tmp/fasttext-langdetect目录。
笔记
假定此功能给出单行文本。在传递文本之前,您应该删除n字符。如果样本太长或太短,精度将降低(例如,如果太短,则将中文被预测为日语)。
from fast_langdetect import detect , detect_multilingual
# Single language detection
print ( detect ( "Hello, world!" ))
# Output: {'lang': 'en', 'score': 0.12450417876243591}
# `use_strict_mode` determines whether the model loading process should enforce strict conditions before using fallback options.
# If `use_strict_mode` is set to True, we will load only the selected model, not the fallback model.
print ( detect ( "Hello, world!" , low_memory = False , use_strict_mode = True ))
# How to deal with multiline text
multiline_text = """
Hello, world!
This is a multiline text.
But we need remove ` n ` characters or it will raise an ValueError.
"""
multiline_text = multiline_text . replace ( " n " , "" ) # NOTE:ITS IMPORTANT TO REMOVE n CHARACTERS
print ( detect ( multiline_text ))
# Output: {'lang': 'en', 'score': 0.8509423136711121}
print ( detect ( "Привет, мир!" )[ "lang" ])
# Output: ru
# Multi-language detection
print ( detect_multilingual ( "Hello, world!你好世界!Привет, мир!" ))
# Output: [{'lang': 'ja', 'score': 0.32009604573249817}, {'lang': 'uk', 'score': 0.27781224250793457}, {'lang': 'zh', 'score': 0.17542070150375366}, {'lang': 'sr', 'score': 0.08751443773508072}, {'lang': 'bg', 'score': 0.05222449079155922}]
# Multi-language detection with low memory mode disabled
print ( detect_multilingual ( "Hello, world!你好世界!Привет, мир!" , low_memory = False ))
# Output: [{'lang': 'ru', 'score': 0.39008623361587524}, {'lang': 'zh', 'score': 0.18235979974269867}, {'lang': 'ja', 'score': 0.08473210036754608}, {'lang': 'sr', 'score': 0.057975586503744125}, {'lang': 'en', 'score': 0.05422825738787651}]detect_language函数 from fast_langdetect import detect_language
# Single language detection
print ( detect_language ( "Hello, world!" ))
# Output: EN
print ( detect_language ( "Привет, мир!" ))
# Output: RU
print ( detect_language ( "你好,世界!" ))
# Output: ZH有关基于语言的文本分配,请参考拆分式倾斜存储库。
有关详细的基准结果,请参阅Zafercavdar/fastText-langdetect#基准。
[1] A. Joulin,E。Grave,P。Bojanowski,T。Mikolov,,用于有效文本分类的技巧
@article { joulin2016bag ,
title = { Bag of Tricks for Efficient Text Classification } ,
author = { Joulin, Armand and Grave, Edouard and Bojanowski, Piotr and Mikolov, Tomas } ,
journal = { arXiv preprint arXiv:1607.01759 } ,
year = { 2016 }
}[2] A. Joulin,E。Grave,P。Bojanowski,M。Douze,H。Jégou,T。Mikolov,fasttext.zip:压缩文本分类模型
@article { joulin2016fasttext ,
title = { FastText.zip: Compressing text classification models } ,
author = { Joulin, Armand and Grave, Edouard and Bojanowski, Piotr and Douze, Matthijs and J{'e}gou, H{'e}rve and Mikolov, Tomas } ,
journal = { arXiv preprint arXiv:1612.03651 } ,
year = { 2016 }
}