Spaces:
Build error
Build error
import logging | |
import re | |
import unicodedata | |
import dateparser | |
import dateparser.search as searcher | |
from nltk import word_tokenize | |
class TextUtils: | |
def __init__(self): | |
logging.debug('TextUtils sınıfı oluşturuldu') | |
def clean_spaces(text): | |
""" | |
Verilen metindeki fazla boşlukları temizler. | |
Args: | |
text (str): Girdi metni. | |
Returns: | |
str: Temizlenmiş metin. | |
""" | |
return " ".join(re.split(r"\s+", text.strip())) | |
def clean_format_str(text): | |
""" | |
Verilen metindeki unicode kontrol sembollerini, ascii olmayan karakterleri ve fazla boşlukları temizler. | |
Args: | |
text (str): Girdi metni. | |
Returns: | |
str: Temizlenmiş metin. | |
""" | |
text = "".join(ch for ch in text if unicodedata.category(ch)[0] != "C") | |
text = "".join([c if ord(c) < 128 else "" for c in text]) | |
text = " ".join(re.split(r"\s+", text.strip())) | |
# text = re.sub(r"\r\n", " ", text) | |
return text | |
def cosine(text1, text2): | |
""" | |
İki metin arasındaki kosinüs benzerliğini hesaplar. | |
Args: | |
text1 (str): İlk metin. | |
text2 (str): İkinci metin. | |
Returns: | |
float: Metinler arasındaki kosinüs benzerliği. | |
""" | |
X = text1.lower() | |
Y = text2.lower() | |
X_list = word_tokenize(X) | |
Y_list = word_tokenize(Y) | |
l1 = [] | |
l2 = [] | |
X_set = {w for w in X_list} | |
Y_set = {w for w in Y_list} | |
rvector = X_set.union(Y_set) | |
for w in rvector: | |
if w in X_set: | |
l1.append(1) | |
else: | |
l1.append(0) | |
if w in Y_set: | |
l2.append(1) | |
else: | |
l2.append(0) | |
c = 0 | |
for i in range(len(rvector)): | |
c += l1[i] * l2[i] | |
x = float((sum(l1) * sum(l2)) ** 0.5) | |
if x != 0: | |
sim = c / x | |
else: | |
sim = 0 | |
return sim | |
def parse_date_time(text): | |
""" | |
Verilen metinden tarih ve saat bilgisini çözümler. | |
Args: | |
text (str): Girdi metni. | |
Returns: | |
str: Çözümlenen tarih ve saat '%d.%m.%Y %H:%M:%S' formatında. | |
""" | |
result = None | |
try: | |
parsed = dateparser.parse(text, settings={'RETURN_AS_TIMEZONE_AWARE': False}) | |
result = parsed.strftime('%d.%m.%Y %H:%M:%S') | |
if result is None: | |
found = searcher.search_dates(text) | |
dl = [] | |
for date in found: | |
if date[0] and date[1]: | |
item = {"part": date[0], "value": date[1].strftime('%d.%m.%Y %H:%M:%S')} | |
dl.append(item) | |
result = dl[0]["value"] | |
except Exception as e: | |
logging.error(f"Bir hata oluştu. Metin: {text}, hata: {str(e)}") | |
return result | |
def text_space_normalizer(text): | |
""" | |
Verilen metindeki boşlukları düzenleyen bir yöntem. | |
Args: | |
text (str): Düzenlenecek metin. | |
Returns: | |
str: Boşlukları düzenlenmiş metin. | |
""" | |
regex = r"(?<=[.,?])(?=[^\s])" | |
subst = " " | |
text = re.sub(regex, subst, text, 0, re.MULTILINE) | |
regex = r"\s\s+" | |
subst = " " | |
text = re.sub(regex, subst, text, 0, re.MULTILINE) | |
regex = r"\s," | |
subst = "" | |
text = re.sub(regex, subst, text, 0, re.MULTILINE) | |
regex = r"\s\’" | |
subst = "" | |
text = re.sub(regex, subst, text, 0, re.MULTILINE) | |
return text | |