Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with pairwise comparison in compare_documents function #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions pysimilar/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,29 +111,27 @@ def compare_documents(self, path_to_documents: Union[str, Path], sort=True, asce
list: [description]
"""
if not os.path.exists(path_to_documents):
raise FileNotFoundError(
f'Path <{path_to_documents}> Does not exist')
raise FileNotFoundError(f'Path <{path_to_documents}> Does not exist')

loaded_documents: Dict = self.load_files(path_to_documents)
vectorized_documents = self.vectorize_dict(loaded_documents)
compared_documents: List[set] = []
comparison_results: List[list] = []
for current_document_name, current_content in vectorized_documents.items():
for document_name, content in vectorized_documents.items():
document_names = list(vectorized_documents.keys())
for i, current_document_name in enumerate(document_names):
for document_name in document_names[i + 1:]:
current_comparison = f'{current_document_name} vs {document_name}'
if (current_document_name == document_name) or (set(current_comparison) in compared_documents):
continue
result = self.compute_similarity(current_content, content)
result = self.compute_similarity(vectorized_documents[current_document_name],
vectorized_documents[document_name])
displayable_result = [current_comparison, result]
comparison_results.append(displayable_result)
# print(displayable_result)
compared_documents.append(set(current_comparison))

if not sort:
return comparison_results

sorted_results = sorted(
comparison_results, key=lambda x: x[1], reverse=not ascending)
sorted_results = sorted(comparison_results, key=lambda x: x[1], reverse=not ascending)

return sorted_results

def compute_similarity(self, vector_a: list, vector_b: list) -> float:
Expand Down