Skip to content

Commit

Permalink
Предварительная версия 0.9.9
Browse files Browse the repository at this point in the history
* Добавлен список ошибок
* Добавлены новые поля статистики
  • Loading branch information
D1midr0sh committed Nov 24, 2023
1 parent 5a48fc6 commit 77bda7a
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 4 deletions.
36 changes: 36 additions & 0 deletions designs/stats.ui
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,42 @@
<string>Назад</string>
</property>
</widget>
<widget class="QLabel" name="leastMistakes">
<property name="geometry">
<rect>
<x>10</x>
<y>150</y>
<width>851</width>
<height>141</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>20</pointsize>
</font>
</property>
<property name="text">
<string/>
</property>
</widget>
<widget class="QLabel" name="avgMistakes">
<property name="geometry">
<rect>
<x>10</x>
<y>310</y>
<width>851</width>
<height>71</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>20</pointsize>
</font>
</property>
<property name="text">
<string/>
</property>
</widget>
</widget>
<resources/>
<connections/>
Expand Down
19 changes: 18 additions & 1 deletion learn.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
from PyQt5 import QtCore, uic
from PyQt5.QtGui import QFont
from PyQt5.QtWidgets import QWidget
from ut import algorithm, divide, similar
from ut import algorithm, divide, find_mistakes, number_declination, similar


class Learn(QWidget):
def __init__(self):
super().__init__()
uic.loadUi("designs/learn.ui", self)
self.mistakes = 0
self.count = -1
self.m = None
self.con = sqlite3.connect("database.sqlite")
Expand Down Expand Up @@ -171,12 +172,28 @@ def check_correct(self):
self.next.setVisible(False)
self.again.setVisible(True)
text += "\nТы сделал много ошибок. Тебе придётся повторить попытку."
mistakes, count_mists, difference = find_mistakes(ch, to_check_with)
if count_mists > 0:
text += "\n Ты неправильно написал"
text += f" {count_mists} {number_declination(count_mists)}:"
text += "\n Правильно - неправильно"
for key in mistakes:
text += f"\n...{key}... - ..{mistakes[key]}..."
if difference > 0:
text += f"\nТы написал на {difference} {number_declination(difference)}"
text += " больше, чем нужно."
if difference < 0:
text += f"\nТы написал на {-difference} {number_declination(-difference)}"
text += " меньше, чем нужно."
self.mistakes += count_mists
self.result.setText(text)

def exit_to_main_menu(self):
req = "UPDATE poem SET wrong_ratio = "
req += f"{sum(self.ratios) / len(self.ratios)} WHERE id = {self.id}"
self.cur.execute(req)
req = f"UPDATE poem SET mistakes = {self.mistakes}"
self.cur.execute(req)
if self.m is None:
self.m = main.Main()
self.con.commit()
Expand Down
4 changes: 3 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ def proceed_to_learn(self):
if self.w is None:
self.w = learn.Learn()
self.hide()
self.w.setVariables(self.poemEdit.toPlainText().split("\n"), self.lines.value(), id)
self.w.setVariables(
self.poemEdit.toPlainText().split("\n"), self.lines.value(), id
)
self.w.show()
self.con.close()

Expand Down
17 changes: 15 additions & 2 deletions stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ class Stats(QWidget):
def __init__(self):
super().__init__()
uic.loadUi("designs/stats.ui", self)
self.backButton.clicked.connect(self.exit_to_main_menu)
self.con = sqlite3.connect("database.sqlite")
self.cur = self.con.cursor()
self.m = None
cur_id = self.cur.execute("""SELECT MAX(id) FROM poem""").fetchone()[0]
if cur_id is None:
self.lastPoem.setText("В базе нет ни одного стиха")
Expand All @@ -21,9 +23,20 @@ def __init__(self):
self.cur.execute("""SELECT AVG(wrong_ratio) FROM poem""")
avg = round(self.cur.fetchone()[0], 2)
self.avgRatio.setText(f"Средний коэффициент правильности за все стихи: {avg}")
self.backButton.clicked.connect(self.exit_to_main_menu)
req = "SELECT * FROM poem WHERE mistakes = (SELECT MIN(mistakes) FROM poem)"
self.cur.execute(req)
poem = self.cur.fetchone()
self.leastMistakes.setText(
f"Стих с наименьшим количеством ошибок ({poem[4]}): {poem[1]}, {poem[2]}"
)
req = "SELECT AVG(mistakes) FROM poem"
self.cur.execute(req)
avg = round(self.cur.fetchone()[0], 2)
self.avgMistakes.setText(f"Среднее количество ошибок за все стихи: {avg}")

def exit_to_main_menu(self):
self.con.close()
if self.m is None:
self.m = main.Main()
self.hide()
main.Main().show()
self.m.show()
37 changes: 37 additions & 0 deletions ut.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ def divide(lst: list, n: int):


def algorithm(verses: list) -> list:
"""
Generates a new list based on the input list of verses.
"""
ans = []
for i in range(1, len(verses) + 1):
verse = verses[i - 1]
Expand All @@ -34,3 +37,37 @@ def algorithm(verses: list) -> list:
to_check.extend(v)
ans.append(to_check)
return ans


def find_mistakes(new: str, original: str) -> (dict, int, int):
"""
Calculate the differences between two strings and find the mistakes.
"""
first = new.split()
second = original.split()
dif = len(second) - len(first)
mistakes = 0
right_wrong = {}
for i in range(min(len(first), len(second))):
if first[i] != second[i]:
if i == 0:
right_wrong[" ".join(first[:2])] = " ".join(second[:2])
elif i == min(len(first), len(second)):
right_wrong[" ".join(first[i - 1 :])] = " ".join(second[i - 1 :])
else:
right_wrong[" ".join(first[i - 1 : i + 2])] = " ".join(
second[i - 1 : i + 2]
)
mistakes += 1
return right_wrong, mistakes, dif


def number_declination(num: int) -> str:
"""
Returns the appropriate declination of the word "слово" based on the given number.
"""
if num % 10 == 1 and num % 100 != 11:
return "слово"
if 2 <= num % 10 <= 4:
return "слова"
return "слов"

0 comments on commit 77bda7a

Please sign in to comment.