11import re
22import pymorphy2
33import string
4+ from enum import Enum
45
56morph = pymorphy2 .MorphAnalyzer ()
67
8+
9+ class CritreriaType (Enum ):
10+ REPORT = 0
11+ PRESENTATION = 1
12+
13+
14+ def criteria_type_to_str (type : CritreriaType ):
15+ if type == CritreriaType .REPORT :
16+ return "Страница"
17+ elif type == CritreriaType .PRESENTATION :
18+ return "Слайд"
19+ else :
20+ return "Элемент"
21+
22+ def get_content_by_file (file , type : CritreriaType ):
23+ if type == CritreriaType .REPORT :
24+ return file .pdf_file .get_text_on_page ().items ()
25+ elif type == CritreriaType .PRESENTATION :
26+ return enumerate (file .get_text_from_slides ())
27+
728def clean_word (word ):
829 punct = string .punctuation .replace ('-' , '' )
930 return word .translate (str .maketrans ('' , '' , punct ))
1031
32+
1133def is_passive_was_were_sentece (sentence ):
34+ """
35+ Примеры плохих предложений (пассивные конструкции с "Был*" - можно убрать):
36+ - Был проведен анализ данных
37+ - Была выполнена работа по исследованию
38+ - Было принято решение о внедрении
39+ - Были получены следующие результаты
40+ - Была создана база данных
41+
42+ Примеры хороших предложений ("Был*" нельзя убрать):
43+ - Было бы здорово получить новые данные
44+ - Был сильный скачок напряжения
45+ - Были времена, когда это казалось невозможным
46+ - Был студентом университета три года назад
47+ - Была программистом до выхода на пенсию
48+ """
1249 first_words = re .split (r'\s+' , sentence .strip (), maxsplit = 2 )
1350 if len (first_words ) < 2 :
1451 return False
@@ -23,4 +60,27 @@ def is_passive_was_were_sentece(sentence):
2360 second_word_parsed = morph .parse (second_word )[0 ]
2461 return ('PRTS' in second_word_parsed .tag and
2562 'pssv' in second_word_parsed .tag )
26- return False
63+ return False
64+
65+
66+ def generate_output_text (detected_senteces , type : CritreriaType ):
67+ output = 'Обнаружены конструкции (Был/Была/Было/Были), которые можно удалить без потери смысла:<br><br>'
68+ for index , messages in detected_senteces .items ():
69+ output_type = criteria_type_to_str (type )
70+ output += f'{ output_type } №{ index + 1 } : <br>' + '<br>' .join (messages ) + '<br><br>'
71+ return output
72+
73+
74+ def get_was_were_sentences (file , type : CritreriaType ):
75+ detected = {}
76+ total_sentences = 0
77+ for page_index , page_text in get_content_by_file (file , type ):
78+ sentences = re .split (r'(?<=[.!?…])\s+' , page_text )
79+ for sentence_index , sentence in enumerate (sentences ):
80+ if is_passive_was_were_sentece (sentence ):
81+ total_sentences += 1
82+ if page_index not in detected :
83+ detected [page_index ] = []
84+ truncated_sentence = sentence [:30 ] + '...' if len (sentence ) > 30 else sentence
85+ detected [page_index ].append (f'{ sentence_index + 1 } : { truncated_sentence } ' )
86+ return detected , total_sentences
0 commit comments