Python: 全文件詞頻(term frequency,簡稱TF)計算, 文件中出現最多次的是那一個字?出現幾次?

加入好友
加入社群
Python: 全文件詞頻(term frequency,簡稱TF)計算, 文件中出現最多次的是那一個字?出現幾次? - 儲蓄保險王

Python: 全文件詞頻(term frequency,簡稱TF)計算, 文件中出現最多次的是那一個字?出現幾次? - 儲蓄保險王

程式碼:

# -*- coding: utf-8 -*-
fileName = r”C:\Python\myfind\telling_love.txt”

from typing import Tuple
def calculateTermFrequency(fileName : str) -> Tuple[int,dict]:
    with open( fileName,”r”  ) as f:
        dataStr = f.read()
       
    lstAll = dataStr.split(” “)
    number = len( set(lstAll) ) ; #print(number)
#解答用len(list)當詞頻的分母,我用len(set)當分母
    dic = {}
   
    for ele in lstAll:
        if ele not in dic : dic[ele] = 1/number
        else: dic[ele] += 1/number    
    return number,dic  

if __name__ == “__main__”:
    number,dic = calculateTermFrequency(fileName)
    print(“字詞共有%d個”%number)  
    print(“字典:”,dic)

Python: 全文件詞頻(term frequency,簡稱TF)計算, 文件中出現最多次的是那一個字?出現幾次? - 儲蓄保險王

參考解答:

Python: 全文件詞頻(term frequency,簡稱TF)計算, 文件中出現最多次的是那一個字?出現幾次? - 儲蓄保險王

出現最多次的字是什麼?

出現幾次?

# -*- coding: utf-8 -*-
fileName = r”C:\Python\term_frequency\telling_love.txt”
from typing import Tuple,List

def calculateTermFrequency(fileName : str) -> Tuple[int,dict]:
    with open( fileName,”r”) as f:
        dataStr = f.read()
    lstAll = dataStr.split(” “)
    number = len( set(lstAll) ) ; #print(number)
#解答使用len(list)當詞頻的分母,我使用len(set)當分母
    dic = {}
    for ele in lstAll:
        if ele not in dic : dic[ele] = 1/number
        else: dic[ele] += 1/number
    return number,dic

def getMaxCountTerm(dic) -> Tuple[ List[str] ,float]:
    cntLst=[]
    maxcount = max( dic.values() ) 
    #dic.values() ;要()
    for k in dic:
        if dic[k] == maxcount:
            cntLst.append(k)
    return cntLst,maxcount

if __name__ == “__main__”:
    number,dic = calculateTermFrequency(fileName)
    print(“字詞共有%d個”%number)
    print(“字典:”,dic)
    cntLst,maxcount = getMaxCountTerm(dic)
    print(“出現最多的字:”,cntLst)
    print(“出現%d次” %(maxcount*number))

Python: 全文件詞頻(term frequency,簡稱TF)計算, 文件中出現最多次的是那一個字?出現幾次? - 儲蓄保險王

輸出結果:
出現最多的字: [‘love’]
出現35次

 

資料夾中有三個txt檔

讀取這些檔案後

輸出一個長度3的list

元素為dict

dict的key為字詞

value為詞頻

Python: 全文件詞頻(term frequency,簡稱TF)計算, 文件中出現最多次的是那一個字?出現幾次? - 儲蓄保險王

推薦hahow線上學習python: https://igrape.net/30afN

telling_love.txt:

how do you define love why assuming we all mean the same thing is dangerous tis the season to celebrate love every year when valentines day comes around most of us focus on romantic love but when you stop to think about it there are many levels and types of love i love my husband i love my sister i love my dog i love my career i love warm nights although im using one word to describe my feeling toward all these objects most people understand what im saying i love my husband as my life and romantic partner i love my sister well as a sister i trust her and share some of my deepest thoughts with her my dog opens my heart i enjoy my work and warm nights make me feel relaxed and happy the greeks had the good sense to break love into four levels storge was kinship philia was friendship eros sexual and romantic love and finally divine love was known as agape they might interpret the sentence i love you but im not in love with you to mean i feel philia toward you but not eros but while the greeks gave love four spots in the dictionary this emotion was feared both plato and socrates saw this emotion as love is a serious mental disease and love is a madness and it was the greeks who coined the phrase lovesick love makes people do stupid things dangerous things as well as magnanimous and bold things but what is love really because people define love differently a common trap is for couples is to assume they are speaking about the same thing and because people define love differently they show it differently and have different expectations of what it should look and feel like many if not most of the problems couples experience is a result of a miscommunicated love or a dashed expectation around love and connection many of us show love in the ways we hope to receive love the golden rule of doing unto others as you would have others do unto you but this assumes your partner defines love the same way you do in fact the couples who come in to see me for therapy have been missing the mark for years by the time they come to therapy they have had years of pain and hurt because they have made too many assumptions about love one wanted physical connection the other wanted to go on a walk together one wanted to buy gifts to show affection but the other would rather have had him or her do the dishes pick up the dry cleaning or even put money into the savings account rather than spend it because thats their definition of love a book i often recommend to clients is gary chapmans 5 love languages because the author does a great job of outlining the five areas people give and receive love when you know what matters to you you can ask for this from your partner the areas are as follows words of affirmation physical touch acts of service gifts quality time are you making assumptions about what love means to your spouse or partner one way you can tell is by checking your resentment levels if they are high ask yourself why when couples start speaking the same language they begin to feel understood acknowledged and appreciated when couples stop making assumptions about what love means to their partner they start having better conversations and they begin to relate more consciously funny how something that seems so simple is actually quite complicated if your relationship is a bit rocky use this article as a conversation starter this valentines day and see what happens do your best to understand your partner and you may be amazed to see that he or she tried harder to understand you wishing you conscious and purposeful love today and every day no part of this publication may be reproduced without the express written permission of the author failure to comply with these terms may expose you to legal action and damages for copyright infringement

 

加入好友
加入社群
Python: 全文件詞頻(term frequency,簡稱TF)計算, 文件中出現最多次的是那一個字?出現幾次? - 儲蓄保險王

儲蓄保險王

儲蓄險是板主最喜愛的儲蓄工具,最喜愛的投資理財工具則是ETF,最喜愛的省錢工具則是信用卡

You may also like...

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *