• Python数据挖掘-中文分词


    将一个汉字序列切分成一个一个单独的词

    安装分词模块: pip install jieba

    分词在特殊场合的实用性,调用add_word(),把我们要添加的分词加入jieba词库

    高效方法:将txt保存的词库一次性导入用户词库中

    import jieba
    jieba.load_userdict("D:\Python\Python数据挖掘\Python数据挖掘实战课程课件\2.2\金庸武功招式.txt")

    1、搭建语料库

    import os
    import os.path
    import codecs
    
    filePaths=[]
    fileContents=[]
    for root,dirs,files in os.walk("D:\Python\Python数据挖掘\Python数据挖掘实战课程课件\2.2\SogouC.mini\Sample"):
        for name in files:
            filePath=os.path.join(root,name)
            filePaths.append(filePath)
            f=codecs.open(filePath,"r","utf-8")
            fileContent=f.read()
            f.close()
            fileContents.append(fileContent)
            
    import pandas
    corpos=pandas.DataFrame({
                             "filePath":filePaths,
                             "fileContent":fileContents})

    2、介绍分词来自哪篇文章

    import jieba
    
    segments=[]
    filePaths=[]
    for index,row in corpos.iterrows():   #这样遍历得到的行是一个字典,row()是一个字典
        filePath=row["filePath"]
        fileContent=row["fileContent"]
        segs=jieba.cut(fileContent)   #调用cut方法对文件内容进行分词
        for seg in segs:
            segments.append(seg)
            filePaths.append(filePath)
            
    segmentDataFrame=pandas.DataFrame({
                                       "segment":segments,
                                       "filepath":filePaths})

    使用数据框的遍历方法,得到语料库中的每行数据,列名作为key

    查了一下相关iterrows()的资料;

    iterrows()返回值为元组,(index,row) 
    上面的代码里,for循环定义了两个变量,index,row,那么返回的元组,index=index,row=row. 

  • 相关阅读:
    【转载】Android IntentService使用全面介绍及源码解析
    【转载】作为Android开发者,你真的熟悉Activity吗?
    【转载】从使用到原理学习Java线程池
    Linux 基础知识+常见命令
    Python中的字典合并
    Python的__hash__函数和__eq__函数
    C#中实现Hash应用
    深入理解python元类
    Docker搭建Gitlab服务器
    python的加密方式
  • 原文地址:https://www.cnblogs.com/U940634/p/9735869.html
Copyright © 2020-2023  润新知