id-auto-formalizer
Version:
Konversi teks Bahasa Indonesia dari kasual ke formal untuk surat, email, dan laporan resmi
206 lines (173 loc) • 7.46 kB
JavaScript
const IndonesianFormalizer = require('../src/formalizer');
describe('IndonesianFormalizer', () => {
let formalizer;
beforeEach(() => {
formalizer = new IndonesianFormalizer();
});
describe('Basic Formalization', () => {
test('should replace informal pronouns', () => {
expect(formalizer.formalize('gue mau pergi')).toBe('Saya ingin pergi.');
expect(formalizer.formalize('lo dimana?')).toBe('Anda di mana?');
});
test('should replace informal verbs', () => {
expect(formalizer.formalize('dia lagi ngomong')).toBe('Beliau sedang berbicara.');
expect(formalizer.formalize('gue nanya dong')).toBe('Saya bertanya.');
});
test('should handle multiple informal words', () => {
const input = 'gue mau nanya, lo udah makan belom?';
const expected = 'Saya ingin bertanya, Anda sudah makan belum?';
expect(formalizer.formalize(input)).toBe(expected);
});
test('should preserve formal text', () => {
const formal = 'Saya ingin bertanya kepada Anda.';
expect(formalizer.formalize(formal)).toBe(formal);
});
});
describe('Capitalization', () => {
test('should capitalize first letter', () => {
expect(formalizer.formalize('saya pergi')).toBe('Saya pergi.');
});
test('should capitalize after punctuation', () => {
expect(formalizer.formalize('halo. apa kabar?')).toBe('Halo. Apa kabar?');
});
test('should capitalize pronouns', () => {
expect(formalizer.formalize('terima kasih bapak dan ibu')).toBe('Terima kasih Bapak dan Ibu.');
});
test('should handle no-capitalize option', () => {
const result = formalizer.formalize('gue pergi', { autoCapitalize: false });
expect(result).toBe('saya pergi.');
});
});
describe('Punctuation', () => {
test('should add period at end', () => {
expect(formalizer.formalize('terima kasih')).toBe('Terima kasih.');
});
test('should not duplicate punctuation', () => {
expect(formalizer.formalize('terima kasih.')).toBe('Terima kasih.');
expect(formalizer.formalize('apa kabar?')).toBe('Apa kabar?');
});
test('should fix spacing around punctuation', () => {
expect(formalizer.formalize('halo , apa kabar ?')).toBe('Halo, apa kabar?');
});
test('should handle no-punctuation option', () => {
const result = formalizer.formalize('terima kasih', { autoPunctuation: false });
expect(result).toBe('Terima kasih');
});
});
describe('Formality Analysis', () => {
test('should analyze very informal text', () => {
const analysis = formalizer.analyzeFormalityLevel('gue capek bgt nih');
expect(analysis.level).toBe('Sangat Informal');
expect(analysis.score).toBeLessThan(50);
});
test('should analyze formal text', () => {
const analysis = formalizer.analyzeFormalityLevel('Saya sangat lelah');
expect(analysis.level).toBe('Sangat Formal');
expect(analysis.score).toBeGreaterThan(90);
});
test('should count informal words correctly', () => {
const analysis = formalizer.analyzeFormalityLevel('gue mau nanya dong');
expect(analysis.informalWords).toBe(3);
expect(analysis.totalWords).toBe(4);
});
});
describe('Suggestions', () => {
test('should suggest word replacements', () => {
const suggestions = formalizer.suggestImprovements('gue capek');
expect(suggestions).toContainEqual(
expect.objectContaining({
type: 'word',
original: 'gue',
suggestion: 'saya'
})
);
});
test('should suggest capitalization', () => {
const suggestions = formalizer.suggestImprovements('saya pergi');
expect(suggestions).toContainEqual(
expect.objectContaining({
type: 'capitalization',
reason: 'Awal kalimat harus kapital'
})
);
});
test('should suggest punctuation', () => {
const suggestions = formalizer.suggestImprovements('Terima kasih');
expect(suggestions).toContainEqual(
expect.objectContaining({
type: 'punctuation',
suggestion: '.'
})
);
});
});
describe('Custom Mappings', () => {
test('should add single mapping', () => {
formalizer.addMapping('ngoding', 'memprogram');
expect(formalizer.formalize('saya ngoding')).toBe('Saya memprogram.');
});
test('should add multiple mappings', () => {
formalizer.addMappings({
'ngegame': 'bermain game',
'ngetweet': 'menulis tweet'
});
expect(formalizer.formalize('dia ngegame')).toBe('Beliau bermain game.');
});
test('should export mappings', () => {
const mappings = formalizer.exportMappings();
expect(mappings).toHaveProperty('gue', 'saya');
expect(mappings).toHaveProperty('lo', 'Anda');
});
});
describe('Batch Processing', () => {
test('should process multiple texts', () => {
const texts = ['gue pergi', 'lo makan', 'dia tidur'];
const results = formalizer.formalizeBatch(texts);
expect(results).toHaveLength(3);
expect(results[0].formalized).toBe('Saya pergi.');
expect(results[1].formalized).toBe('Anda makan.');
expect(results[2].formalized).toBe('Beliau tidur.');
});
test('should include analysis in batch results', () => {
const texts = ['gue capek bgt'];
const results = formalizer.formalizeBatch(texts);
expect(results[0]).toHaveProperty('original');
expect(results[0]).toHaveProperty('formalized');
expect(results[0]).toHaveProperty('analysis');
expect(results[0].analysis.level).toBe('Sangat Informal');
});
});
describe('Special Cases', () => {
test('should handle greetings', () => {
expect(formalizer.formalize('hai, gue mau nanya')).toBe('Dengan hormat, saya ingin bertanya.');
expect(formalizer.formalize('pagi pak')).toBe('Selamat pagi, Pak.');
});
test('should handle closings', () => {
expect(formalizer.formalize('thanks ya. bye')).toBe('Terima kasih. Hormat saya.');
expect(formalizer.formalize('wassalam')).toBe('Wassalamualaikum.');
});
test('should handle numbers and units', () => {
expect(formalizer.formalize('harganya 50rb')).toBe('Harganya 50 ribu.');
expect(formalizer.formalize('jaraknya 5km')).toBe('Jaraknya 5km.');
});
test('should remove filler words', () => {
expect(formalizer.formalize('mau dong')).toBe('Ingin.');
expect(formalizer.formalize('iya deh')).toBe('Iya.');
expect(formalizer.formalize('gitu sih')).toBe('Begitu.');
});
test('should handle empty or whitespace text', () => {
expect(formalizer.formalize('')).toBe('');
expect(formalizer.formalize(' ')).toBe('');
});
});
describe('Statistics', () => {
test('should return correct statistics', () => {
const stats = formalizer.getStatistics();
expect(stats.totalMappings).toBeGreaterThan(100);
expect(stats.categories).toHaveProperty('pronouns');
expect(stats.categories).toHaveProperty('verbs');
expect(stats.categories).toHaveProperty('adjectives');
expect(stats.categories).toHaveProperty('slang');
});
});
});