-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshingling.py
executable file
·56 lines (45 loc) · 1.52 KB
/
shingling.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# Shingling.py
import argparse
def main(argv):
# Define arguments
parser = argparse.ArgumentParser(description="Pass two sentences...")
parser.add_argument(
"text_a",
type=str,
help="text 1 to create shingles",
)
parser.add_argument(
"text_b",
type=str,
help="text 2 to create shingles and compare for similarity.",
)
parser.add_argument(
"-k",
type=int,
default=5,
help="the size of shingles",
)
args = parser.parse_args(argv[1:])
try:
similarity_score = compute_jaccard_similarity(args.text_a, args.text_b, args.k)
print(f"Jaccard Similarity: {similarity_score:.4f}")
except Exception as err:
print(err)
parser.print_help()
def create_shingles(text, k=5):
"""Generates a set of shingles for given text."""
return set(text[i: i + k] for i in range(len(text) - k + 1))
def compute_jaccard_similarity(text_a, text_b, k):
"""Calculates the Jaccard similarity between two shingle sets."""
shingles_a = create_shingles(text_a.lower(), k)
print("Shingles for text_a is ", shingles_a)
shingles_b = create_shingles(text_b.lower(), k)
print("Shingles for text_b is ", shingles_b)
intersection = len(shingles_a & shingles_b)
union = len(shingles_a | shingles_b)
print("Intersection - text_a ∩ text_b: ", intersection)
print("Union - text_a ∪ text_b: ", union)
return intersection / union
if __name__ == "__main__":
import sys
main(sys.argv)