-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweighted-shingling.py
67 lines (54 loc) · 1.55 KB
/
weighted-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
57
58
59
60
61
62
63
64
65
66
67
import re
import argparse
import json
def main(argv):
# Define arguments
parser = argparse.ArgumentParser(description="Pass a text sentence with size and weights...")
parser.add_argument(
"text",
type=str,
help="The input text.",
)
parser.add_argument(
"weights",
type=str,
help="A dictionary mapping words to their weights in string format.",
)
parser.add_argument(
"-k",
type=int,
default=5,
help="The length of shingles.",
)
args = parser.parse_args(argv[1:])
try:
print(json.loads(args.weights))
weighted_shingles = generate_weighted_shingles(args.text, json.loads(args.weights), args.k)
print("List of Shingles with corresponding weights: ", weighted_shingles)
except Exception as err:
print(err)
parser.print_help()
def generate_weighted_shingles(text, weights, k):
"""
Generate weighted shingles of length k from the given text.
Args:
text (str): The input text.
k (int): The length of shingles.
weights (dict): A dictionary mapping words to their weights.
Returns:
list: A list of weighted shingles.
"""
# Tokenize the text into words
words = re.findall(r'\w+', text.lower())
# Generate all shingles of length k
shingles = [' '.join(words[i:i + k]) for i in range(len(words) - k + 1)]
# Compute the weight of each shingle
weighted_shingles = []
for shingle in shingles:
shingle_words = shingle.split()
shingle_weight = sum(weights.get(word, 1) for word in shingle_words)
weighted_shingles.append((shingle, shingle_weight))
return weighted_shingles
if __name__ == "__main__":
import sys
main(sys.argv)