自然语言处理学习1:nltk英文分句WordPunctTokenizer、分词word_tokenize和词频统计FreqDist

1. 分句Sentences Segment:使用nltk中的punkt句子分割器进行断句

加载:nltk.data.load('tokenizer/punkt/english.pickle')

import nltk
from nltk.tokenize import WordPunctTokenizer
import numpy as np
# 输入一个段落,分成句子(Punkt句子分割器)
paragraph = "Life  is  not  easy  for  any  of  us. We  must  work,and  above  all \
 we must  believe  in  ourselves. We  must  believe  that  each  one  of  us  is  \
 able to  do  something well, and  that, when  we  discover  what  this  something  is,\
 we  must  work  hard  at  it  until we  succeed."
#将所有大写字母转换为小写字母
paragraph = paragraph.lower()
#加载punkt句子分割器
sen_tokenizer = nltk.data.load('tokenizers/punkt/english.pickle') 
#对句子进行分割
sentences = sen_tokenizer.tokenize(paragraph) 

2. 分词word tokenize:使用nltk.word_tokenize(text)

3. 词频统计FreqDist

#nltk.FreqDist返回一个词典,key是不同的词,value是词出现的次数
freq_dist = nltk.FreqDist(words)
freq_list = []
num_words = len(freq_dist.values())
for i in range(num_words):
    freq_list.append([list(freq_dist.keys())[i],list(freq_dist.values())[i]])
freqArr = np.array(freq_list)

  • 12
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值