Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reference for korean past #3

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 106 additions & 10 deletions src/korean.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,21 @@ const breakdown = (input) => {
// input: array of 2 or 3 numbers representing modern jamo (components) that make up a hangul syllable
// output: a hangul character
const combineSymbols = (input) => {
const initialValue = input[0] * 588;
const medialValue = input[1] * 28;
const finalValue = input[2] ? input[2] : 0;
const total = initialValue + medialValue + finalValue + 44032;
const finalWord = String.fromCharCode(total);
return finalWord;
let unicodeTotal = (input[0] * 588) + (input[1] * 28) + 44032;
if (input.length === 3) {
unicodeTotal += input[2];
}
return String.fromCharCode(unicodeTotal);
};

const makePast = (syllable, stem, wordLength, replacement) => {
syllable.push(replacement);
syllable.push(20); // add ㅆ
syllable = combineSymbols(syllable);
if (wordLength <= 2) {
return `${syllable}어`;
}
return `${stem}${syllable}어`;
};

class Korean {
Expand Down Expand Up @@ -130,16 +139,103 @@ class Korean {
}

doPast (word) {
console.info(word);
// stuff for past tense
const wordLength = word.length;
let conjugate = '';
// if the ending is 'ha' then convert to 'haet': '하' to '했'
if (word[wordLength - 2] === '하') {
conjugate = word.slice(0, wordLength - 2);
return `${conjugate}했어`;
} else if (word[wordLength - 2] === '르') {
const stem = breakdown(word.slice(0, wordLength - 2));
stem.push(8); // ㄹ
const newSyllable = combineSymbols(stem);
switch (stem[stem.length - 2]) {
case 0:
case 8:
// ㅏ and ㅗ are followed by 라
return `${newSyllable}랐어`;
default:
// other vowels are followed by 러
return `${newSyllable}렀어`;
}
} else if (word[wordLength - 2] === '비') {
return `${word[wordLength - 2]}었어`;
} else if (word[wordLength - 2] === '있') {
return '있었어';
} else {
/** breakdown the word to find out the 2nd to last character's letter **/
const brokeWord = breakdown(word[wordLength - 2]);
const brokeLength = brokeWord.length;
const syllableEnd = brokeWord[brokeLength - 1];
const newSyllable = brokeWord.slice(0, brokeLength - 1);
let stemWord = word.slice(0, wordLength - 2);

switch (syllableEnd) {
case 0:
case 4:
// if last letter is ㅏ leave alone
return word.slice(0, wordLength - 1);
case 1:
case 8:
// ㄱ, ㄹ
switch (brokeWord[brokeLength - 2]) {
case 0:
case 8:
// ㅗㄹ, ㅏㄹ, ㅗㄱ, ㅏㄱ
stemWord = word.slice(0, wordLength - 1);
return `${stemWord}았어`;
case 4:
case 13:
case 18:
// ㅓㄹ, ㅜㄹ, ㅡㄹ, ㅓㄱ, ㅜㄱ, ㅡㄱ
stemWord = word.slice(0, wordLength - 1);
return `${stemWord}었어`;
default:
// replace with: ㅘ (9)
// concat back to word
return makePast(newSyllable, stemWord, wordLength, 9);
}
case 13:
// 13 (ㅜ) convert to 14 (ㅝ)
return makePast(newSyllable, stemWord, wordLength, 14);
case 17:
// ㅂ
switch (brokeWord[brokeLength - 2]) {
case 4:
case 13:
brokeWord.pop();
return `${combineSymbols(brokeWord)}웠어`;
default:
break;
}
break;
case 18:
// vowel ㅡ replace with ㅓ (4)
return makePast(newSyllable, stemWord, wordLength, 4);
case 20:
// vowel ㅣ replace with ㅕ(6)
return makePast(newSyllable, stemWord, wordLength, 6);
default:
// if consonant
conjugate = word.slice(0, wordLength - 1);
return conjugate.concat('어');
}
}
}

doFuture (word) {
const stem = breakdown(word.slice(0, -1));
if (stem[-1] !== 8) {
if (stem.length < 3) {
stem.push(8);
return `${combineSymbols(stem)} 거야`;
}
if (stem[stem.length - 1] === 17) {
stem.pop();
return `${combineSymbols(stem)}울 거야`;
} else if (stem[stem.length - 1] === 8) {
return `${combineSymbols(stem)} 거야`;
}
return `${combineSymbols(stem)}꺼야`;
return `${combineSymbols(stem)}을 거야`;
}
} // end for class

Expand Down
201 changes: 157 additions & 44 deletions test/korean_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,48 +105,161 @@ describe('Korean', () => {
presentWord = kc.conjugate('서두르다', {tense: 'present'});
presentWord = kc.conjugate('서둘러', {tense: 'present'});
});
});
describe('Future Tense', () => {
it('should conjugate regular verbs correctly', () => {
const kc = new Korean();
let presentWord = kc.conjugate('하다', {tense: 'future'});
expect(presentWord).to.equal('할꺼야');

presentWord = kc.conjugate('오다', {tense: 'future'});
expect(presentWord).to.equal('올꺼야');

presentWord = kc.conjugate('비다', {tense: 'future'});
expect(presentWord).to.equal('빌꺼야');
});
it('should conjugate words with ㄹ final vowel correctly', () => {
const kc = new Korean();
let presentWord = kc.conjugate('놀다', {tense: 'future'});
expect(presentWord).to.equal('놀꺼야');

presentWord = kc.conjugate('날다', {tense: 'future'});
expect(presentWord).to.equal('날꺼야');

presentWord = kc.conjugate('울다', {tense: 'future'});
expect(presentWord).to.equal('울꺼야');
});
});
describe('Present Continuous Tense', () => {
it('should conjugate verbs correctly', () => {
const kc = new Korean();
let presentWord = kc.conjugate('하다', {tense: 'PresentContinuous'});
expect(presentWord).to.equal('하고있어');

presentWord = kc.conjugate('오다', {tense: 'PresentContinuous'});
expect(presentWord).to.equal('오고있어');

presentWord = kc.conjugate('비다', {tense: 'PresentContinuous'});
expect(presentWord).to.equal('비고있어');

presentWord = kc.conjugate('먹다', {tense: 'PresentContinuous'});
expect(presentWord).to.equal('먹고있어');

presentWord = kc.conjugate('좋아하다', {tense: 'PresentContinuous'});
expect(presentWord).to.equal('좋아하고있어');
});
});
});
describe('Future Tense', () => {
it('should conjugate verbs with stem ending with a vowel correctly', () => {
const kc = new Korean();
let futureWord = kc.conjugate('하다', {tense: 'future'});
expect(futureWord).to.equal('할 거야');

futureWord = kc.conjugate('오다', {tense: 'future'});
expect(futureWord).to.equal('올 거야');

futureWord = kc.conjugate('비다', {tense: 'future'});
expect(futureWord).to.equal('빌 거야');
});
it('should conjugate verbs with stem ending with a ㄹ consonant correctly', () => {
const kc = new Korean();
let futureWord = kc.conjugate('놀다', {tense: 'future'});
expect(futureWord).to.equal('놀 거야');

futureWord = kc.conjugate('날다', {tense: 'future'});
expect(futureWord).to.equal('날 거야');

futureWord = kc.conjugate('울다', {tense: 'future'});
expect(futureWord).to.equal('울 거야');
});
it('should conjugate verbs with stem ending with ㅂ consonant correctly', () => {
const kc = new Korean();
let futureWord = kc.conjugate('춥다', {tense: 'future'});
expect(futureWord).to.equal('추울 거야');

futureWord = kc.conjugate('덥다', {tense: 'future'});
expect(futureWord).to.equal('더울 거야');
});
it('should conjugate verbs with stem ending with other consonants correctly', () => {
const kc = new Korean();
let futureWord = kc.conjugate('있다', {tense: 'future'});
expect(futureWord).to.equal('있을 거야');

futureWord = kc.conjugate('먹다', {tense: 'future'});
expect(futureWord).to.equal('먹을 거야');
});
});

