How would I Call the save_dictionary() function to save the document’s dictionary with TF (term frequencies) to a file, where the filename should be tf_DOCID.txt in the same path.
Below is all my code that I have and need help moving forward with it. Thanks.
class Document:
def __init__(self, doc_id):
# create a new document with its ID
self.id = doc_id
# create an empty dictionary
# that will hold the term frequency (TF) counts
self.tfs = {}
def tokenization(self, text):
# split a title into words,
# using space " " as delimiter
words = text.lower().split(" ")
for word in words:
# for each word in the list
if word in self.tfs:
# if it has been counted in the TF dictionary
# add 1 to the count
self.tfs[word] = self.tfs[word] + 1
else:
# if it has not been counted,
# initialize its TF with 1
self.tfs[word] = 1
def save_dictionary(diction_data, file_path_name):
# print the key-values pair in a dictionary
f = open("./textfiles", "w+")
for key in diction_data:
f.print(key, diction_data[key])
f.close()
def vectorize(data_path):
Document = []
for i in range(1, 21):
file_name = "./textfiles/"+ i + ".txt"
# create a new document with an ID
Document = Document(i+1)
#Read the files
f = open(Document)
print(f.read())
# compute the term frequencies
Document.tokenization(file_name)
# add the documents to the lists
Documents.append(Document)