-
Notifications
You must be signed in to change notification settings - Fork 2
751_decimal_places_limit #758
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import re | ||
| from ..base_check import BasePresCriterion, answer | ||
| from utils.decimal_checker import DecimalPlacesChecker, DocumentType | ||
|
|
||
|
|
||
| class PresExcessiveDecimalPlacesCheck(BasePresCriterion): | ||
| description = 'Проверка на избыточное количество десятичных знаков в числах' | ||
| label = "Проверка на избыточное количество десятичных знаков" | ||
| id = 'pres_excessive_decimal_places_check' | ||
|
|
||
| def __init__(self, file_info, max_decimal_places=2, max_number_of_violations=1): | ||
| super().__init__(file_info) | ||
| self.max_decimal_places = max_decimal_places | ||
| self.max_number_of_violations = max_number_of_violations | ||
| self.checker = DecimalPlacesChecker(max_decimal_places, DocumentType.PRESENTATION) | ||
|
|
||
| def check(self): | ||
|
|
||
| detected_slides = {} | ||
| total_violations = 0 | ||
|
|
||
| for slide_num, slide_text in enumerate(self.file.get_text_from_slides()): | ||
| lines = re.split(r'\n', slide_text) | ||
|
|
||
| slide_violations = self.checker.find_violations_in_lines(lines) | ||
|
|
||
| for line_idx, number_str, decimal_places, line in slide_violations: | ||
| total_violations += 1 | ||
|
|
||
| if slide_num not in detected_slides: | ||
| detected_slides[slide_num] = [] | ||
|
|
||
| violation_msg = self.checker.format_violation_message( | ||
| line_idx, line, number_str, decimal_places | ||
| ) | ||
| detected_slides[slide_num].append(violation_msg) | ||
|
|
||
| if total_violations > self.max_number_of_violations: | ||
| result_str = self.checker.format_failure_message( | ||
| total_violations, | ||
| detected_slides, | ||
| format_page_link_fn=self.format_page_link | ||
| ) | ||
| result_score = 0 | ||
| else: | ||
| result_str = self.checker.format_success_message() | ||
| result_score = 1 | ||
|
|
||
| return answer(result_score, result_str) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import re | ||
| from ..base_check import BaseReportCriterion, answer | ||
| from utils.decimal_checker import DecimalPlacesChecker | ||
|
|
||
|
|
||
| class ReportExcessiveDecimalPlacesCheck(BaseReportCriterion): | ||
| label = "Проверка на избыточное количество десятичных знаков" | ||
| description = 'Проверка на избыточное количество десятичных знаков в числах' | ||
| id = 'excessive_decimal_places_check' | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Придерживайтесь одного стандартного формата для id критериев (как в критерии для презентаций) |
||
|
|
||
| def __init__(self, file_info, max_decimal_places=2, max_number_of_violations=2): | ||
| super().__init__(file_info) | ||
| self.max_decimal_places = max_decimal_places | ||
| self.max_number_of_violations = max_number_of_violations | ||
| self.checker = DecimalPlacesChecker(max_decimal_places) | ||
|
|
||
| def check(self): | ||
| if self.file.page_counter() < 4: | ||
| return answer(False, "В отчете недостаточно страниц. Нечего проверять.") | ||
|
|
||
| detected_pages = {} | ||
| total_violations = 0 | ||
|
|
||
| for page_num, page_text in self.file.pdf_file.get_text_on_page().items(): | ||
| lines = re.split(r'\n', page_text) | ||
|
|
||
| page_violations = self.checker.find_violations_in_lines(lines) | ||
|
|
||
| for line_idx, number_str, decimal_places, line in page_violations: | ||
| total_violations += 1 | ||
|
|
||
| if page_num not in detected_pages: | ||
| detected_pages[page_num] = [] | ||
|
|
||
| violation_msg = self.checker.format_violation_message( | ||
| line_idx, line, number_str, decimal_places | ||
| ) | ||
| detected_pages[page_num].append(violation_msg) | ||
|
|
||
| if total_violations > self.max_number_of_violations: | ||
| result_str = self.checker.format_failure_message( | ||
| total_violations, | ||
| detected_pages, | ||
| format_page_link_fn=self.format_page_link | ||
| ) | ||
| result_score = 0 | ||
| else: | ||
| result_str = self.checker.format_success_message() | ||
| result_score = 1.0 | ||
|
Comment on lines
+21
to
+49
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. С учетом, что эта логика 1 в 1 повторяется в обоих критериях - кажется её можно вынести в одну функцию / метод +- как вы сделали в https://github.com/moevm/document_insight_system/pull/755/files |
||
|
|
||
| return answer(result_score, result_str) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| import re | ||
| from enum import Enum | ||
|
|
||
| class DocumentType(Enum): | ||
| REPORT = 'report' | ||
| PRESENTATION = 'pres' | ||
|
|
||
| class DecimalPlacesChecker: | ||
| """ | ||
| Класс для проверки чисел на избыточное количество десятичных знаков. | ||
| Игнорирует IP-адреса, версии ПО и другие составные числа. | ||
|
|
||
| Проверяет: | ||
| - Обычные числа с десятичными знаками (например, -3.14159) | ||
| Не проверяет: | ||
| - IP-адреса (например, 192.168.1.1) | ||
| - Версии ПО (например, 1.2.3.4) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Если версия выглядит как дробное число, то засечет (см. скрин проверки у критерия презентаций), но думаю это слишком частный случай - его можно будет учесть в пороге для критериев |
||
| - Другие составные числа | ||
| """ | ||
|
|
||
| DECIMAL_PATTERN = r'(?<!\d)(?<!\.)-?\d+[.,]\d+(?!\d)' | ||
|
|
||
| def __init__(self, max_decimal_places=2, type=DocumentType.REPORT): | ||
| self.max_decimal_places = max_decimal_places | ||
| self.type = type | ||
|
|
||
| def is_valid_number(self, match, text): | ||
| start_pos = match.start() | ||
| end_pos = match.end() | ||
|
|
||
| char_before = text[start_pos - 1] if start_pos > 0 else ' ' | ||
|
|
||
| if char_before == '.': | ||
| return False | ||
|
|
||
| if end_pos < len(text): | ||
| char_after = text[end_pos] | ||
| if char_after == '.' and end_pos + 1 < len(text) and text[end_pos + 1].isdigit(): | ||
| return False | ||
|
|
||
| return True | ||
|
|
||
| def count_decimal_places(self, number_str): | ||
| normalized = number_str.replace(',', '.') | ||
|
|
||
| if '.' in normalized: | ||
| decimal_part = normalized.split('.')[1] | ||
| return len(decimal_part) | ||
|
|
||
| return 0 | ||
|
|
||
| def has_excessive_decimals(self, number_str): | ||
| return self.count_decimal_places(number_str) > self.max_decimal_places | ||
|
|
||
| def find_violations_in_text(self, text): | ||
| violations = [] | ||
| matches = re.finditer(self.DECIMAL_PATTERN, text) | ||
|
|
||
| for match in matches: | ||
| if not self.is_valid_number(match, text): | ||
| continue | ||
|
|
||
| number_str = match.group() | ||
| decimal_places = self.count_decimal_places(number_str) | ||
|
|
||
| if decimal_places > self.max_decimal_places: | ||
| violations.append((number_str, decimal_places, match)) | ||
|
|
||
| return violations | ||
|
|
||
| def find_violations_in_lines(self, lines): | ||
| violations = [] | ||
|
|
||
| for line_idx, line in enumerate(lines): | ||
| line_violations = self.find_violations_in_text(line) | ||
|
|
||
| for number_str, decimal_places, match in line_violations: | ||
| violations.append((line_idx, number_str, decimal_places, line)) | ||
|
|
||
| return violations | ||
|
|
||
| def highlight_number(self, line, number_str): | ||
| return line.replace(number_str, f'<b>{number_str}</b>', 1) | ||
|
|
||
| def format_violation_message(self, line_idx, line, number_str, decimal_places): | ||
| highlighted_line = self.highlight_number(line, number_str) | ||
| return ( | ||
| f'Строка {line_idx + 1}: {highlighted_line} ' | ||
| f'<i>(найдено {decimal_places} знаков после запятой, ' | ||
| f'максимум: {self.max_decimal_places})</i>' | ||
| ) | ||
|
|
||
| def format_success_message(self): | ||
| if self.type == DocumentType.REPORT: | ||
| document_type = "документе" | ||
| elif self.type == DocumentType.PRESENTATION: | ||
| document_type = "презентации" | ||
| return ( | ||
| f'Проверка пройдена! Все числа в {document_type} имеют допустимое количество ' | ||
| f'десятичных знаков (не более {self.max_decimal_places}).' | ||
| ) | ||
|
|
||
| def format_failure_message(self, total_violations, | ||
| violations_by_location, | ||
| format_page_link_fn=None): | ||
| if self.type == DocumentType.REPORT: | ||
| location_label = "Страница" | ||
| elif self.type == DocumentType.PRESENTATION: | ||
| location_label = "Слайд" | ||
|
Comment on lines
+93
to
+109
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Если вся "возня" с enum и if/else только для красивого вывода - это явно лишняя логика, без которой можно спокойно жить, поэтому уберите |
||
| result_str = ( | ||
| f'<b>Найдены числа с избыточным количеством десятичных знаков!</b><br>' | ||
| f'Максимально допустимое количество знаков после запятой: {self.max_decimal_places}<br>' | ||
| f'Всего нарушений: {total_violations}<br><br>' | ||
| ) | ||
|
|
||
| for location_num, violations in violations_by_location.items(): | ||
| if format_page_link_fn: | ||
| location_str = f'{location_label} {format_page_link_fn([location_num])}' | ||
| else: | ||
| location_str = f'{location_label} №{location_num}' | ||
|
|
||
| result_str += f'<b>{location_str}:</b><br>' | ||
| result_str += '<br>'.join(violations) | ||
| result_str += '<br><br>' | ||
|
|
||
| return result_str | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.