|
import gradio as gr |
|
import re |
|
from collections import Counter |
|
from datetime import datetime |
|
import emoji |
|
import logging |
|
from typing import Tuple, List, Optional |
|
import statistics |
|
import csv |
|
from io import StringIO |
|
|
|
|
|
logging.basicConfig(level=logging.INFO) |
|
logger = logging.getLogger(__name__) |
|
|
|
def clean_text(text): |
|
"""Очищает текст от лишних пробелов и переносов строк""" |
|
return ' '.join(text.split()) |
|
|
|
def count_emojis(text): |
|
"""Подсчитывает количество эмодзи в тексте""" |
|
return len([c for c in text if c in emoji.EMOJI_DATA]) |
|
|
|
def extract_mentions(text): |
|
"""Извлекает упоминания пользователей из текста""" |
|
return re.findall(r'@[\w\.]+', text) |
|
|
|
def get_comment_words(text): |
|
"""Получает список слов из комментария для анализа""" |
|
words = re.findall(r'\w+', text.lower()) |
|
return [w for w in words if len(w) > 2] |
|
|
|
def analyze_sentiment(text): |
|
"""Расширенный анализ тональности по эмодзи и ключевым словам""" |
|
positive_indicators = ['🔥', '❤️', '👍', '😊', '💪', '👏', '🎉', '♥️', '😍', '🙏', |
|
'круто', 'супер', 'класс', 'огонь', 'пушка', 'отлично', 'здорово', |
|
'прекрасно', 'молодец', 'красота', 'спасибо', 'топ', 'лучший'] |
|
negative_indicators = ['👎', '😢', '😞', '😠', '😡', '💔', '😕', '😑', |
|
'плохо', 'ужас', 'отстой', 'фу', 'жесть', 'ужасно', |
|
'разочарован', 'печаль', 'грустно'] |
|
|
|
text_lower = text.lower() |
|
positive_count = sum(1 for ind in positive_indicators if ind in text_lower) |
|
negative_count = sum(1 for ind in negative_indicators if ind in text_lower) |
|
|
|
exclamation_count = text.count('!') |
|
positive_count += exclamation_count * 0.5 if positive_count > negative_count else 0 |
|
negative_count += exclamation_count * 0.5 if negative_count > positive_count else 0 |
|
|
|
if positive_count > negative_count: |
|
return 'positive' |
|
elif negative_count > positive_count: |
|
return 'negative' |
|
return 'neutral' |
|
|
|
def extract_comment_data(comment_text): |
|
"""Извлекает данные из отдельного комментария""" |
|
try: |
|
if 'Скрыто алгоритмами Instagram' in comment_text: |
|
username_match = re.search(r"Фото профиля ([^\n]+)", comment_text) |
|
if username_match: |
|
return username_match.group(1).strip(), "", 0, 0 |
|
|
|
username_match = re.search(r"Фото профиля ([^\n]+)", comment_text) |
|
if not username_match: |
|
return None, None, 0, 0 |
|
|
|
username = username_match.group(1).strip() |
|
|
|
comment_pattern = fr"{re.escape(username)}\n(.*?)(?:\d+ нед\.)" |
|
comment_match = re.search(comment_pattern, comment_text, re.DOTALL) |
|
if comment_match: |
|
comment = clean_text(comment_match.group(1)) |
|
comment = re.sub(fr'^{re.escape(username)}\s*', '', comment) |
|
comment = re.sub(r'^@[\w\.]+ ', '', comment) |
|
else: |
|
comment = "" |
|
|
|
week_match = re.search(r'(\d+) нед\.', comment_text) |
|
weeks = int(week_match.group(1)) if week_match else 0 |
|
|
|
likes = 0 |
|
likes_patterns = [ |
|
r"(\d+) отметк[аи] \"Нравится\"", |
|
r"Нравится: (\d+)", |
|
] |
|
|
|
for pattern in likes_patterns: |
|
likes_match = re.search(pattern, comment_text) |
|
if likes_match: |
|
likes = int(likes_match.group(1)) |
|
break |
|
|
|
return username, comment.strip(), likes, weeks |
|
except Exception as e: |
|
logger.error(f"Error extracting comment data: {e}") |
|
return None, None, 0, 0 |
|
|
|
def analyze_post(content_type, link_to_post, post_likes, post_date, description, comment_count, all_comments): |
|
try: |
|
|
|
comments_blocks = re.split(r'(?=Фото профиля|Скрыто алгоритмами Instagram)', all_comments) |
|
comments_blocks = [block for block in comments_blocks if block.strip()] |
|
|
|
|
|
hidden_comments = len(re.findall(r'Скрыто алгоритмами Instagram', all_comments)) |
|
|
|
usernames = [] |
|
comments = [] |
|
likes = [] |
|
weeks = [] |
|
|
|
total_emojis = 0 |
|
mentions = [] |
|
sentiments = [] |
|
comment_lengths = [] |
|
words_per_comment = [] |
|
all_words = [] |
|
user_engagement = {} |
|
|
|
|
|
for block in comments_blocks: |
|
if 'Скрыто алгоритмами Instagram' in block: |
|
continue |
|
|
|
username, comment, like_count, week_number = extract_comment_data(block) |
|
if username and (comment is not None): |
|
usernames.append(username) |
|
comments.append(comment) |
|
likes.append(str(like_count)) |
|
weeks.append(week_number) |
|
|
|
total_emojis += count_emojis(comment) |
|
mentions.extend(extract_mentions(comment)) |
|
sentiment = analyze_sentiment(comment) |
|
sentiments.append(sentiment) |
|
comment_lengths.append(len(comment)) |
|
|
|
words = get_comment_words(comment) |
|
words_per_comment.append(len(words)) |
|
all_words.extend(words) |
|
|
|
if username not in user_engagement: |
|
user_engagement[username] = { |
|
'comments': 0, |
|
'total_likes': 0, |
|
'emoji_usage': 0, |
|
'avg_length': 0, |
|
'sentiments': [], |
|
'weeks': [] |
|
} |
|
user_stats = user_engagement[username] |
|
user_stats['comments'] += 1 |
|
user_stats['total_likes'] += like_count |
|
user_stats['emoji_usage'] += count_emojis(comment) |
|
user_stats['avg_length'] += len(comment) |
|
user_stats['sentiments'].append(sentiment) |
|
user_stats['weeks'].append(week_number) |
|
|
|
|
|
total_comments = len(comments) |
|
if total_comments != comment_count: |
|
logger.warning(f"Found {total_comments} comments, but expected {comment_count}") |
|
|
|
|
|
for username in user_engagement: |
|
stats = user_engagement[username] |
|
stats['avg_length'] /= stats['comments'] |
|
stats['engagement_rate'] = stats['total_likes'] / stats['comments'] |
|
stats['sentiment_ratio'] = sum(1 for s in stats['sentiments'] if s == 'positive') / len(stats['sentiments']) |
|
stats['activity_period'] = max(stats['weeks']) - min(stats['weeks']) if stats['weeks'] else 0 |
|
|
|
|
|
avg_comment_length = sum(comment_lengths) / total_comments |
|
sentiment_distribution = Counter(sentiments) |
|
most_active_users = Counter(usernames).most_common(5) |
|
most_mentioned = Counter(mentions).most_common(5) |
|
avg_likes = sum(map(int, likes)) / len(likes) if likes else 0 |
|
earliest_week = max(weeks) if weeks else 0 |
|
latest_week = min(weeks) if weeks else 0 |
|
|
|
|
|
median_comment_length = statistics.median(comment_lengths) |
|
avg_words_per_comment = sum(words_per_comment) / total_comments |
|
common_words = Counter(all_words).most_common(10) |
|
|
|
|
|
engagement_periods = { |
|
'early': [], |
|
'middle': [], |
|
'late': [] |
|
} |
|
week_range = max(weeks) - min(weeks) if weeks else 0 |
|
period_length = week_range / 3 if week_range > 0 else 1 |
|
|
|
for i, week in enumerate(weeks): |
|
if week >= max(weeks) - period_length: |
|
engagement_periods['early'].append(i) |
|
elif week >= max(weeks) - 2 * period_length: |
|
engagement_periods['middle'].append(i) |
|
else: |
|
engagement_periods['late'].append(i) |
|
|
|
period_stats = { |
|
period: { |
|
'comments': len(indices), |
|
'avg_likes': sum(int(likes[i]) for i in indices) / len(indices) if indices else 0, |
|
'sentiment_ratio': sum(1 for i in indices if sentiments[i] == 'positive') / len(indices) if indices else 0 |
|
} |
|
for period, indices in engagement_periods.items() |
|
} |
|
|
|
|
|
csv_data = { |
|
'metadata': { |
|
'content_type': content_type, |
|
'link': link_to_post, |
|
'post_likes': post_likes, |
|
'post_date': post_date, |
|
'total_comments': total_comments, |
|
'expected_comments': comment_count, |
|
'hidden_comments': hidden_comments |
|
}, |
|
'basic_stats': { |
|
'avg_comment_length': avg_comment_length, |
|
'median_comment_length': median_comment_length, |
|
'avg_words': avg_words_per_comment, |
|
'total_emojis': total_emojis, |
|
'avg_likes': avg_likes |
|
}, |
|
'sentiment_stats': { |
|
'positive': sentiment_distribution['positive'], |
|
'neutral': sentiment_distribution['neutral'], |
|
'negative': sentiment_distribution['negative'] |
|
}, |
|
'period_analysis': period_stats, |
|
'top_users': dict(most_active_users), |
|
'top_mentioned': dict(most_mentioned) |
|
} |
|
|
|
|
|
output = StringIO() |
|
writer = csv.writer(output) |
|
for section, data in csv_data.items(): |
|
writer.writerow([section]) |
|
for key, value in data.items(): |
|
writer.writerow([key, value]) |
|
writer.writerow([]) |
|
csv_output = output.getvalue() |
|
|
|
|
|
analytics_summary = ( |
|
f"CSV DATA:\n{csv_output}\n\n" |
|
f"ДЕТАЛЬНЫЙ АНАЛИЗ:\n" |
|
f"Контент: {content_type}\n" |
|
f"Ссылка: {link_to_post}\n\n" |
|
f"СТАТИСТИКА:\n" |
|
f"- Всего комментариев: {total_comments} (ожидалось: {comment_count})\n" |
|
f"- Скрытых комментариев: {hidden_comments}\n" |
|
f"- Всего лайков: {sum(map(int, likes))}\n" |
|
f"- Среднее лайков: {avg_likes:.1f}\n" |
|
f"- Период: {earliest_week}-{latest_week} недель\n\n" |
|
f"АНАЛИЗ КОНТЕНТА:\n" |
|
f"- Средняя длина: {avg_comment_length:.1f} символов\n" |
|
f"- Медиана длины: {median_comment_length} символов\n" |
|
f"- Среднее слов: {avg_words_per_comment:.1f}\n" |
|
f"- Эмодзи: {total_emojis}\n" |
|
f"- Тональность:\n" |
|
f" * Позитив: {sentiment_distribution['positive']}\n" |
|
f" * Нейтрально: {sentiment_distribution['neutral']}\n" |
|
f" * Негатив: {sentiment_distribution['negative']}\n\n" |
|
f"ПОПУЛЯРНЫЕ СЛОВА:\n" |
|
+ "\n".join([f"- {word}: {count}" for word, count in common_words]) + "\n\n" |
|
f"АКТИВНЫЕ ПОЛЬЗОВАТЕЛИ:\n" |
|
+ "\n".join([f"- {user}: {count}" for user, count in most_active_users]) + "\n\n" |
|
f"УПОМИНАНИЯ:\n" |
|
+ "\n".join([f"- {user}: {count}" for user, count in most_mentioned if user]) + "\n\n" |
|
f"АНАЛИЗ ПО ПЕРИОДАМ:\n" |
|
+ "\n".join([f"- {period}: {stats['comments']} комментариев, {stats['avg_likes']:.1f} лайков/коммент, " |
|
f"{stats['sentiment_ratio']*100:.1f}% позитивных" |
|
for period, stats in period_stats.items()]) + "\n\n" |
|
f"ЭКСПЕРИМЕНТАЛЬНАЯ АНАЛИТИКА:\n" |
|
f"- Самый активный период: {max(period_stats.items(), key=lambda x: x[1]['comments'])[0]}\n" |
|
f"- Наиболее позитивный период: {max(period_stats.items(), key=lambda x: x[1]['sentiment_ratio'])[0]}\n" |
|
f"- Период с макс. вовлеченностью: {max(period_stats.items(), key=lambda x: x[1]['avg_likes'])[0]}" |
|
) |
|
|
|
return analytics_summary, "\n".join(usernames), "\n".join(comments), "\n".join(likes), str(sum(map(int, likes))) |
|
|
|
except Exception as e: |
|
logger.error(f"Error in analyze_post: {e}", exc_info=True) |
|
return f"Error: {str(e)}", "", "", "", "0" |
|
|
|
|
|
iface = gr.Interface( |
|
fn=analyze_post, |
|
inputs=[ |
|
gr.Radio(choices=["Photo", "Video"], label="Content Type", value="Photo"), |
|
gr.Textbox(label="Link to Post"), |
|
gr.Number(label="Likes", value=0), |
|
gr.Textbox(label="Post Date"), |
|
gr.Textbox(label="Description", lines=3), |
|
gr.Number(label="Total Comment Count", value=0), |
|
gr.Textbox(label="All Comments", lines=10) |
|
], |
|
outputs=[ |
|
gr.Textbox(label="Analytics Summary", lines=20), |
|
gr.Textbox(label="Usernames"), |
|
gr.Textbox(label="Comments"), |
|
gr.Textbox(label="Likes Chronology"), |
|
gr.Textbox(label="Total Likes on Comments") |
|
], |
|
title="Enhanced Instagram Comment Analyzer", |
|
description="Анализатор комментариев Instagram с расширенной аналитикой и CSV-форматированием" |
|
) |
|
|
|
if __name__ == "__main__": |
|
iface.launch() |