Как переделать что бы окончаний не удалялись типа "АЛГОР", а брались в ковычках АЛГОР+"РИТМ"
Как переделать что бы окончаний не удалялись типа "АЛГОР", а брались в ковычках АЛГОР+"РИТМ"
def splitting_by_words(text):
result = re.findall(r'\w+', text)
return result
def sorting_affixes(file_name):
affixes_wb = xlrd.open_workbook(affixes_file_name) #Открывает файл в формете XLS
affixes_sh = affixes_wb.sheet_by_index(0) #Возвращает лист книги по индексу, экземпляр класса
affixes = [] #Создание строки
for rownum in range(affixes_sh.nrows-1): #Число, количество строк nrows-1
affix = affixes_sh.cell(rownum+1,0).value #Возвращает экземпляр объекта “Ячейка”
if '\ufeff' in affix:
affix = affix.replace('\ufeff', '') #Замена слов!
affixes.append(affix) #Добавление элементов в строку
sorted_affixes = sorted(affixes, key=len, reverse=True) #отсортированный список
return sorted_affixes
def stem(word, affixes):
word_len = len(word) #Рассчет колличество
min_len_of_word = 2
stems = []
if word_len > min_len_of_word:
n = word_len - min_len_of_word
for i in range(n+1, 0, -1):
word_affix = word[word_len - (i-1):]
stem = word[:word_len-len(word_affix)]
for affix in affixes:
if affix == word_affix:
stems.append(stem)
elif affix == '' or word_affix == '':
stems.append(word)
else:
stems.append(word)
return stems[0]
def stemming(file_name, affixes):
text_file = open(file_name, 'r', encoding="utf-8")
text_file = text_file.read()
text = splitting_by_words(text_file)
res_text = []
for word in text:
if word not in res_text:
res_text.append(word)
stem_text = {}
for word in res_text:
stemm = stem(word, affixes)
stem_text.update({word: stemm})
for i in stem_text.keys():
word = str(i)
stemm = str(stem_text[i])
if word in text_file:
text_file = re.sub((rf"\b{word}\b"), word, text_file)
#Как тут надо переделать?
return text_file