-
Notifications
You must be signed in to change notification settings - Fork 0
/
features.py
69 lines (50 loc) · 1.26 KB
/
features.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
68
69
# coding: utf-8
import re
def contains_capital(word):
"""
Check whether a word contains a capital letter
:type word: str
"""
if word.lower() == word:
return False
return True
def contains_digit(word):
for digit in '0123456789': # smarter way?
if digit in word:
return True
return False
def contains_hyphen(word):
for hyphen in '―–‒-—': # added other hyphens; probably, some of them are dashes
if hyphen in word:
return True
return False
def prefix(word, n):
"""
Return an n-prefix of a word
"""
return word.lower()[:n]
def suffix(word, n):
"""
Return an n-suffix of a word
"""
return word.lower()[-n:]
def shape1(word):
transformed = ''
for char in word:
if char.islower():
transformed += 'x'
elif char.isupper():
transformed += 'X'
else:
transformed += char # this covers both 0's and punctuation
return transformed
def shape2(word):
s = shape1(word)
s = re.sub('X+', 'X', s) # performance hit? # why?
s = re.sub('x+', 'x', s)
s = re.sub('0+', '0', s)
return s
def full_word(word):
return word
def full_word_lower(word):
return word.lower()