describe('Present Continuous Tense', () => {
it('should conjugate verbs correctly', () => {
const kc = new Korean();
let presentWord = kc.conjugate('하다', {tense: 'PresentContinuous'});
expect(presentWord).to.equal('하고있어');

presentWord = kc.conjugate('오다', {tense: 'PresentContinuous'});
expect(presentWord).to.equal('오고있어');

presentWord = kc.conjugate('비다', {tense: 'PresentContinuous'});
expect(presentWord).to.equal('비고있어');

presentWord = kc.conjugate('먹다', {tense: 'PresentContinuous'});
expect(presentWord).to.equal('먹고있어');

presentWord = kc.conjugate('좋아하다', {tense: 'PresentContinuous'});
expect(presentWord).to.equal('좋아하고있어');
});
});

describe('Past Tense', () => {
it('should conjugate 하다 verbs correctly', () => {
const kc = new Korean();
let pastWord = kc.conjugate('하다', { tense: 'past' });
expect(pastWord).to.equal('했어');

pastWord = kc.conjugate('좋아하다', {tense: 'past'});
expect(pastWord).to.equal('좋아했어');
});
it('should conjugate ㅗ다 words correctly', () => {
const kc = new Korean();
let pastWord = kc.conjugate('오다', {tense: 'past'});
expect(pastWord).to.equal('왔어');

pastWord = kc.conjugate('보다', {tense: 'past'});
expect(pastWord).to.equal('봤어');
});
it('should conjugate ㅜ다 words correctly', () => {
const kc = new Korean();
let pastWord = kc.conjugate('춤추다', {tense: 'past'});
expect(pastWord).to.equal('춤췄어');

pastWord = kc.conjugate('도와주다', {tense: 'past'});
expect(pastWord).to.equal('도와줬어');
});
it('should conjugate ㅡ다 words correctly', () => {
const kc = new Korean();
let pastWord = kc.conjugate('쓰다', {tense: 'past'});
expect(pastWord).to.equal('썼어');
});
it('should conjugate ㅣ다 words correctly', () => {
const kc = new Korean();
let pastWord = kc.conjugate('우기다', {tense: 'past'});
expect(pastWord).to.equal('우겼어');

pastWord = kc.conjugate('지다', {tense: 'past'});
expect(pastWord).to.equal('졌어');
});
it('should conjugate ㅗ르 and ㅜ르 words correctly', () => {
const kc = new Korean();
let pastWord = kc.conjugate('모르다', {tense: 'past'});
expect(pastWord).to.equal('몰랐어');

pastWord = kc.conjugate('부르다', {tense: 'past'});
expect(pastWord).to.equal('불렀어');
});
it('should conjugate words with ㅗㄹ and ㅏㄹ final vowel correctly', () => {
const kc = new Korean();
let pastWord = kc.conjugate('놀다', {tense: 'past'});
expect(pastWord).to.equal('놀았어');

pastWord = kc.conjugate('날다', {tense: 'past'});
expect(pastWord).to.equal('날았어');
});
it('should conjugate words with ㅗㄱ and ㅏㄱ final vowel correctly', () => {
const kc = new Korean();
let pastWord = kc.conjugate('녹다', {tense: 'past'});
expect(pastWord).to.equal('녹았어');

pastWord = kc.conjugate('막다', {tense: 'past'});
expect(pastWord).to.equal('막았어');
});
it('should conjugate words with ㅜㄹ, ㅓㄹ, and ㅡㄹ final vowel correctly', () => {
const kc = new Korean();
let pastWord = kc.conjugate('울다', {tense: 'past'});
expect(pastWord).to.equal('울었어');

pastWord = kc.conjugate('얼다', {tense: 'past'});
expect(pastWord).to.equal('얼었어');

pastWord = kc.conjugate('늘다', {tense: 'past'});
expect(pastWord).to.equal('늘었어');
});
it('should conjugate words with ㅜㄱ, ㅓㄱ, and ㅡㄱ final vowel correctly', () => {
const kc = new Korean();
let pastWord = kc.conjugate('죽다', {tense: 'past'});
expect(pastWord).to.equal('죽었어');

pastWord = kc.conjugate('먹다', {tense: 'past'});
expect(pastWord).to.equal('먹었어');
});
it('should conjugate words with ㅂ다 words correctly', () => {
const kc = new Korean();
let pastWord = kc.conjugate('춥다', {tense: 'past'});
expect(pastWord).to.equal('추웠어');

pastWord = kc.conjugate('덥다', {tense: 'past'});
expect(pastWord).to.equal('더웠어');
});
it('should conjugate words with 있다 words correctly', () => {
const kc = new Korean();
let pastWord = kc.conjugate('있다', {tense: 'past'});
expect(pastWord).to.equal('있었어');
});
});
});