Skip to content

Commit

Permalink
feat(main): implement paragraph choosing
Browse files Browse the repository at this point in the history
  • Loading branch information
Iamhexi committed Oct 4, 2024
1 parent e0398b8 commit c019b13
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
25 changes: 23 additions & 2 deletions knowledge_verificator/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from knowledge_verificator.materials import MaterialDatabase
from knowledge_verificator.nli import NaturalLanguageInference, Relation
from knowledge_verificator.qg import QuestionGeneration
from knowledge_verificator.utils.string import clip_text


if __name__ == '__main__':
Expand Down Expand Up @@ -61,8 +62,28 @@
continue

material = material_db.materials[chosen_index]
# TODO: A user should be able to choose a paragraph.
paragraph = material.paragraphs[0]

MAX_LENGTH_OF_CLIPPED_PARAGRAPH = 15
console.print('Available paragraphs: ')
for i, paragraph in enumerate(material.paragraphs):
console.print(
f'{i+1} {clip_text(paragraph, MAX_LENGTH_OF_CLIPPED_PARAGRAPH)}'
)

console.print('Which paragraph you want to learn? ')
paragraph_choice = input()

if not paragraph_choice.isnumeric():
console.print(INCORRECT_CHOICE_WARNING)
continue

chosen_index = int(paragraph_choice) - 1
if chosen_index < 0 or chosen_index >= len(material.paragraphs):
console.print(INCORRECT_CHOICE_WARNING)
continue

# TODO: Refactor this fragment to apply DRY principle.
paragraph = material.paragraphs[chosen_index]

console.print('Learn this paragraph: ')
console.print(paragraph)
Expand Down
26 changes: 26 additions & 0 deletions knowledge_verificator/utils/string.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""Module with string-related utility function."""


def clip_text(text: str, max_length: int) -> str:
"""
Clip `text` if its length exceeds `max_length`.
If the text was clipped, it has three dots `...` at the end. Otherwise,
the text is returned unchanged.
Args:
text (str): Text to clip.
max_length (int): Maximum allowed length. It will not be exceeded.
Returns:
str: Clipped text.
"""
if max_length <= 4:
raise ValueError(
'Minimal reasonable value of `max_length` is 4 (one character '
f'and three dots). Supplied value of {max_length}.'
)
text_length = len(text)
if text_length > max_length:
return text[: max_length - 3] + '...'
return text

0 comments on commit c019b13

Please sign in to comment.