AttributeError: ‘str‘ object has no attribute ‘word‘

bash 复制代码
def stopword():
    stop_word_path = r'C:/Users/DELL/douban/douban/cn_stopwords.txt'
    stopword_list = [sw.replace('\n', '') for sw in open(stop_word_path,encoding='utf-8').readlines()]
    return stopword_list
def cut_word(sentence):
    seg_list = jieba.cut(sentence)
    return seg_list
def word_filter(seg_list):
    stopword_list = stopword()
    filter_list = []
    for seg in seg_list:
        word = seg.word
        flag = seg.flag
        if not flag.startswith('n'):
            continue
        if not word in stopword_list and len(word) > 1:
            filter_list.append(word)
    return filter_list
def tf_value(filter_list):
    filter_list = filter_list
    tf_value_dict = {}
    tf_value = {}
    for word in filter_list:
        tf_value_dict[word] = tf_value_dict.get(word, 0.0) + 1.0
    for key, value in tf_value_dict.items():
        tf_value[key] = float(value / len(filter_list))
    return tf_value

def load_data():
    corpus_path = r'C:/Users/DELL/douban/douban/why.txt'
    doc_list = []
    for line in open(corpus_path, 'r', encoding='utf-8'):
        content = str(line.strip())
        seg_list = cut_word(content)
        filter_word = word_filter(seg_list)
        doc_list.append(filter_word)
    return doc_list
def train_idf():
    doc_list = load_data()
    idf_dic = {}
    total_doc_num = len(doc_list) # 总的文档的数目
    # 每个词出现的文档数
    for doc in doc_list:
        for word in set(doc):
            idf_dic[word] = idf_dic.get(word, 0.0) + 1.0

    # 按照idf公式进行转换
    for key, value in idf_dic.items():
        # 加1是拉普拉斯平滑,防止部分新词在语料库中没有出现导致分母为0
        idf_dic[key] = math.log(total_doc_num / (1.0 + value))
    return idf_dic
def tf_idf(tf):
    tf_value_dict = tf # tf的值,tf_value是个字典
    idf_value = train_idf() # idf的值,idf是个字典
    tf_idf_dict = {}
    for key, value in tf_value_dict.items():
        tf_idf_dict[key] = value
        for key_idf, value_idf in idf_value.items():
            if key == key_idf:
                tf_idf_dict[key] = value * value_idf
    return tf_idf_dict

def rank():
    keyword_num = 10
    tf_idf_dict = tf_idf(tf)
    final_dict = sorted(tf_idf_dict.items(), key = lambda x: x[1], reverse = True)
    for i in range(0, len(final_dict)):
        print(final_dict[i][0] + '/', end = '')
        if i > 10:
            break
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import jieba
import warnings
warnings.filterwarnings('ignore')
import  ssl
ssl._create_default_https_context = ssl._create_unverified_context



if __name__ == '__main__':
    text = '文学'

    seg_list = cut_word(text)
    filter_word = word_filter(seg_list)
    tf = tf_value(filter_word)
    tf_idf(tf)
    rank()

各位大佬怎么搞啊这个

相关推荐
hummhumm20 分钟前
第 25 章 - Golang 项目结构
java·开发语言·前端·后端·python·elasticsearch·golang
杜小满24 分钟前
周志华深度森林deep forest(deep-forest)最新可安装教程,仅需在pycharm中完成,超简单安装教程
python·随机森林·pycharm·集成学习
Better Bench29 分钟前
如何将Latex的文章内容快速用word+Endnote排版
word·latex
是Yu欸30 分钟前
【Word】一键批量引用论文上标——将正文字体改为上标格式
word
Morantkk34 分钟前
Word和Excel使用有感
word·excel
databook1 小时前
『玩转Streamlit』--布局与容器组件
python·机器学习·数据分析
nuclear20112 小时前
使用Python 在Excel中创建和取消数据分组 - 详解
python·excel数据分组·创建excel分组·excel分类汇总·excel嵌套分组·excel大纲级别·取消excel分组
Lucky小小吴2 小时前
有关django、python版本、sqlite3版本冲突问题
python·django·sqlite
GIS 数据栈3 小时前
每日一书 《基于ArcGIS的Python编程秘笈》
开发语言·python·arcgis
爱分享的码瑞哥3 小时前
Python爬虫中的IP封禁问题及其解决方案
爬虫·python·tcp/ip