Python中离线检测文本语言及中文与非中文分句有什么好用的包?

语言检测,如'你好啊...'=>'zh', 'good morning'=>'en', 'guten tag'=> 'de' 'bonsoir'=>'fr' 我只知道 langid 和 langdetect,都不是太好用,初始时间太长,短一点的中文会被检测为韩文或日文等等。

中文分句,正则再稍微处理一下大致可以用。另外,smallseg 好像也可以用,smallseg 的字典数据文件 几个 M,好像有点大。

非中文分句,nltk 里的 tokenizer 可处理各种语言(其实是用 nltk 里的 pickle 文件),可惜用了 nltk 的 Python 程序生成 exe 包时麻烦多多,因为 nltk 的 data 目录结构不在 python lib 下。另外 github 上有个 segtok 项目,好像还可以用,但性能不如 nltk。

各位高人语言检测、文本分句都用什么包?请各位分享一下,先感谢……


Python中离线检测文本语言及中文与非中文分句有什么好用的包?

7 回复

分词还是分句?


对于离线检测文本语言,推荐用 langdetectfasttext(需要下载模型文件)。langdetect 轻量,fasttext 支持语言多、准确度高。

分句方面,中文分句直接用 jiebapyltp 的句子分割功能。中英文混合时,可以用正则按标点切,但复杂情况建议用 spaCy(需下载中文模型)或 nltksent_tokenize(对英文友好,中文需调整)。

简单示例:

# 语言检测
from langdetect import detect
text = "这是一个测试句子。This is a test sentence."
print(detect(text))  # 输出 'zh-cn' 或 'en'

# 中文分句(用结巴)
import jieba
jieba.enable_paddle()  # 启用paddle模式提升分句准确度
sentences = jieba.cut(text, cut_all=False)
print(' '.join(sentences))

# 中英文混合分句(简单正则)
import re
split_sentences = re.split(r'[。!?!?]', text)
print([s.strip() for s in split_sentences if s.strip()])

总结:按需选 langdetectfasttext 做语言检测,中文分句用 jieba,混合分句考虑 spaCy

结巴分词不好用吗

感谢。

不过 snownlp 的分句逗号处也分了。

zh = snownlp.SnowNLP(‘非中文分句,nltk 里的 tokenizer 可处理各种语言(其实是用 nltk 里的 pickle 文件),可惜用了 nltk 的 Python 程序生成 exe 包时麻烦多多,因为 nltk 的 data 目录结构不在 python lib 下。另外 github 上有个 segtok 项目,好像还可以用,但性能不如 nltk。’)
zh.sentences
[‘非中文分句’,
‘nltk 里的 tokenizer 可处理各种语言(其实是用 nltk 里的 pickle 文件)’,
‘可惜用了 nltk 的 Python 程序生成 exe 包时麻烦多多’,
‘因为 nltk 的 data 目录结构不在 python lib 下’,
‘另外 github 上有个 segtok 项目’,
‘好像还可以用’,
‘但性能不如 nltk’]

而且标点符号也没了。估计是简单用 re.split 做的。

snwonlp 想做成中文版 textblob, 可惜在分句上没做足功夫。

textblob 的分句:
blob = textblob.TextBlob(‘The rider sits on and operates these vehicles like a motorcycle, but the extra wheels give more stability at slower speeds. Although equipped with three or four wheels, six-wheel models exist for specialized applications. Engine sizes of ATVs currently for sale in the United States, (as of 2008 products), range from 49 to 1,000 cc (3 to 61 cu in).’)
blob.sentences
[Sentence(“The rider sits on and operates these vehicles like a motorcycle, but the extra wheels give more stability at slower speeds.”),
Sentence(“Although equipped with three or four wheels, six-wheel models exist for specialized applications.”),
Sentence(“Engine sizes of ATVs currently for sale in the United States, (as of 2008 products), range from 49 to 1,000 cc (3 to 61 cu in).”)]

检测文本语言可以使用 NGram 来做,我在毕设中用到过,效果还是不错的

回到顶部