考虑以下课程和功能:
class DictEntry:
term = ""
docFreq = 0
termFreq = 0
postingList = []
def __init__(self, t, df, tf, pl):
self.term = t
self.docFreq = df
self.termFreq = tf
self.postingList = pl
def insertDict(docId, token, termFreq, dictionary):
entry = dictionary.get(token)
if entry is None:
postList = []
entry = DictEntry(token, 0, 0, postList)
dictionary[token] = entry
entry.docFreq +=1
entry.postingList.extend([docId])
entry.termFreq += termFreq
函数insertDict
为密钥创建一个对象,如果它不在字典中。
在main中,我需要将dictionary
与键和类实例一起保存到文件中。