Skip to content

Commit c424a93

Browse files
authored
Merge pull request #6 from ianzur/citation-common-phrase-div-by-zero
Citation common phrase div by zero
2 parents aeec310 + d838bec commit c424a93

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

src/academic_tracker/helper_functions.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,7 @@ def _compute_common_phrase_percent(prev_citation, new_citation, characters_to_re
669669
min_len (int): the minimum length of a subphrase.
670670
671671
Returns:
672-
((int, int)|None): if either citation is None, then return None, else the percentage of common to uncommon phrase length for each citation.
672+
((int, int)|None): if either citation is None or empty after character removal and stripping, then return None, else the percentage of common to uncommon phrase length for each citation.
673673
"""
674674
if prev_citation and new_citation:
675675
citation_strip_regex = "|".join([f"\{char}" for char in characters_to_remove])
@@ -685,8 +685,12 @@ def _compute_common_phrase_percent(prev_citation, new_citation, characters_to_re
685685
prev_citation_common_phrases_removed = prev_citation_common_phrases_removed.replace(phrase.strip(), "")
686686
new_citation_common_phrases_removed = new_citation_common_phrases_removed.replace(phrase.strip(), "")
687687
common_base_string = "".join(common_subphrases)
688-
prev_common_percentage = len(common_base_string) / len(common_base_string + prev_citation_common_phrases_removed.strip()) * 100
689-
new_common_percentage = len(common_base_string) / len(common_base_string + new_citation_common_phrases_removed.strip()) * 100
688+
prev_common_denom = len(common_base_string + prev_citation_common_phrases_removed.strip())
689+
new_common_denom = len(common_base_string + new_citation_common_phrases_removed.strip())
690+
if prev_common_denom == 0 or new_common_denom == 0:
691+
return None
692+
prev_common_percentage = len(common_base_string) / prev_common_denom * 100
693+
new_common_percentage = len(common_base_string) / new_common_denom * 100
690694

691695
return prev_common_percentage, new_common_percentage
692696
else:

0 commit comments

Comments
 (0)