bookmarks-dataset / analyze_bookmarks.py
J94's picture
Upload folder using huggingface_hub
9e0b3ba verified
import json
import pandas as pd
import matplotlib.pyplot as plt
from pathlib import Path
import seaborn as sns
# Set style
plt.style.use('ggplot')
sns.set(style="whitegrid")
# Load the data
data_path = Path("data.jsonl")
bookmarks = []
with open(data_path, 'r', encoding='utf-8') as f:
for line in f:
bookmarks.append(json.loads(line))
# Convert to DataFrame
df = pd.DataFrame(bookmarks)
print(f"Loaded {len(df)} bookmarks")
# Basic statistics
print("\nSource Distribution:")
source_counts = df['source'].value_counts()
print(source_counts)
print("\nTop Domains:")
domain_counts = df['domain'].value_counts().head(20)
print(domain_counts)
# Create output directory for plots
plots_dir = Path("plots")
plots_dir.mkdir(exist_ok=True)
# Source distribution pie chart
plt.figure(figsize=(10, 7))
source_counts.plot.pie(autopct='%1.1f%%', textprops={'fontsize': 10})
plt.title('Bookmark Sources', fontsize=14)
plt.ylabel('')
plt.savefig(plots_dir / 'source_distribution.png', bbox_inches='tight')
plt.close()
print("Created source distribution chart: plots/source_distribution.png")
# Top domains bar chart
plt.figure(figsize=(12, 8))
domain_counts.head(15).plot.barh()
plt.title('Top 15 Domains', fontsize=14)
plt.xlabel('Count')
plt.ylabel('Domain')
plt.tight_layout()
plt.savefig(plots_dir / 'top_domains.png', bbox_inches='tight')
plt.close()
print("Created top domains chart: plots/top_domains.png")
# Bookmarks by month (if date data is available)
if 'created_at' in df.columns:
try:
# Convert to datetime if not already
if not pd.api.types.is_datetime64_any_dtype(df['created_at']):
df['created_at'] = pd.to_datetime(df['created_at'], errors='coerce')
# Extract year and month
df['year_month'] = df['created_at'].dt.to_period('M')
# Count bookmarks by month
monthly_counts = df.groupby('year_month').size()
plt.figure(figsize=(14, 8))
monthly_counts.plot.bar()
plt.title('Bookmarks by Month', fontsize=14)
plt.xlabel('Month')
plt.ylabel('Count')
plt.xticks(rotation=45)
plt.tight_layout()
plt.savefig(plots_dir / 'bookmarks_by_month.png', bbox_inches='tight')
plt.close()
print("Created bookmarks by month chart: plots/bookmarks_by_month.png")
# Bookmarks by source over time
source_time = pd.crosstab(df['year_month'], df['source'])
plt.figure(figsize=(14, 8))
source_time.plot.area(alpha=0.6)
plt.title('Bookmarks by Source Over Time', fontsize=14)
plt.xlabel('Month')
plt.ylabel('Count')
plt.legend(title='Source')
plt.tight_layout()
plt.savefig(plots_dir / 'sources_over_time.png', bbox_inches='tight')
plt.close()
print("Created sources over time chart: plots/sources_over_time.png")
except Exception as e:
print(f"Error creating time-based charts: {e}")
# Content length distribution
if 'content_length' in df.columns:
plt.figure(figsize=(12, 8))
sns.histplot(df['content_length'].clip(upper=5000), bins=50)
plt.title('Content Length Distribution (clipped at 5000 chars)', fontsize=14)
plt.xlabel('Content Length (characters)')
plt.ylabel('Count')
plt.tight_layout()
plt.savefig(plots_dir / 'content_length.png', bbox_inches='tight')
plt.close()
print("Created content length distribution: plots/content_length.png")
print("\nAnalysis complete. Check the 'plots' directory for visualizations.")