import json
import re
import string
from collections import Counter
import os
LANGUAGES = {
"sah": "./datasets/downloads/sah/"
}
output_dir = "./datasets/generated"
files_suffix = "_generated"
def to_unicode(text, encoding='utf8', errors='strict'):
if isinstance(text, str):
return text
return str(text, encoding, errors=errors)
RE_PUNCT = re.compile('([%s])+' % re.escape(string.punctuation), re.UNICODE)
def strip_punctuation(s):
return RE_PUNCT.sub(" ", s)
RE_NUMERIC = re.compile(r"[0-9]+", re.UNICODE)
def strip_numeric(s):
return RE_NUMERIC.sub("", s)
def clean_text(text):
cleaning_functions = [to_unicode, lambda x: x.lower(), strip_punctuation, strip_numeric]
for f in cleaning_functions:
text = f(text)
return text
def add_padding(text):
padding = " " * (3 - 1)
return " " + text + padding
def create_ngrams(text):
text = add_padding(text)
for length in range(1, 4):
for i in range(len(text) - length + 1):
yield text[i:i + length]
def create_langdetect_profile(text: str, lang_code: str) -> dict:
text = clean_text(text)
ngram_freq = {}
for ngram in create_ngrams(text):
if ngram in ngram_freq:
ngram_freq[ngram] += 1
else:
ngram_freq[ngram] = 1
uni_freq = {k: v for k, v in ngram_freq.items() if len(k.strip()) == 1}
bi_freq = {k: v for k, v in ngram_freq.items() if len(k.strip()) == 2}
tri_freq = {k: v for k, v in ngram_freq.items() if len(k.strip()) == 3}
uni_top = Counter(uni_freq).most_common(300)
bi_top = Counter(bi_freq).most_common(300)
tri_top = Counter(tri_freq).most_common(300)
combined_freq = {}
for gram, count in uni_top + bi_top + tri_top:
combined_freq[gram] = count
n_words = [
sum(uni_freq.values()),
sum(bi_freq.values()),
sum(tri_freq.values())
]
profile = {
"freq": combined_freq,
"n_words": n_words,
"name": lang_code.lower()
}
return profile
if __name__ == "__main__":
os.makedirs(output_dir, exist_ok=True)
for lang_code, path in LANGUAGES.items():
text = ""
for filename in os.listdir(path):
with open(os.path.join(path, filename), "r", encoding="utf-8") as f:
text += f.read() + "\n"
profile = create_langdetect_profile(text, lang_code)
output_file = f"{output_dir}/{lang_code}{files_suffix}.json"
with open(output_file, "w", encoding="utf-8") as f:
json.dump(profile, f, ensure_ascii=False, indent=2)
print(f"Generated profile for {lang_code} and saved to {output_file}")