作りたいもの
・tfをかえす
・nを返す → 全部で何単語あるか
example.pyからひろってきた関数
```
def calc_tf(wds):
""" tfの作成 単語辞書のリストを受け取る """
tf = []
for d in wds:
total = sum(d.values()) # この文書内での単語の全出現数を求める
newd = {} # この文書のtfを保持する辞書
for word in d:
newd[word] = d[word]/total # この文書の各単語の出現数を全出現数で割る
tf.append(newd) # この文書のtfをリストに追加
return tf # 各文書のtfのリストを返す
wds → wdwds1
def calc_bmft(wds)
```
wdwds1の中身
[{'apple': 1, 'and': 4, 'orange': 2, 'banana': 1, 'berry': 1}, {'banana': 1, 'and': 4, 'orange': 1, 'b
erry': 2, 'grape': 1}, {'horse': 1, 'and': 6, 'zebra': 1, 'dog': 1, 'cat': 1, 'fish': 1, 'tiger': 1, '
grape': 1}, {'horse': 2, 'and': 5, 'orange': 1, 'berry': 1, 'cat': 1, 'banana': 1}]
dict = wdwds1[0]
{'apple': 1, 'and': 4, 'orange': 2, 'banana': 1, 'berry': 1}
for wdwds1の長さ
def wdict_merge(wds):
d = dict(wds[0])
for dd in wds[1:]:
for key, value in wdwds1[i].items():
if k in d:
d[k] += v
else:
d[k] = v
return d
課題1
```
def calc_bmtf(wds)
tf = calc_tf(wds)
n = 0
#nどうするか問題
for i in range(len(wdwds1)):
for k,v in wdwds1[i].items():
n +=v
```
```
def calc_bmtf(wds):
tf = calc_tf(wds)
n = 0
for i in range(len(wdwds1)):
for k, v in wdwds1[i].items():
n += int()vpp
return tf, n
```
作りたい関数の実行例
```
tf1, total1 = calc_bmtf(wdwds1)
tf2, total2 = calc_bmtf(wdwds2)
prtf("tf1", tf1)
prtf("tf2", tf2)
#↑個々までは実行できる
#↓ここからエーラー
print("total1", total1)
print("total2", total2)
```
```
data1 = ["apple and orange and orange and banana and berry",
"banana and orange and berry and berry and grape",
"horse and zebra and dog and cat and fish and tiger and grape",
"horse and horse and orange and berry and cat and banana"]
n = [9 ,9, 13, 11]
```
```
wdwds1 = [word_c(x.split()) for x in data1]
wdwds1 = [{'apple': 1, 'and': 4, 'orange': 2, 'banana': 1, 'berry': 1},
{'banana': 1, 'and': 4, 'orange': 1, 'berry': 2, 'grape': 1},
{'horse': 1, 'and': 6, 'zebra': 1, 'dog': 1, 'cat': 1, 'fish': 1, 'tiger': 1, 'grape': 1},
{'horse': 2, 'and': 5, 'orange': 1, 'berry': 1,'cat': 1, 'banana': 1}]
tf1, total1 = calc_bmtf(wdwds1)
#%% tfの作成
def calc_bmtf(wds):
""" tfの作成 単語辞書のリストを受け取る """
tf = []
n=[]
for d in wds:
total = sum(d.values()) # この文書内での単語の全出現数を求める
n.append(total)
newd = {} # この文書のtfを保持する辞書
for word in d:
newd[word] = d[word]/total # この文書の各単語の出現数を全出現数で割る
tf.append(newd) # この文書のtfをリストに追加
return tf # 各文書のtfのリストを返す
d = {'apple': 1, 'and': 4, 'orange': 2, 'banana': 1, 'berry': 1}
d.value = [1, 4, 2, 1, 1]
total = 9
```
```
#%%
def calc_tf(wds):
""" tfの作成 単語辞書のリストを受け取る """
tf = []
n=[]
for d in wds:
total = sum(d.values()) # この文書内での単語の全出現数を求める
n.append(total)
newd = {} # この文書のtfを保持する辞書
for word in d:
newd[word] = d[word]/total # この文書の各単語の出現数を全出現数で割る
tf.append(newd) # この文書のtfをリストに追加
return tf # 各文書のtfのリストを返す
#---------ここまでは同じ-----------------
#%%
def calc_bmtf(wds):
tf=calc_tf(wds)
n = []
for i in range(len(wdwds1)):
for k , v in wdwds1[i].items():
n += v
return tf,n
```
```
def calc_bmtf(wds):
tf = []
n = []
for d in wds:
total = sum(d.values()) # この文書内での単語の全出現数を求める
n.append(total)
newd = {} # この文書のtfを保持する辞書
for word in d:
newd[word] = d[word]/total # この文書の各単語の出現数を全出現数で割る
tf.append(newd) # この文書のtfをリストに追加
return tf,n
```
```
def calc_bmidf(wds, gd):
ndoc = len(gd)
idf = {}
for word in gd:
f = 0
for d in wds:
if word in d:
f += 1
tf,n = calc_bmtf(wds)
ca = (n - f + 0.5) / (f + 0.5)
idf[word]=math.log(ca)
return idf
```