Repository navigation
Expand file tree
/
Copy pathutil.py
More file actions
74 lines (55 loc) · 1.8 KB
/
Copy pathutil.py
File metadata and controls
74 lines (55 loc) · 1.8 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import os.path as path
import colorama
from functools import lru_cache
colorama.init(autoreset=False)
SUBSTRING_DB_FILE = "scraped/substring_data.db"
WORD_DB_FILE = "scraped/word_data.db"
PHONETIC_DB_FILE = "scraped/phonetic_data.db"
class Words:
def __init__(self, source: str):
self.path = path.join(source, "words.txt")
self._len = sum(1 for _ in open(self.path, "r", encoding="utf8"))
def __len__(self):
return self._len
def __iter__(self):
with open(self.path, "r", encoding="utf8") as words_file:
for line in words_file:
match line.split(" "):
case [word, freq_count]:
yield word, int(freq_count)
case _:
yield line, 1
@lru_cache(maxsize=50000)
def find_all_substrings(text, sub):
"""Yields indexes of all occurrences of sub in text"""
start = 0
while True:
start = text.find(sub, start)
if start == -1: return
yield start
start += len(sub)
def segment_ipa(ipa_txt):
segments = []
i = 0
while i < len(ipa_txt):
if i + 1 < len(ipa_txt) and ipa_txt[i + 1] in {"ː", "ˑ"}:
segments.append(ipa_txt[i] + ipa_txt[i + 1])
i += 2
else:
segments.append(ipa_txt[i])
i += 1
return tuple(segments)
@lru_cache(maxsize=100000)
def get_substrings(word):
result = set()
for i in range(len(word)):
for j in range(i + 1, len(word) + 1):
result.add(word[i:j])
return result
@lru_cache(maxsize=100000)
def get_ipa_substrings(segmented_ipa):
result = set()
for i in range(len(segmented_ipa)):
for j in range(i + 1, len(segmented_ipa) + 1):
result.add("".join(segmented_ipa[i:j]))
return result