自然语言处理基础

2017-12-07  本文已影响0人  王志坦

前言

环境配置

1.正则表达式

2.NLP中常用linux shell命令

3.中文分词:jieba包的使用

#encoding:utf8
#Created on 2017-12-06    by  wzt

#引入需要的包
import jieba
import codecs   #自然语言编码转换模块
import csv
from collections import Counter  
import sys
reload(sys)
sys.setdefaultencoding('utf8')

#数据目录
data_path = '/home/wang/Desktop/bat/data20171204/normal.txt'
result_path = '/home/wang/Desktop/bat/data20171204/result.csv'

#切出来是一个大list
with codecs.open(data_path,'r','utf-8') as f:
    seg_list = jieba.cut(f.read(),cut_all=False)
    words_list = list(seg_list)

#使用Counter方法选取词频top100
count = Counter(words_list)
most100 = count.most_common(100)
print most100

#写入result.csv
for x in xrange(len(most100)):
    print '词语:[',most100[x][0],']==>','频次:',most100[x][1]
    with codecs.open(result_path, 'a','utf-8') as csvfile:
        writer = csv.writer(csvfile)
        rows = most100[x][0],most100[x][1]
        writer.writerow(rows)

结尾

如果有写的不好的地方或者错误的地方,请各位看官斧正!
谢谢大家!

上一篇 下一篇

猜你喜欢

热点阅读