-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
111 lines (97 loc) · 3.43 KB
/
train.py
File metadata and controls
111 lines (97 loc) · 3.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
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import argparse
import glob
import io
import os
import re
import unicodedata
from typing import Self
class Trainer:
ALLOWED_CHARS = "abcdefghijklmnopqrstuvwxyz"
IGNORED_CHARS = re.compile("[^a-z ]")
@staticmethod
def clean_string(s: str) -> str:
s = s.lower()
s = "".join(
c for c in unicodedata.normalize("NFKD", s)
if unicodedata.category(c) != "Mn")
s = re.sub("æ", "ae", s)
s = re.sub("œ", "oe", s)
s = Trainer.IGNORED_CHARS.sub(" ", s)
s = re.sub(r" +", " ", s)
return s
@staticmethod
def expand_input_paths(*paths: str) -> list[str]:
output = []
for path in paths:
if os.path.isfile(path):
output.append(path)
elif os.path.isdir(path):
for filename in next(os.walk(path))[2]:
output.append(os.path.join(path, filename))
else:
for fpath in glob.glob(path):
if os.path.isfile(fpath):
output.append(fpath)
return output
def __init__(self, max_token_length: int=4) -> None:
self._max_token_length: int = max_token_length
self._tokens: dict[str, dict[str, int]] = {}
def _feed_word(self, word: str) -> None:
word = f"^{word}$"
for i in range(len(word) - 1):
for j in range(self._max_token_length):
if i + j + 1>= len(word):
continue
token = word[i:i+j+1]
char = word[i + j + 1]
self._tokens.setdefault(token, {})
self._tokens[token].setdefault(char, 0)
self._tokens[token][char] += 1
def _feed(self, fs: io.TextIOWrapper, buffer_size: int=4096) -> None:
prefix = ""
while True:
buffer = fs.read(buffer_size)
if not buffer:
break
split = (prefix + self.clean_string(buffer)).split(" ")
prefix = split[-1]
for word in filter(lambda s: s, split[:-1]):
self._feed_word(word)
def train(self, *paths: str) -> Self:
for path in self.expand_input_paths(*paths):
print("Feeding text at", path)
with open(path, "r", encoding="utf8") as file:
self._feed(file)
print("Model contains", len(self._tokens), "tokens")
return self
def to_tsv(self, path: str) -> Self:
print("Saving to", path)
with open(path, "w") as file:
file.write("\t" + "\t".join(Trainer.ALLOWED_CHARS + "$") + "\n")
for token in self._tokens:
file.write(f"{token}")
for c in Trainer.ALLOWED_CHARS + "$":
file.write(f"\t{self._tokens[token].get(c, '')}")
file.write("\n")
return self
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"paths",
type=str,
nargs="+",
help="paths to input text files")
parser.add_argument(
"-o", "--output-path",
type=str,
default="data/tokens.tsv",
help="path to output TSV")
parser.add_argument(
"-m", "--max-token-length",
type=int,
default=4,
help="maximum token length")
args = parser.parse_args()
Trainer(args.max_token_length).train(*args.paths).to_tsv(args.output_path)
if __name__ == "__main__":
main()