-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshingle.py
More file actions
48 lines (34 loc) · 1.43 KB
/
Copy pathshingle.py
File metadata and controls
48 lines (34 loc) · 1.43 KB
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
# Assign length of the shingles
length = 2
def shingle_letters(string):
'''Function to divide a string (text) into shingles.
The string is divided (tokenized) into smaller units called shingles of
character length k (default length = 2). These partially overlaps
to one another. The last characters (shingle length -1) are ignored.
string = 'the fox jumps' --> shingle_letters(string) --> ['th', 'he', 'e ', ' f', 'fo', 'ox', 'x ', ' j', 'ju', 'um', 'mp', 'ps']
Parameters:
string : a text
Returns:
list: shingles of character length k
'''
shingle = [string[i:i+length] for i in range(len(string)-(length-1))]
return shingle
def shingle_words(string):
'''Function to divide a string (text) into shingles.
The string is divided (tokenized) into smaller units called shingles of
word length k (default length = 2). These partially overlaps
to one another. The last word (shingle length -1) are ignored.
string = 'a brown fox jumps high' --> shingle_words(string) --> ['a brown', 'brown fox', 'fox jumps', 'jumps high']
Parameters:
string : a text
Returns:
list: shingles of word length k
'''
# Split a string into words using white spaces
shingle = string.split()
shingles = [' '.join(shingle[i:i+length]) for i in range(len(shingle) - (length - 1))]
return shingles
select= {
'letters': shingle_letters,
'words': shingle_words
}