[英]Counting words per sentence and sentences per paragraph in a text file
我無法正常化字典。 在我的字典中,我有一堆字,我們要在文本文件中計算。 現在對於這些單詞/字符中的每一個,在我的項目的上下文中,“規范化”將其頻率/值除以給定文本中的句子總數。 然后我必須用這些新的字符替換字典的舊值。
即我的字典名稱是計數,鍵和值如下:
{'and': 5, ';' : 3, '-' : 0...}
def main(textfile, normalize == True):
.
.
.
.
if normalize == True:
for x in count:
new_count[x] = count[x]/numSentence
print(x,count[x])
這是一個試用任何代碼的示例文件: https : //www.dropbox.com/s/7xph5pb9bdf551h/sample2.txt?dl=0另請注意,在上面的代碼中,normalize == True存在,因為在頂層功能
下面的代碼顯示了一個在字符串中搜索單詞的示例,例如"remember me"
有兩個匹配"me"
一個單詞“remember”,另一個是“me”,但只有一個是單詞示例:
"remember me".count('me') # output: 2
'me' in 'remember me' == 2 # True
只匹配整個單詞
'me' in 'remember me'.split() == 1 # True
所以如果我在這里正確理解你的問題,你需要匹配整個單詞:
mydict = {'and': 5, ';' : 3, '-' : 0}
text = 'hello and me; in mem;ory ; me-ome _ -'
# find a word frequency in a text
def count(word, text):
return len([w for w in text.split() if w == word])
# update dictionary with new count
mydict = {key:count(key, text) for key in mydict}
print(mydict)
輸出:
{'and': 1, ';': 0, '-': 1}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.