今天主要对从CSDN爬取的标题利用jieba(结巴)进行分词,但在分词过程中发现,如大数据被分成了大/数据,云计算被分隔成了云/计算。

后来又从百度百科---》信息领域爬取了相关词语作为词典,预计今天晚上完成切词任务。

其中分割代码如下:

 import jieba
import io #对句子进行分词
def cut():
f=open("E://luntan.txt","r+",encoding="utf-8")
for line in f:
seg_list=jieba.cut(line)
#print(' '.join(seg_list))
for i in seg_list:
print(i)
write(i+" ")
#write(' '.join(seg_list)) #分词后写入
def write(contents):
f=open("E://luntan_cut.txt","a+",encoding="utf-8")
f.write(contents)
print("写入成功!")
f.close() #创建停用词
def stopwordslist(filepath):
stopwords = [line.strip() for line in open(filepath, 'r', encoding='utf-8').readlines()]
return stopwords # 对句子进行去除停用词
def seg_sentence(sentence):
sentence_seged = jieba.cut(sentence.strip())
stopwords = stopwordslist('E://stop.txt') # 这里加载停用词的路径
outstr = ''
for word in sentence_seged:
if word not in stopwords:
if word != '\t':
outstr += word
outstr += " "
return outstr #循环去除
def cut_all():
inputs = open('E://luntan_cut.txt', 'r', encoding='utf-8')
outputs = open('E//luntan_stop', 'w')
for line in inputs:
line_seg = seg_sentence(line) # 这里的返回值是字符串
outputs.write(line_seg + '\n')
outputs.close()
inputs.close() if __name__=="__main__":
cut()

分割后的文本

从百度爬取词典要把百度页面 地址:https://baike.baidu.com/wikitag/taglist?tagId=76607

该页拉到最下,并存为本地mhtml格式,在浏览器打开然后右击查看源代码,源代码保存为txt格式文件,

代码如下:

 import io
import re patton=re.compile(r'title=".*"')
def read():
f=open("E://mhtml.txt","r+",encoding="utf-8")
for line in f:
line=line.rstrip("\n")
m=patton.findall(line)
#print(line)
if len(m)!=0:
print(m)
write(str(m).lstrip("['title=\"").rstrip("\"']")+"\r") def write(contents):
f=open("E://xinxi.txt","a+",encoding="utf-8")
f.write(contents)
print("写入成功!")
f.close() if __name__=="__main__":
read()

效果:

最新文章

  1. H5程序员如何利用cordova开发跨平台应用
  2. ***HTML +CSS 总结与归纳
  3. java即时通信小例子
  4. 如何判断pc或者移动端
  5. Array-练习-自定义功能
  6. JDK注解替代Hibernate的Entity映射
  7. Unity3D 中 Generic 动画导入设置和 Root Motion 之间的关系
  8. Cdn加速
  9. JavaScript之四种继承方式讲解
  10. 第一天开通博客,就粗略写一下刚了解TCP/IP协议工作过程
  11. 如果浏览器自动调用quirks模式打开的话
  12. asp图片化电话号码,避免蜘蛛之类爬走用户隐私
  13. sequence1(暴力)
  14. Linux常用操作
  15. 图的基本算法(BFS和DFS)(转载)
  16. ORACLE - 管理控制文件
  17. 2.6 datetime 模块
  18. python ftp 传输文件
  19. web基础----->jersey整合jetty开发restful应用(一)
  20. Kintone学习

热门文章

  1. CSS权威指南(第三版)
  2. css3新增边框、阴影、边框、背景、文本、字体
  3. 【POJ - 2533】Longest Ordered Subsequence (最长上升子序列 简单dp)
  4. PHP0017:PHP http协议 视频下载
  5. python_函数笔记
  6. EasyUI笔记(三)Window窗口
  7. VMware与Centos系统安装之重置root密码
  8. 论文-MobileNet-V1、ShuffleNet-V1、MobileNet-V2、ShuffleNet-V2、MobileNet-V3
  9. 深入浅出Mybatis系列十-SQL执行流程分析(源码篇)
  10. 从Windows10中彻底删除【3D对象】文件夹