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
JavaScript
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;