new truncate_title logic

This commit is contained in:
vvzvlad
2025-04-17 19:43:10 +03:00
parent bf1477ab95
commit ecaba7e8bb
2 changed files with 15 additions and 3 deletions
+8 -1
View File
@@ -142,7 +142,14 @@ class PostParser:
return "Unknown author"
def truncate_title(self, first_line: str) -> str:
"""Truncate the title according to the rules from _generate_title."""
"""Truncate the title """
# Step 1: Cut at the first period followed by a space, if present
period_match = re.search(r'\.(?=\s)', first_line)
if period_match:
first_line = first_line[:period_match.start()]
first_line = first_line.rstrip() # Remove trailing spaces after cut
# Step 2: Apply the old logic
cut_at = 37
max_extra_chars = 15
limit_index = cut_at + max_extra_chars # 52
+7 -2
View File
@@ -377,7 +377,6 @@ class TestPostParserGenerateTitle(unittest.TestCase):
title = self.parser._generate_title(message)
self.assertEqual(title, expected_output, f"Ошибка при обработке '{input_text}': получено '{title}', ожидалось '{expected_output}'")
# --- New tests for text length logic ---
def test_generate_title_media_with_short_caption(self):
"""Media title should be used if caption is short (< 10 chars)."""
@@ -433,7 +432,13 @@ class TestPostParserGenerateTitle(unittest.TestCase):
message = self._create_mock_message(web_page=web_page_mock, text="Short txt")
self.assertEqual(self.parser._generate_title(message), "Short txt")
# --- End new tests ---
def test_generate_title_truncate_at_first_period(self):
"""Title should be truncated at the first period if present."""
text = "⚡️ OpenAI сегодня представила o3/o4-mini. кажется, они сделали очень сильную ставку на \"агентскость\"."
message = self._create_mock_message(text=text)
expected_title = "⚡️ OpenAI сегодня представила o3/o4-mini"
self.assertEqual(self.parser._generate_title(message), expected_title)
if __name__ == '__main__':
unittest.main()