python:jieba, snownlp, pinyin 拼音

本文展示了使用Python的jieba库进行中文分词,snownlp库进行自然语言处理,包括情感分析、关键词提取和文本摘要,以及pinyin库实现拼音转换和中译英词典查询的功能。示例代码详细演示了这些工具的使用方法。
部署运行你感兴趣的模型镜像

python jieba 是中文分词,snownlp 是自然语言处理,pinyin 拼音和中译英词典。

pip install jieba;

pip install snownlp;

pip install pinyin; 拼音

存在 Lib/site-packages/pinyin/cedict.py 中译英词典

测试程序 test_pinyin.py

# -*- coding: utf-8 -*-
import jieba
import snownlp
import pinyin

# statement 语句
stmt = "我爱我的祖国" 
#stmt = "我的目的地是上海浦东开发区"
seg_list = jieba.cut(stmt, cut_all=False)
words = ' '.join(seg_list)
print(words)
pyin = pinyin.get(words)
print(pyin)
print(type(seg_list))

from snownlp import SnowNLP
sn = SnowNLP(stmt)
wlist = sn.words
pinyins = sn.pinyin
print('wlist=', wlist)   # 分词:
print('pinyin:',pinyins) # 拼音:
print('sentiments:', sn.sentiments) #  # 情感分析:
print('keywords:', sn.keywords(3))  # 关键词:
print('summary:', sn.summary(3))      # 摘要/文本概括:
print('sim:', sn.sim(['我','国']))  # 词的相似度[ ]

# 中译英词典
from pinyin import cedict 
cedict.init() # 初始化 中译英词典
for w in wlist:
	english = cedict.translate_word(w, dictionary=['simplified'])
	print(f"{w}: {english}")

运行 python pinyin_test.py 后输出

Prefix dict has been built succesfully.
我 爱 我 的 祖国
wǒ ài wǒ de zǔguó
<class 'generator'>
wlist= ['我', '爱', '我', '的', '祖国']
pinyin: ['wo', 'ai', 'wo', 'de', 'zu', 'guo']
sentiments: 0.9304746506862666
keywords: ['祖国', '爱']
summary: ['我爱我的祖国']
sim: [0.5877866649021191, 0, 0.5877866649021191, 0, 0, 1.2992829841302609]
我: ['I', 'me', 'my']
爱: ['to love', 'to be fond of', 'to like', 'affection', 'to be inclined (to do sth)', 'to tend to (happen)']
我: ['I', 'me', 'my']
的: ['aim', 'clear']
祖国: ['motherland']

注意:seg_list 类型是 generator , 不是 list .

您可能感兴趣的与本文相关的镜像

Python3.10

Python3.10

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值