UNPKG

hadith-collections

Version:

A comprehensive npm package for searching and browsing hadith collections with Arabic and English support

299 lines (249 loc) • 9.49 kB
const HadithDB = require('./index.js'); /** * Hadith Data Analysis Script * Analyzes the hadith database to provide comprehensive statistics */ class HadithAnalyzer { constructor() { this.db = new HadithDB(); } async connect() { await this.db.connect(); } async close() { await this.db.close(); } /** * Get hadith count per collection using the content tables directly */ async getHadithCountsByCollection() { console.log('šŸ“Š Analyzing hadith counts per collection...'); // Get all collections first const collections = await this.db.getCollections(); const results = []; for (const collection of collections) { try { // Count Arabic hadiths const arabicCount = await this.db.db.getAsync( 'SELECT COUNT(*) as count FROM hadith_content WHERE c1 = ?', [collection.id] ); results.push({ id: collection.id, title: collection.title, title_en: collection.title_en, arabic_hadith_count: arabicCount.count }); } catch (error) { console.warn(`Warning: Could not get count for collection ${collection.id}: ${error.message}`); results.push({ id: collection.id, title: collection.title, title_en: collection.title_en, arabic_hadith_count: 0 }); } } return results; } /** * Get English translation counts per collection */ async getEnglishTranslationCounts() { console.log('šŸŒ Analyzing English translation counts...'); const collections = await this.db.getCollections(); const results = []; for (const collection of collections) { try { // Count English translations const englishCount = await this.db.db.getAsync( 'SELECT COUNT(*) as count FROM hadith_en_content WHERE c2 = ?', [collection.id] ); results.push({ id: collection.id, title: collection.title, title_en: collection.title_en, english_translation_count: englishCount.count }); } catch (error) { console.warn(`Warning: Could not get English count for collection ${collection.id}: ${error.message}`); results.push({ id: collection.id, title: collection.title, title_en: collection.title_en, english_translation_count: 0 }); } } return results; } /** * Get books and chapters count per collection */ async getBooksAndChaptersCounts() { console.log('šŸ“š Analyzing books and chapters per collection...'); const collections = await this.db.getCollections(); const results = []; for (const collection of collections) { try { // Count books const bookCount = await this.db.db.getAsync( 'SELECT COUNT(*) as count FROM book WHERE collection_id = ?', [collection.id] ); // Count chapters const chapterCount = await this.db.db.getAsync( 'SELECT COUNT(*) as count FROM chapter WHERE collection_id = ?', [collection.id] ); results.push({ id: collection.id, title: collection.title, title_en: collection.title_en, book_count: bookCount.count, chapter_count: chapterCount.count }); } catch (error) { console.warn(`Warning: Could not get book/chapter count for collection ${collection.id}: ${error.message}`); results.push({ id: collection.id, title: collection.title, title_en: collection.title_en, book_count: 0, chapter_count: 0 }); } } return results; } /** * Calculate translation coverage */ calculateTranslationCoverage(hadithCounts, englishCounts) { console.log('šŸ“ˆ Calculating translation coverage...'); const coverage = []; let totalArabic = 0; let totalEnglish = 0; for (let i = 0; i < hadithCounts.length; i++) { const arabic = hadithCounts[i].arabic_hadith_count; const english = englishCounts[i].english_translation_count; totalArabic += arabic; totalEnglish += english; const coveragePercent = arabic > 0 ? ((english / arabic) * 100).toFixed(2) : '0.00'; coverage.push({ id: hadithCounts[i].id, title: hadithCounts[i].title, title_en: hadithCounts[i].title_en, arabic_count: arabic, english_count: english, coverage_percent: coveragePercent }); } const overallCoverage = totalArabic > 0 ? ((totalEnglish / totalArabic) * 100).toFixed(2) : '0.00'; return { collections: coverage, overall: { total_arabic: totalArabic, total_english: totalEnglish, overall_coverage_percent: overallCoverage } }; } /** * Format and display results */ displayResults(hadithCounts, englishCounts, booksAndChapters, coverage) { console.log('\n' + '='.repeat(80)); console.log('šŸŒ™ HADITH DATABASE ANALYSIS REPORT'); console.log('='.repeat(80)); // Summary statistics console.log('\nšŸ“‹ SUMMARY STATISTICS:'); console.log(`Total Collections: ${hadithCounts.length}`); console.log(`Total Arabic Hadiths: ${coverage.overall.total_arabic.toLocaleString()}`); console.log(`Total English Translations: ${coverage.overall.total_english.toLocaleString()}`); console.log(`Overall Translation Coverage: ${coverage.overall.overall_coverage_percent}%`); // Detailed breakdown per collection console.log('\nšŸ“Š DETAILED BREAKDOWN BY COLLECTION:'); console.log('-'.repeat(80)); for (let i = 0; i < hadithCounts.length; i++) { const hadith = hadithCounts[i]; const english = englishCounts[i]; const books = booksAndChapters[i]; const cov = coverage.collections[i]; console.log(`\n${hadith.id}. ${hadith.title_en}`); console.log(` Arabic Title: ${hadith.title}`); console.log(` šŸ“– Books: ${books.book_count.toLocaleString()}`); console.log(` šŸ“‘ Chapters: ${books.chapter_count.toLocaleString()}`); console.log(` šŸ“œ Arabic Hadiths: ${hadith.arabic_hadith_count.toLocaleString()}`); console.log(` šŸŒ English Translations: ${english.english_translation_count.toLocaleString()}`); console.log(` šŸ“ˆ Translation Coverage: ${cov.coverage_percent}%`); } // Translation coverage ranking console.log('\nšŸ† TRANSLATION COVERAGE RANKING:'); console.log('-'.repeat(80)); const sortedCoverage = [...coverage.collections].sort((a, b) => parseFloat(b.coverage_percent) - parseFloat(a.coverage_percent) ); sortedCoverage.forEach((collection, index) => { const medal = index === 0 ? 'šŸ„‡' : index === 1 ? '🄈' : index === 2 ? 'šŸ„‰' : ' '; console.log(`${medal} ${collection.title_en}: ${collection.coverage_percent}% (${collection.english_count}/${collection.arabic_count})`); }); // Collections with no translations console.log('\nāš ļø COLLECTIONS WITH NO ENGLISH TRANSLATIONS:'); console.log('-'.repeat(80)); const noTranslations = coverage.collections.filter(c => c.english_count === 0); if (noTranslations.length > 0) { noTranslations.forEach(collection => { console.log(`āŒ ${collection.title_en} (${collection.arabic_count} hadiths)`); }); } else { console.log('āœ… All collections have at least some English translations!'); } console.log('\n' + '='.repeat(80)); console.log('šŸ“Š Analysis Complete!'); console.log('='.repeat(80)); } /** * Run the complete analysis */ async runAnalysis() { console.log('šŸš€ Starting Hadith Database Analysis...\n'); try { await this.connect(); // Get all the data const hadithCounts = await this.getHadithCountsByCollection(); const englishCounts = await this.getEnglishTranslationCounts(); const booksAndChapters = await this.getBooksAndChaptersCounts(); // Calculate coverage const coverage = this.calculateTranslationCoverage(hadithCounts, englishCounts); // Display results this.displayResults(hadithCounts, englishCounts, booksAndChapters, coverage); // Export to JSON for further analysis const exportData = { timestamp: new Date().toISOString(), summary: coverage.overall, collections: hadithCounts.map((collection, index) => ({ ...collection, english_translation_count: englishCounts[index].english_translation_count, book_count: booksAndChapters[index].book_count, chapter_count: booksAndChapters[index].chapter_count, coverage_percent: coverage.collections[index].coverage_percent })) }; require('fs').writeFileSync('hadith_analysis_report.json', JSON.stringify(exportData, null, 2)); console.log('\nšŸ’¾ Detailed report exported to: hadith_analysis_report.json'); } catch (error) { console.error('āŒ Error during analysis:', error.message); console.error(error.stack); } finally { await this.close(); } } } // Run the analysis if this script is executed directly if (require.main === module) { const analyzer = new HadithAnalyzer(); analyzer.runAnalysis().catch(console.error); } module.exports = HadithAnalyzer;