id-auto-formalizer
Version:
Konversi teks Bahasa Indonesia dari kasual ke formal untuk surat, email, dan laporan resmi
140 lines (116 loc) ⢠5.01 kB
JavaScript
const IndonesianFormalizer = require('../src/index');
const chalk = require('chalk');
console.log(chalk.cyan('\nš®š© Bahasa Indonesia Auto Formalizer - Demo\n'));
// 1. Basic Usage
console.log(chalk.yellow('1. Basic Usage:'));
const formalizer = new IndonesianFormalizer();
const casualText = 'gue mau nanya dong, lo udah selesai ngerjain tugasnya belom?';
const formalText = formalizer.formalize(casualText);
console.log('Casual:', chalk.gray(casualText));
console.log('Formal:', chalk.green(formalText));
// 2. Convenience Functions
console.log(chalk.yellow('\n2. Using Convenience Functions:'));
const { formalize, analyze, suggest } = require('../src/index');
const text1 = 'makasih bgt ya udh bantuin gue kemaren';
console.log('Input:', chalk.gray(text1));
console.log('Output:', chalk.green(formalize(text1)));
// 3. Formality Analysis
console.log(chalk.yellow('\n3. Formality Analysis:'));
const texts = [
'gue capek bgt nih',
'saya agak lelah',
'Saya merasa sangat lelah',
'lo tau gak sih?',
'Apakah Anda mengetahui hal tersebut?'
];
texts.forEach(text => {
const analysis = analyze(text);
const color = analysis.score > 80 ? chalk.green :
analysis.score > 50 ? chalk.yellow : chalk.red;
console.log(`"${text}" ā ${color(analysis.level)} (${analysis.score}%)`);
});
// 4. Suggestions
console.log(chalk.yellow('\n4. Improvement Suggestions:'));
const textToImprove = 'thanks ya gue udh sampe';
const suggestions = suggest(textToImprove);
console.log(`Text: "${textToImprove}"`);
console.log('Suggestions:');
suggestions.forEach((s, i) => {
console.log(` ${i + 1}. ${s.reason}: "${s.original}" ā "${s.suggestion}"`);
});
// 5. Batch Processing
console.log(chalk.yellow('\n5. Batch Processing:'));
const emailDrafts = [
'hai pak, gue mau izin ga masuk hari ini krn sakit',
'sorry telat kirim laporannya, kemaren ada kendala',
'btw meeting besok jadi ga? gue blm dapet konfirmasi',
'thanks buat kesempatannya, gue seneng bgt bisa ikut project ini'
];
console.log('Email drafts to formalize:');
const results = formalizer.formalizeBatch(emailDrafts);
results.forEach((result, i) => {
console.log(`\n${i + 1}. ${chalk.gray(result.original)}`);
console.log(` ${chalk.green(result.formalized)}`);
console.log(` Formality: ${result.analysis.level} (${result.analysis.score}%)`);
});
// 6. Custom Mappings
console.log(chalk.yellow('\n6. Adding Custom Mappings:'));
formalizer.addMapping('ngoding', 'memprogram');
formalizer.addMapping('ngetest', 'melakukan pengujian');
formalizer.addMappings({
'deploy': 'meluncurkan',
'debug': 'memperbaiki kesalahan',
'merge': 'menggabungkan'
});
const techText = 'gue lagi ngoding, abis itu ngetest sebelum deploy';
console.log('Before:', chalk.gray(techText));
console.log('After:', chalk.green(formalizer.formalize(techText)));
// 7. Different Contexts
console.log(chalk.yellow('\n7. Different Contexts:'));
const contexts = {
'Casual Chat': 'hai bro, gmn kabarnya? lama ga ketemu ya',
'Work Email': 'gue mau nanya soal deadline project kemaren',
'Report': 'hasil nya bagus bgt, target tercapai semua',
'Application': 'gue tertarik bgt sama lowongan ini',
'Complaint': 'gue kecewa sama pelayanan nya, ga profesional'
};
for (const [context, text] of Object.entries(contexts)) {
console.log(`\n${chalk.blue(context)}:`);
console.log(`Original: ${chalk.gray(text)}`);
console.log(`Formal: ${chalk.green(formalizer.formalize(text))}`);
}
// 8. Statistics
console.log(chalk.yellow('\n8. Formalizer Statistics:'));
const stats = formalizer.getStatistics();
console.log(`Total word mappings: ${chalk.cyan(stats.totalMappings)}`);
console.log('Categories:');
Object.entries(stats.categories).forEach(([category, count]) => {
console.log(` - ${category}: ${count}`);
});
// 9. Edge Cases
console.log(chalk.yellow('\n9. Handling Edge Cases:'));
const edgeCases = [
'', // empty
' ', // whitespace
'GUE MAU NANYA!!!', // all caps
'gue...mau...nanya...', // excessive punctuation
'haha gue bingung wkwkwk', // with laugh expressions
'@user1 gue setuju sama lo #formalizer', // social media style
];
console.log('Edge cases:');
edgeCases.forEach(text => {
const result = formalizer.formalize(text);
console.log(`"${chalk.gray(text)}" ā "${chalk.green(result)}"`);
});
// 10. Performance Test
console.log(chalk.yellow('\n10. Performance Test:'));
const startTime = Date.now();
const iterations = 1000;
for (let i = 0; i < iterations; i++) {
formalize('gue mau nanya dong, lo udah makan belom?');
}
const endTime = Date.now();
const avgTime = (endTime - startTime) / iterations;
console.log(`Processed ${chalk.cyan(iterations)} texts in ${endTime - startTime}ms`);
console.log(`Average time per text: ${chalk.green(avgTime.toFixed(2))}ms`);
console.log(chalk.cyan('\n⨠Demo selesai! Happy formalizing! š®š©\n'));