한글 Random Sentence Generator

Updated! Click here.

Here’s my very first pass at a random sentence generator in Korean.  It’s pretty bad, but gotta start somewhere.  Here’s a gist.

This is all of the code so far (caveats and limitations are commented in-line):

// putting Korean words together out of characters proved unfruitful on first pass
// I couldn't figure out how to get to the unicode value of the word from the
// values of the individual characters, will have to come back to it
//var hangulCons = ['ㅂ','ㅃ','ㅈ','ㅉ','ㄷ','ㄸ','ㄱ','ㄲ','ㅅ','ㅆ','ㅁ','ㄴ','ㅇ','ㄹ','ㅎ','ㅋ','ㅌ','ㅊ','ㅍ'];
//var hangulVows = ['ㅕ','ㅑ','ㅐ','ㅔ','ㅓ','ㅗ','ㅏ','ㅣ','ㅠ','ㅜ','ㅡ'];

//var min = 0;
//var cmax = 18;
//var vmax = 10;

// in future add arrays of common particles for subjects,
// objects, prepositions and verb endings
//var sub = [];
//var obj = [];
//var end = [];

var wmin = 44032;
var wmax = 55204;

var getRandomInt = function() {
	var min = Math.ceil(1);
  var max = Math.floor(3);
  return Math.floor(Math.random() * (max - min)) + min;
};

var getRandomKWord = function() {
	var word = '';
  var min = Math.ceil(44032);
  var max = Math.floor(55204);
  var num = getRandomInt();

  for (var i = 0; i < num; i++) {
  	var rand = Math.floor(Math.random() * (max - min)) + min;
  	word = word + String.fromCodePoint(rand);
  }
  return word;
};

var title = document.getElementById('title');

// note: it is impossible to tell the word ending
// so I cannot change the particle appropriately
// e.g., '는' follows a vowel, but '은' follows a consonant
var text = getRandomKWord() +
	'는' +
  getRandomKWord() +
  '를' +
  getRandomKWord() +
  '습니다.';

title.innerText = text;

UPDATE!

So I spent some time researching this issue of getting the unicode Hangul Jamo characters to convert to the unicode Hangul Syllable characters because this is the only real way I can know what the syllable ends in (vowel endings result in the use of slightly different particles (such as subject and object particles) than do consonant endings).

So I posted a question on StackOverflow. And then answered it myself because my next stop just happened to be this Wikipedia page.  So now my JSFiddle has this updated code (which makes random, unintelligible, but slightly more grammatical Korean sentences):

// here is the formula for conversion:
// [{(initial) × 588} + {(medial) × 28} + (final)] + 44032

// here are the tables (arrays) which make the formula make a little more sense
//var initialJaeum = ['ㄱ','ㄲ','ㄴ','ㄷ','ㄸ','ㄹ','ㅁ','ㅂ','ㅃ','ㅅ','ㅆ','ㅇ','ㅈ','ㅉ','ㅊ','ㅋ','ㅌ','ㅍ','ㅎ'];
//var medialMoeum = ['ㅏ','ㅐ','ㅑ','ㅒ','ㅓ','ㅔ','ㅕ','ㅖ','ㅗ','ㅘ','ㅙ','ㅚ','ㅛ','ㅜ','ㅝ','ㅞ','ㅟ','ㅠ','ㅡ','ㅢ','ㅣ'];
//var finalJaeum = ['','ㄱ','ㄲ','ㄳ','ㄴ','ㄵ','ㄶ','ㄷ','ㄹ','ㄺ','ㄻ','ㄼ','ㄽ','ㄾ','ㄿ','ㅀ','ㅁ','ㅂ','ㅄ','ㅅ','ㅆ','ㅇ','ㅈ','ㅊ','ㅋ','ㅌ','ㅍ','ㅎ'];

// in future add arrays of common particles for subjects,
// objects, prepositions and verb endings
//var sub = [];
//var obj = [];
//var end = [];

//var wmin = 44032;  // unicode hangul syllables
//var wmax = 55204;

var getRandomInt = function(n, o) {
	var min = Math.ceil(n);
  var max = Math.floor(o);
  return Math.floor(Math.random() * (max - min)) + min;
};

var getRandomKWord = function() {
  var word = '';
  var num = getRandomInt(1, 3);

  for (var i = 0; i < num; i++) {
    var randInit = getRandomInt(0, 19) * 588;
    var randMed = getRandomInt(0, 21) * 28;
    var randFin = getRandomInt(0, 28);
    var hangulFormula = randInit + randMed + randFin + 44032;

    word = word + String.fromCodePoint(hangulFormula);
  }
  return { word: word, final: randFin };
};

var title = document.getElementById('title');

var subject = getRandomKWord();
var object = getRandomKWord();  // don't use 'object' as a variable name
var verb = getRandomKWord();

var subParticle = subject.final ? '는' : '은';
var objParticle = object.final ? '를' : '을';

var text = subject.word +
	subParticle +
  object.word +
  objParticle +
  verb.word +
  '습니다.';

title.innerText = text;
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s