2013-12-18 3 views
1

내가 가짜 일본어 단어를 생성하는 데 사용되는 토큰의 목록을 가지고, 그것은 다음과 같습니다알려진 토큰 목록에서 문자열을 토큰 화하는 방법은 무엇입니까?

var syllables = ["chi","tsu","shi","ka","ki","ku","ke","ko","ta","te","to","sa","su","se","so","na","ni","nu","ne","no","ha","hi","fu","he","ho","ma","mi","mu","me","mo","ya","yu","yo","ra","ri","ru","re","ro","wa","wo"]; 

는 "yoniyotachihochinitarehakemukenushihofure"와 같은 단어를 감안할 때,이 같은 배열로 별도의 토큰으로 분할 할 수 있습니다 [ "yo", "ni", "yo"... RegExps 사용?

지금까지, 나는

var s=""; 
    for(var i=0;i<syllables.length;i++) 
    s+=("("+syllables[i]+")"); 
    s+="+"; 
    console.log(s); 
    var splitregex = new RegExp(s,"gi"); 
    console.log(str.split(splitregex)); 

을하고 난 "[ 'yoniyotachihochinitarehakemukenushihofure']"돌아

+0

알 관련성이 높습니다. http://thedailywtf.com/Articles/The-Automated-Curse-Generator.aspx – striking

답변

3
var syllables = ["chi","tsu","shi","ka","ki","ku","ke","ko","ta","te","to","sa","su","se","so","na","ni","nu","ne","no","ha","hi","fu","he","ho","ma","mi","mu","me","mo","ya","yu","yo","ra","ri","ru","re","ro","wa","wo"]; 

var r = new RegExp(syllables.join('|'), 'g'); 

var str = 'yoniyotachihochinitarehakemukenushihofure'; 

console.log(str.match(r)); 
// return ["yo", "ni", "yo", "ta", "chi", "ho", "chi", "ni", "ta", "re", "ha", "ke", "mu", "ke", "nu", "shi", "ho", "fu", "re"] 

http://jsfiddle.net/T8LeY/

설명 : 단순히 모든 일치하는 정규식 /chi|tsu|.../g를 생성 목록의 음절

관련 문제