-
Notifications
You must be signed in to change notification settings - Fork 2
750 was were check #755
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
Open
baydakov-georgiy
wants to merge
2
commits into
master
Choose a base branch
from
750_was_were_check
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
750 was were check #755
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from ..base_check import BasePresCriterion, answer | ||
from app.nlp.is_passive_was_were_sentence import CritreriaType, generate_output_text, get_was_were_sentences | ||
|
||
class PresWasWereCheck(BasePresCriterion): | ||
label = 'Проверка на пассивные конструкции, начинающиеся с Был/Была/Было/Были, которые можно убрать без потери смысла' | ||
description = '' | ||
id = 'pres_was_were_check' | ||
|
||
def __init__(self, file_info, threshold=3): | ||
super().__init__(file_info) | ||
self.threshold = threshold | ||
|
||
def check(self): | ||
detected_sentences, total_sentences = get_was_were_sentences(self.file, CritreriaType.PRESENTATION) | ||
if total_sentences > self.threshold: | ||
result_str = generate_output_text(detected_sentences, CritreriaType.PRESENTATION) | ||
result_score = 0 | ||
else: | ||
result_str = 'Пройдена!' | ||
result_score = 1 | ||
return answer(result_score, result_str) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from ..base_check import BaseReportCriterion, answer | ||
from app.nlp.is_passive_was_were_sentence import CritreriaType, generate_output_text, get_was_were_sentences | ||
|
||
class ReportWasWereCheck(BaseReportCriterion): | ||
label = 'Проверка на пассивные конструкции, начинающиеся с Был/Была/Было/Были, которые можно убрать без потери смысла' | ||
description = '' | ||
id = 'report_was_were_check' | ||
|
||
def __init__(self, file_info, threshold=3): | ||
super().__init__(file_info) | ||
self.threshold = threshold | ||
|
||
def check(self): | ||
if self.file.page_counter() < 4: | ||
return answer(False, 'В отчёте недостаточно страниц. Нечего проверять.') | ||
detected, total_sentences = get_was_were_sentences(self.file, CritreriaType.REPORT) | ||
if total_sentences > self.threshold: | ||
result_str = generate_output_text(detected, CritreriaType.REPORT) | ||
result_score = 0 | ||
else: | ||
result_str = 'Пройдена!' | ||
result_score = 1 | ||
return answer(result_score, result_str) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import re | ||
import pymorphy2 | ||
import string | ||
from enum import Enum | ||
|
||
morph = pymorphy2.MorphAnalyzer() | ||
|
||
|
||
class CritreriaType(Enum): | ||
REPORT=0 | ||
PRESENTATION=1 | ||
|
||
|
||
def criteria_type_to_str(type: CritreriaType): | ||
if type == CritreriaType.REPORT: | ||
return "Страница" | ||
elif type == CritreriaType.PRESENTATION: | ||
return "Слайд" | ||
else: | ||
return "Элемент" | ||
|
||
def get_content_by_file(file, type: CritreriaType): | ||
if type == CritreriaType.REPORT: | ||
return file.pdf_file.get_text_on_page().items() | ||
elif type == CritreriaType.PRESENTATION: | ||
return enumerate(file.get_text_from_slides()) | ||
|
||
def clean_word(word): | ||
punct = string.punctuation.replace('-', '') | ||
return word.translate(str.maketrans('', '', punct)) | ||
|
||
|
||
def is_passive_was_were_sentece(sentence): | ||
""" | ||
Примеры плохих предложений (пассивные конструкции с "Был*" - можно убрать): | ||
- Был проведен анализ данных | ||
- Была выполнена работа по исследованию | ||
- Было принято решение о внедрении | ||
- Были получены следующие результаты | ||
- Была создана база данных | ||
|
||
Примеры хороших предложений ("Был*" нельзя убрать): | ||
- Было бы здорово получить новые данные | ||
- Был сильный скачок напряжения | ||
- Были времена, когда это казалось невозможным | ||
- Был студентом университета три года назад | ||
- Была программистом до выхода на пенсию | ||
""" | ||
first_words = re.split(r'\s+', sentence.strip(), maxsplit=2) | ||
if len(first_words) < 2: | ||
return False | ||
|
||
first_word = clean_word(first_words[0]) | ||
second_word = clean_word(first_words[1]) | ||
|
||
parsed = morph.parse(first_word)[0] | ||
if (parsed.normal_form == 'быть' and | ||
'past' in parsed.tag and | ||
parsed.tag.POS == 'VERB'): | ||
second_word_parsed = morph.parse(second_word)[0] | ||
return ('PRTS' in second_word_parsed.tag and | ||
'pssv' in second_word_parsed.tag) | ||
return False | ||
|
||
|
||
def generate_output_text(detected_senteces, type: CritreriaType): | ||
output = 'Обнаружены конструкции (Был/Была/Было/Были), которые можно удалить без потери смысла:<br><br>' | ||
for index, messages in detected_senteces.items(): | ||
output_type = criteria_type_to_str(type) | ||
output += f'{output_type} №{index + 1}: <br>' + '<br>'.join(messages) + '<br><br>' | ||
return output | ||
|
||
|
||
def get_was_were_sentences(file, type: CritreriaType): | ||
detected = {} | ||
total_sentences = 0 | ||
for page_index, page_text in get_content_by_file(file, type): | ||
sentences = re.split(r'(?<=[.!?…])\s+', page_text) | ||
for sentence_index, sentence in enumerate(sentences): | ||
if is_passive_was_were_sentece(sentence): | ||
total_sentences += 1 | ||
if page_index not in detected: | ||
detected[page_index] = [] | ||
truncated_sentence = sentence[:30] + '...' if len(sentence) > 30 else sentence | ||
detected[page_index].append(f'{sentence_index+1}: {truncated_sentence}') | ||
return detected, total_sentences |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Добавьте в данном файле в виде комментария примеры хороших и плохих предложений, которые начинаются с Был*