fix: add check line and rating, find_longest_review returns first longest rewiew
This commit is contained in:
parent
bb5b9ac705
commit
be986cbc17
25
reviews.py
25
reviews.py
@ -10,11 +10,24 @@ def load_reviews(filepath: str) -> list:
|
||||
with open(filepath, "r", encoding="utf-8") as file:
|
||||
for line in file:
|
||||
line = line.strip()
|
||||
if not line:
|
||||
continue
|
||||
parts = line.split("|")
|
||||
if len(parts) != 3:
|
||||
print("Пропущена строка с неверным форматом:", line)
|
||||
continue
|
||||
username, rating, text = parts
|
||||
try:
|
||||
rating_int = int(rating.strip())
|
||||
except ValueError:
|
||||
print("Неверный рейтинг:", line)
|
||||
continue
|
||||
if not (1 <= rating_int <= 5):
|
||||
print("Рейтинг вне диапазона [1–5]:", line)
|
||||
continue
|
||||
reviews.append({
|
||||
"username": username.strip(),
|
||||
"rating": int(rating),
|
||||
"rating": rating_int,
|
||||
"text": text.strip()
|
||||
})
|
||||
return reviews
|
||||
@ -47,8 +60,14 @@ def count_words_in_review(review: dict) -> int:
|
||||
def find_longest_review(reviews: list) -> dict:
|
||||
if not reviews:
|
||||
return {}
|
||||
|
||||
return max(reviews, key=count_words_in_review)
|
||||
longest = reviews[0]
|
||||
max_words = count_words_in_review(longest)
|
||||
for rev in reviews[1:]:
|
||||
words = count_words_in_review(rev)
|
||||
if words > max_words:
|
||||
max_words = words
|
||||
longest = rev
|
||||
return longest
|
||||
|
||||
|
||||
def build_word_frequency(reviews: list) -> dict:
|
||||
|
||||
Loading…
Reference in New Issue
Block a user