国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

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

這篇具有很好參考價值的文章主要介紹了AttributeError: ‘str‘ object has no attribute ‘word‘。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報違法"按鈕提交疑問。

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) # 總的文檔的數(shù)目
    # 每個詞出現(xiàn)的文檔數(shù)
    for doc in doc_list:
        for word in set(doc):
            idf_dic[word] = idf_dic.get(word, 0.0) + 1.0

    # 按照idf公式進(jìn)行轉(zhuǎn)換
    for key, value in idf_dic.items():
        # 加1是拉普拉斯平滑,防止部分新詞在語料庫中沒有出現(xiàn)導(dǎo)致分母為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 = '文學(xué)'

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

AttributeError: ‘str‘ object has no attribute ‘word‘,word,python,tf-idf各位大佬怎么搞啊這個文章來源地址http://www.zghlxwxcb.cn/news/detail-734229.html

到了這里,關(guān)于AttributeError: ‘str‘ object has no attribute ‘word‘的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 已解決AttributeError: ‘str‘ object has no attribute ‘decode‘異常的正確解決方法,親測有效?。?!

    已解決AttributeError: ‘str’ object has no attribute \\\'decode’異常的正確解決方法,親測有效?。?! AttributeError: ‘str‘ object has no attribute ‘decode‘ 這個錯誤通常是因?yàn)槟銍L試在一個字符串對象上調(diào)用 decode 方法,但是字符串對象本身沒有 decode 方法。 下滑查看解決方法 decode 方法是

    2024年02月10日
    瀏覽(48)
  • Python AttributeError: ‘NoneType‘ object has no attribute ‘shape‘

    Python AttributeError: ‘NoneType‘ object has no attribute ‘shape‘

    ? ?運(yùn)行出現(xiàn)上述錯誤,這個錯誤表示某個圖像對象為 NoneType ,沒有 \\\'shape\\\' 屬性。通常情況下,這是因?yàn)?OpenCV 沒有能夠正確地加載圖像,導(dǎo)致無法訪問圖像數(shù)據(jù)。 可以嘗試以下步驟來解決這個錯誤: 1. 檢查圖像路徑是否設(shè)置正確:檢查輸入的圖像路徑是否正確,并確保路徑

    2024年02月15日
    瀏覽(31)
  • Python 中 AttributeError: Int object Has No Attribute 錯誤

    Python 中 AttributeError: Int object Has No Attribute 錯誤

    int 數(shù)據(jù)類型是最基本和最原始的數(shù)據(jù)類型之一,它不僅在 Python 中,而且在其他幾種編程語言中都用于存儲和表示整數(shù)。 只要沒有小數(shù)點(diǎn),int 數(shù)據(jù)類型就可以存儲任何正整數(shù)或負(fù)整數(shù)。 本篇文章重點(diǎn)介紹并提供了一種解決方案,以應(yīng)對我們在 Python 中使用 int 數(shù)據(jù)類型時可能

    2024年02月04日
    瀏覽(33)
  • python: AttributeError: ‘tuple‘ object has no attribute ‘xx‘

    python: AttributeError: ‘tuple‘ object has no attribute ‘xx‘

    在使用argparse模塊創(chuàng)建一個包含命令行中所有參數(shù)的對象,后續(xù)調(diào)用時出現(xiàn)這個錯誤。 ?原始代碼如下: 在Pycharm中執(zhí)行這個命令時,報錯: 但這個錯誤很奇怪,如果是在Anaconda的spyder中執(zhí)行的話是沒有報錯的。 如果想要在Pycharm中執(zhí)行此命令,需要把parser.parse_args()改成parse

    2024年02月05日
    瀏覽(44)
  • Python報錯:AttributeError: ‘ImageDraw‘ object has no attribute ‘textbbox‘

    報錯原因是pillow的版本過低,導(dǎo)致不能使用 解決方法: 打開Anaconda prompt查看下載列表 刪除原有的pillow: ?從新下載pillow: 如果上面的命令報錯下載不成功嘗試下面的代碼:(從清華鏡像下載pillow) 其他下載鏡像: 清華:https://pypi.tuna.tsinghua.edu.cn/simple 阿里云:http://mirror

    2024年02月06日
    瀏覽(30)
  • Python 中 AttributeError: ‘NoneType‘ object has no attribute ‘X‘ 錯誤

    Python 中 AttributeError: ‘NoneType‘ object has no attribute ‘X‘ 錯誤

    Python “ AttributeError: ‘NoneType’ object has no attribute ” 發(fā)生在我們嘗試訪問 None 值的屬性時,例如 來自不返回任何內(nèi)容的函數(shù)的賦值。 要解決該錯誤,請在訪問屬性之前更正分配。 這是一個非常簡單的示例,說明錯誤是如何發(fā)生的。 嘗試訪問或設(shè)置 None 值的屬性會導(dǎo)致錯誤

    2024年02月06日
    瀏覽(25)
  • 【Python】成功解決AttributeError: ‘list‘ object has no attribute ‘replace‘

    【Python】成功解決AttributeError: ‘list‘ object has no attribute ‘replace‘

    【Python】成功解決AttributeError: ‘list’ object has no attribute ‘replace’ ?? 個人主頁:高斯小哥 ?? 高質(zhì)量專欄:Matplotlib之旅:零基礎(chǔ)精通數(shù)據(jù)可視化、Python基礎(chǔ)【高質(zhì)量合集】、PyTorch零基礎(chǔ)入門教程?? 希望得到您的訂閱和支持~ ?? 創(chuàng)作高質(zhì)量博文(平均質(zhì)量分92+),分享更多

    2024年03月25日
    瀏覽(25)
  • 【python|OpenCV】AttributeError: ‘NoneType‘ object has no attribute ‘shape‘

    當(dāng)使用OpenCV出現(xiàn)這個錯誤時,但是又沒有中文路徑或者路徑的錯誤時,可能是版本沒有對上,或者是其他的問題,我也用過很多博主的辦法,但是都沒辦法解決的時候,真的可以試一下 直接刪除,再重新下載。 目錄 情況描述 我的做法 感想 留言 本來我使用的是版本是4.7.0,

    2024年02月03日
    瀏覽(25)
  • 【Python】成功解決AttributeError: ‘list‘ object has no attribute ‘split‘

    【Python】成功解決AttributeError: ‘list‘ object has no attribute ‘split‘

    【Python】成功解決AttributeError: ‘list‘ object has no attribute ‘split‘ ?? 個人主頁:高斯小哥 ?? 高質(zhì)量專欄:Matplotlib之旅:零基礎(chǔ)精通數(shù)據(jù)可視化、Python基礎(chǔ)【高質(zhì)量合集】、PyTorch零基礎(chǔ)入門教程?? 希望得到您的訂閱和支持~ ?? 創(chuàng)作高質(zhì)量博文(平均質(zhì)量分92+),分享更多關(guān)

    2024年04月14日
    瀏覽(27)
  • Python 中出現(xiàn)AttributeError: ‘Event‘ object has no attribute ‘key‘

    《python編程從入門到實(shí)踐》中在學(xué)習(xí)外星人入侵項(xiàng)目中運(yùn)行程序時出現(xiàn)報錯 AttributeError: \\\'Event\\\' object has no attribute \\\'key\\\' 錯誤代碼如下: 運(yùn)行錯誤提示 導(dǎo)致錯誤的原因?yàn)椤?#按Q鍵退出游戲”這部分程序中“elif event.key == pygame.K_q:”這句語句寫在了與 事件類型 “event.type == pygame

    2024年02月11日
    瀏覽(33)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包