Dự án phân tích và dự đoán xếp hạng bài toán Codeforces sử dụng Web Scraping, Web Mining Analysis và Machine Learning (Random Forest).
Dự án này thực hiện phân tích toàn diện dữ liệu từ Codeforces theo 3 trụ cột Web Mining:
- Phân tích tags, độ khó (rating)
- Phân cụm bài toán (clustering)
- Phân tích tiêu đề và chủ đề
- Cấu trúc contest
- Mối quan hệ giữa các bài toán
- Tag co-occurrence network
- Độ khó tăng dần trong contest
- Submission patterns
- Ngôn ngữ lập trình phổ biến
- Tỷ lệ thành công (acceptance rate)
- Hiệu suất thời gian và bộ nhớ
- Random Forest Regressor để dự đoán rating cho bài toán
- Feature engineering từ tags, solved_count, contest structure
- Model evaluation với MAE, RMSE, R² score
WEBMINING_CODEFORCES_SCRAPY/
│
├── analysis/ # Phân tích Web Mining
│ └── web_mining_analysis.py # 3 trụ cột: Content, Structure, Usage
│
├── api/ # Flask REST API
│ └── server.py # Backend API endpoints
│
├── codeforces_scrapy/ # Scrapy crawler
│ ├── spiders/
│ │ └── codeforces_spider.py # Spider crawl Codeforces API
│ ├── items.py # Data models
│ ├── mongo_pipeline.py # MongoDB pipeline
│ └── settings.py # Scrapy settings
│
├── codeforces-dashboard/ # React Dashboard
│ ├── src/
│ │ ├── App.js # Main dashboard
│ │ ├── services/
│ │ │ └── api.js # API client
│ │ └── App.css
│ ├── public/
│ └── package.json
│
├── ML_randomforest/ # Machine Learning
│ ├── rating_predictor.py # Random Forest model
│ └── codeforces_rating_model.pkl # Trained model (generated)
│
├── .env # Environment variables
├── requirements.txt # Python dependencies
├── scrapy.cfg # Scrapy configuration
├── run.bat # run
└── README.md # File này
# Chạy trực tiếp file run.bat
run.bat
git clone <repository-url>
cd WebMining_codeforces_scrapy# Tạo virtual environment
python -m venv venv
# Kích hoạt virtual environment
# Windows:
venv\Scripts\activate
# Linux/Mac:
source venv/bin/activate
# Cài đặt dependencies
pip install -r requirements.txtFile requirements.txt:
scrapy>=2.11.0
pymongo>=4.6.0
python-dotenv>=1.0.0
pandas>=2.0.0
numpy>=1.24.0
scikit-learn>=1.3.0
joblib>=1.3.0
networkx>=3.0
flask>=3.0.0
flask-cors>=4.0.0cd codeforces-dashboard
# Cài đặt dependencies
npm install
# Hoặc với yarn
yarn install(không cần, có sẵn trên git)
Terminal 1 - Backend:
python api/server.pyTerminal 2 - Frontend:
cd codeforces-dashboard
npm start
⚠️ Lưu ý: Bước này mất 2-3 giờ. Bạn có thể bỏ qua nếu đã có dữ liệu trong MongoDB.
cd codeforces_scrapy
# Chạy spider
scrapy crawl codeforces_enhanced
# Hoặc với settings tùy chỉnh
scrapy crawl codeforces_enhanced -s CONCURRENT_REQUESTS=1 -s DOWNLOAD_DELAY=2Dữ liệu được thu thập:
- ✅ Problems: >10,000 bài toán
- ✅ Users: Top 2,000 users
- ✅ Submissions: ~950,000 submissions
python analysis/web_mining_analysis.pyKết quả phân tích:
- Content Mining: Tag distribution, difficulty analysis, clustering
- Structure Mining: Contest structure, tag co-occurrence
- Usage Mining: Submission patterns, language preferences, success rates
Kết quả được lưu vào MongoDB collection analysis_reports.
python ML_randomforest/rating_predictor.pyQuy trình:
- Load dữ liệu từ MongoDB
- Feature engineering (tags, solved_count, title_length, etc.)
- Train Random Forest với 200 trees
- Evaluate model (MAE, RMSE, R²)
- Predict rating cho bài toán chưa có rating
- Lưu predictions vào MongoDB
- Lưu trained model (.pkl file)
Kết quả mong đợi:
Test Set:
MAE: ~150-200
RMSE: ~200-250
R²: 0.85-0.90
Thủ công:
# Terminal 1 - Backend
python api/server.py
# Terminal 2 - Frontend
cd codeforces-dashboard
npm startTruy cập: http://localhost:3000
def content_mining_analysis(self):
# Tag distribution
# Difficulty (rating) distribution
# Problem clustering by tags (KMeans)
# Title length analysisInsights:
- Top tags: implementation, math, greedy, dp
- Rating distribution: 800-3500
- Clustering: 10 clusters by topic similarity
def structure_mining_analysis(self):
# Contest structure analysis
# Tag co-occurrence network
# Difficulty progression in contests
# Problem index distributionInsights:
- Average problems per contest: 5-7
- Tag pairs: (dp, greedy), (graphs, trees)
- Progressive difficulty: 60-70% contests
def usage_mining_analysis(self):
# Programming language distribution
# Verdict distribution
# User activity patterns
# Time/memory performance
# Language success ratesInsights:
- Top languages: GNU C++17, C++20, C++23
- Acceptance rate: ~30-40%
- Most active users: 500+ submissions
Features = [
'title_length', # Độ dài tiêu đề
'num_tags', # Số lượng tags
'index_position', # Vị trí trong contest (A=1, B=2, ...)
'solved_count', # Số người giải được
'contest_type', # GYM hoặc REGULAR
'tag_*' # One-hot encoding 50+ tags
]RandomForestRegressor(
n_estimators=4999, # decision trees
max_depth=20, # Depth limit
min_samples_split=5,
min_samples_leaf=2,
random_state=42
)- MAE (Mean Absolute Error): Sai số trung bình
- RMSE (Root Mean Squared Error): Sai số bình phương
- R² Score: Hệ số xác định (0-1, càng cao càng tốt)
- Total statistics
- Rating distribution chart
- Top programming languages
- Top problem tags (Pie chart)
- Tag distribution (Bar chart)
- Clustering results
- Contest structure metrics
- Tag co-occurrence network
- Difficulty progression
- Verdict distribution (Pie chart)
- Submission statistics
- Language success rates
- Predicted ratings for problems
- Model performance metrics
- Sample predictions table
- API Docs: https://codeforces.com/apiHelp
- Rate Limits: 5 requests/second
- Methods:
problemset.problems: Lấy danh sách bài toánuser.ratedList: Lấy danh sách usersuser.status: Lấy submissions của user
- Scrapy: Web scraping framework
- MongoDB: NoSQL database
- Flask: REST API framework
- Flask-CORS: Cross-origin requests
- scikit-learn: Random Forest, preprocessing
- pandas: Data manipulation
- numpy: Numerical computing
- React: UI framework
- Recharts: Data visualization
- Lucide Icons: Icon library
- Axios: HTTP client
- Docker: Containerization
- Docker Compose: Multi-container orchestration
analysis/ → Web Mining analysis scripts
api/ → Flask backend server
codeforces_scrapy/ → Scrapy crawler
├── spiders/ → Spider implementations
├── items.py → Data models
├── pipelines.py → Data processing pipelines
└── settings.py → Crawler configuration
codeforces-dashboard/ → React frontend
ML_randomforest/ → Machine Learning models
- Crawl dữ liệu: 2-3 giờ (10K problems + 2K users + 1M submissions)
- Web Mining Analysis: 5-10 phút
- Train Random Forest: 2-5 phút
- Dashboard load: 10-20 giây
- MongoDB data: ~500MB
- Node modules: ~200MB
- Python packages: ~300MB