2014-12-19 112 views
2

假設我有單詞'buddy'根據另一個陣列的值拆分陣列

第一個數組將單詞按音節打破,例如。 ['bud','dy']

第二個數組是由他們的唱片分手例如。 ['b','u','dd',y']

現在我的問題是表音文字'dd'應該是'd','d',因爲在那裏有一個音節突破,但是我想不出一種有效的方式來解析這兩個數組並且打破唱片,這樣它是['b','u','d','d',y']

這裏是其他一些例子

字:車道

音節:['drive','way']

個表音文字:['d','r','i','ew','ay']

應爲:['d','r','i','v','e','w','ay']

字:越來越

音節:['get','ting']

錄音製品:['g','e','tt','i','ng']

應該是:['g','e','t','t','i','ng']

任何人都知道我可以做到這一點的方式?

+1

這不是關於JavaScript的實際。應該標記_natural語言處理_,_nlp_或相對的東西。對於像我這樣的JS人來說,這仍然是一個有趣的問題。 – Leo 2014-12-19 02:35:21

+0

'每天'不包含音節'['drive','way']',你可能想要清理那個例子。解析這兩個數組並打破唱片的低效率方法是什麼? – adamdc78 2014-12-19 02:36:37

+0

這是因爲我試圖編寫一個腳本來比較兩個數組,並解決他們是否被分割 – Ardenexal 2014-12-19 02:37:14

回答

0

我會嘗試這個[僞]

given i = 0, word = "" 
for phono in phonograms  //loop over all phonograms 
     word += phonograms[phono] //and try to assemble the current syllable 
     if word === syllable[i]     //we may have found a sullable 
      i++ 
      word = "" 
      continue 
     if not the syllable[i] starts with word //if not syllable starts with word, 
               //then we have a phono that 
               //breaks our rules 
      phonos = phonograms[phono].split("") //so let's split that into characters 
      for j in phonos 
       phonograms.splice(phono++, 0, phonos[j]) //and insert to 
                 //the list of phonograms 
      i++