|
import re |
|
import gradio as gr |
|
|
|
def parse_comments(raw_text): |
|
|
|
comment_blocks = raw_text.split("Ответить") |
|
|
|
results = [] |
|
for block in comment_blocks: |
|
|
|
user_match = re.search(r'^([a-zA-Z0-9_]+)', block.strip()) |
|
|
|
text_match = re.search(r'@\w+\s+(.+?)(?:\d+\s(?:нед|дн)\.)', block) |
|
|
|
likes_match = re.search(r'Нравится:\s*(\d+)', block) |
|
|
|
if user_match and text_match: |
|
user = user_match.group(1) |
|
text = text_match.group(1).strip() |
|
likes = int(likes_match.group(1)) if likes_match else 0 |
|
|
|
|
|
if text: |
|
results.append({"Пользователь": user, "Текст": text, "Лайков": likes}) |
|
|
|
return results |
|
|
|
def format_output(parsed_comments): |
|
|
|
formatted_output = "" |
|
for comment in parsed_comments: |
|
formatted_output += f"Пользователь: {comment['Пользователь']}\n" |
|
formatted_output += f"Текст: {comment['Текст']}\n" |
|
formatted_output += f"Лайков: {comment['Лайков']}\n\n" |
|
return formatted_output |
|
|
|
def process_text(raw_text): |
|
parsed_comments = parse_comments(raw_text) |
|
return format_output(parsed_comments) |
|
|
|
|
|
iface = gr.Interface( |
|
fn=process_text, |
|
inputs=gr.Textbox(lines=10, placeholder="Введите текст сюда..."), |
|
outputs=gr.Textbox(lines=10, placeholder="Результат...") |
|
) |
|
|
|
|
|
iface.launch() |