I have this bit of code from https://towardsdatascience.com/training-your-own-message-suggestions-model-using-deep-learning-3609c0057ba8 which I am trying right now. However, I am getting the error: "ValueError: I/O operation on closed file" on the target_similarity_matrix_file.flush() line. How do I fix this issue? I am extremely new to python and nlp so please be gentle. I do not have any prior experience with this, so I am unable to troubleshoot.
with open('target_similarity_matrix.txt', 'w') as target_similarity_matrix_file:
for i in tqdm(range(len(target_texts))):
neighbor_index, distances = targetAnnoyIndex.get_nns_by_item(i, len(target_texts), include_distances=True)
target_similarity_row = [-1] * len(target_texts)
for index in range(len(neighbor_index)):
j = neighbor_index[index]
target_similarity_row[j] = distances[index]
target_similarity_matrix_file.write(str(target_similarity_row))
if i != len(target_texts) - 1:
target_similarity_matrix_file.write("\n")
target_similarity_matrix_file.flush()
target_similarity_matrix_file.close()
"With" automatically close and flush the file, so just delete the 2 last lines, the file is already closed, you don't need to flush/close it. Close already flushes the file, flush on a file is used usually when you've a lot of writes pending in the os cache and you want them on the filesystem.