MCPcopy
hub / github.com/kyclark/tiny_python_projects / stemmer

Function stemmer

14_rhymer/solution1_regex.py:40–61  ·  view source on GitHub ↗

Return leading consonants (if any), and 'stem' of word

(word)

Source from the content-addressed store, hash-verified

38
39# --------------------------------------------------
40def stemmer(word):
41 """Return leading consonants (if any), and 'stem' of word"""
42
43 word = word.lower()
44 vowels = 'aeiou'
45 consonants = ''.join(
46 [c for c in string.ascii_lowercase if c not in vowels])
47 pattern = (
48 '([' + consonants + ']+)?' # capture one or more, optional
49 '([' + vowels + '])' # capture at least one vowel
50 '(.*)' # capture zero or more of anything
51 )
52 pattern = f'([{consonants}]+)?([{vowels}])(.*)'
53
54 match = re.match(pattern, word)
55 if match:
56 p1 = match.group(1) or ''
57 p2 = match.group(2) or ''
58 p3 = match.group(3) or ''
59 return (p1, p2 + p3)
60 else:
61 return (word, '')
62
63
64# --------------------------------------------------

Callers 2

mainFunction · 0.70
test_stemmerFunction · 0.70

Calls

no outgoing calls

Tested by 1

test_stemmerFunction · 0.56