forked from justinsloan/wordflash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphonics_lexer.py
More file actions
54 lines (46 loc) · 1.66 KB
/
phonics_lexer.py
File metadata and controls
54 lines (46 loc) · 1.66 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
49
50
51
52
53
54
import re
class PhonicsLexer(object):
"""Lexical Analyzer for parsing phonics and blend word combinations."""
def __init__(self, source_words):
"""Takes a list as source_words"""
self.source_words = source_words
def tokenize(self):
"""Returns a dict with the phonics token and count."""
source_index = 0
tokens = {}
source_words = self.source_words
phonics = self._phonics_tokens()
while source_index < len(source_words):
word = source_words[source_index]
for sound in phonics:
# Check to see if the index (sound) is a regex
if sound[0] == "[":
if re.search(r"" + sound, f"{word}."):
#print(f"{sound} MATCHED {word}")
if sound in tokens:
tokens[sound] += 1
else:
tokens[sound] = 1
elif sound in word:
if sound in tokens:
tokens[sound] += 1
else:
tokens[sound] = 1
source_index += 1
return tokens
def _phonics_tokens(self):
"""Returns a list of phonics sounds and regular expressions"""
# '[aeiou][^vr]e(?=[^r])' matches 'Sneaky E' words
tokens = ["are",
"au",
"ea",
"ee",
"igh",
"ny",
"ou",
"oo",
"ore",
"ure",
"[aeiou][^vr]e(?=[^r])"
]
return tokens