鱼C论坛

 找回密码
 立即注册
查看: 877|回复: 4

python自然语言处理,分词处理

[复制链接]
发表于 2018-8-31 17:12:14 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 chunguang 于 2018-8-31 17:15 编辑

python在做分词的时候,为什么下列程序运行不了
  1. import jieba
  2. import re
  3. import jieba.analyse
  4. from scipy.misc import imread
  5. from wordcloud import WordCloud
  6. from wordcloud import ImageColorGenerator
  7. import matplotlib.pyplot as plt
  8. from os import path

  9. def stopwordslist(filepath):  #停用词list
  10.     stopwords = [line.strip() for line in open(filepath, 'r', encoding='utf-8').readlines()]
  11.     return stopwords

  12. def seg_sentence(sentence):  #分词
  13.     zhongwen_pat = re.compile(r'^[\u4e00-\u9fa5a-zA-Z])
  14.     cut_list = [c for c in jieba.cut(sentence[0]) if zhongwen_pat.search(c)]
  15.     cut_set = "".join(cut_list)
  16.     sentence_seged = jieba.cut(cut_set.strip())
  17.     stopwords = stopwordslist('C:/Users/lenovo/Desktop/中文停用词.txt')  #这里加载停用词的路径
  18.     outstr = ''
  19.     for word in sentence_seged:
  20.         if word not in stopwords:
  21.             if word != '\t':   #'\t'是横向制表符,可以理解成空白
  22.                 outstr += word
  23.                 outstr += " "
  24.     return outstr

  25. inputs = open('C:/Users/lenovo/Desktop/白沙comment.txt', 'r', encoding='gbk')
  26. outputs = open('C:/Users/lenovo/Desktop/白沙comments3.txt', 'w')
  27. for line in inputs:
  28.     line_seg = seg_sentence(line)  # 这里的返回值是字符串
  29.     outputs.write(line_seg + '\n' )
  30. outputs.close()
  31. inputs.close()
复制代码


求大神
数据如下:

白沙comment.txt

36.39 KB, 下载次数: 6

数据

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-8-31 17:20:55 | 显示全部楼层
完全没用过街霸之类的,
但是你运行的代码是这么写的么
  1. def seg_sentence(sentence):  #分词
  2.     zhongwen_pat = re.compile(r'^[\u4e00-\u9fa5a-zA-Z]+

  3. 求大神
  4. 数据如下:)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-8-31 17:23:47 | 显示全部楼层
塔利班 发表于 2018-8-31 17:20
完全没用过街霸之类的,
但是你运行的代码是这么写的么

不是,我改了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-8-31 17:34:09 | 显示全部楼层

嗯,这得请大神才能解决了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-2 19:57:31 | 显示全部楼层
本帖最后由 程序员的救赎 于 2018-9-2 20:08 编辑

你的报错信息发一下。
还有,停用词文件呢?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-2 10:07

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表