This repository has been archived on 2022-02-08. You can view files and clone it, but cannot push or open issues or pull requests.
su_task3-nlp/da_detector.py

49 lines
998 B
Python
Raw Normal View History

2021-12-16 02:12:42 +01:00
"""
this file holds a class, which implements a "detector" - language classifier -
that learns from predefined, frequency-analysed sets of ngrams
"""
class da_detector:
def __init__(self, langs_to_check=["sk"]):
2021-12-16 02:12:42 +01:00
# langs to check
# to be picked from ["cz", "sk", "de", "en", "fr"]
self.langs_to_check = langs_to_check
2021-12-16 02:12:42 +01:00
def rm_interpunction(data):
2021-12-16 03:13:46 +01:00
from string import punctuation
2021-12-16 02:12:42 +01:00
for ngram in data:
try:
2021-12-16 03:13:46 +01:00
ngram = ngram.translate(str.maketrans('', '', punctuation))
2021-12-16 02:12:42 +01:00
except Exception as e:
raise e
2021-12-16 03:14:07 +01:00
return data
2021-12-16 02:12:42 +01:00
def rm_digits(data):
from string import digits
try:
for ngram in data:
2021-12-16 02:12:42 +01:00
ngram = ngram.translate(None, digits)
except Exception as e:
raise e
2021-12-16 03:14:07 +01:00
return data
2021-12-16 04:34:24 +01:00
def parse_freqs(self, path):
import json
fullpath = freqs_folder + path
try:
with open(fullpath, 'r') as f:
j_data = f.read()
except Exception as e:
raise e
try:
obj = json.loads(j_data)
except Exception as e:
raise e
return obj
2021-12-16 04:35:13 +01:00
# vim: ff=unix noexpandtab