2016-11-09 7 views
0

文中のほとんどの単語の終わりの文字と、その文字で終わるすべての単語を見つけようとしています。文章中の単語が終わる最も一般的な文字を見つける機能

これは私の宿題ですが、私は分割機能を試しましたが、スライス機能には役立ちませんが、時間がかかりすぎます。

var sentence = 'Down by the river there is a man that quiver and shiver, but he needs to deliver a packet that he think is a big racket and a packet of of gum.' 

function mostWordsEndsWith(){ 
    for (var i = 0; i < wordsEndsWith.length; i++) { 
     var currentWord = wordsEndWith[i].split('t') 
     console.log(currentWord); 
    } 
} 
mostWordsEndsWith(sentence); 
+0

なぜ「分割」は役に立たないのですか? – Filburt

+1

私はあなたが言葉の配列をループするだけで、 '' yourArray [Index] .substring(yourArray [index] .length - 1、yourArray [index] .length); ''を実行し、カウンタに追加することができますあなたが行くように。長いけど、シンプル。別のループでサブストリングを収集し、それらの別々のアレイをループして最長のループを見つけることも、より良い方法です。 – Crowes

+0

私はあなたが少なくとも何か実際に何かをするいくつかのコードを考え出すことができるまで試してみることをお勧めします - 現在あなたの関数 'mostWordsEndsWith'はあなたがそれを呼び出そうとしているパラメータとあなたの配列' wordsEndsWith' isn 'どこでも定義できます。 – Filburt

答えて

-1

これを行う方法があります。互換性が問題になる場合は、いくつかのES6機能を使用することに注意してください。ここ

var sentence = "Down by the river there is a man that quiver" 
 
      + " and shiver, but he needs to deliver a packet" 
 
      + " that he think is a big racket and a packet" 
 
      + " of of gum." 
 

 
function mostWordsEndsWith (str){ 
 
    //index for: letter --> { count, words } 
 
    //this is quite a good object itself, however not yet the 
 
    //desired final result 
 
    let endLetterCount = 
 
     //get array of words 
 
     str.match(/\b\w+\b/g) 
 
      //add information about last letter. 
 
      //There is no error handling, but looking what was 
 
      //matched before, something must go very wrong 
 
      //for it to not match. 
 
      .map((w) => ({ 
 
       word: w, 
 
       last: w.match(/(\w)(?:[^\w]|$)/)[1] 
 
      })) 
 
      //create index 
 
      .reduce((p, c) => { 
 
       if (p.hasOwnProperty(c.last)) { 
 
       p[c.last].count++; 
 
       p[c.last].words.push(c.word); 
 
       } else p[c.last] = { count: 1, words: [c.word] }; 
 
       return p; 
 
      }, 
 
      //ugly, hi javascript... 
 
      //There are lengthy discussions on why objects 
 
      //dont per default work similar to this, not too 
 
      //relevant for this question. 
 
      { 
 
       [Symbol.iterator]: function*(){ 
 
       //Here we know exactly what the object is, 
 
       //so issues regarding "own property" are 
 
       //non-existant 
 
       for (prop in this) 
 
        yield { key: prop, value: this[prop] }; 
 
       } 
 
      }); 
 
    
 
    //get the letter with the highest count 
 
    let highestCount = [...endLetterCount].reduce(
 
    (p, c) => c.value.count > p.value.count ? c : p 
 
); 
 
    
 
    //return some nice output format, whatever is wanted. 
 
    return `Letter "${highestCount.key}" occurred most, ` 
 
     + `${highestCount.value.count} times: ` 
 
     + `${highestCount.value.words}.`; 
 
} 
 

 
console.log(mostWordsEndsWith(sentence));

0

let sentence = 'Down by the river there is a man that quiver and shiver, but he needs to deliver a packet that he think is a big racket and a packet of of gum.'; 
 

 
let words = sentence.match(/(\w+)/g); 
 

 
let letters = {}; 
 

 
//Create an array of object containing the word and his last latter 
 
words = words.map(function(word) { 
 
    return { 
 
    word: word, 
 
    lastLetter: word.slice(-1) 
 
    } 
 
}); 
 

 
//Add the number of occurence for each letters inside the letters object 
 
words.forEach(function(elem) { 
 
    letters[elem.lastLetter] = letters[elem.lastLetter] || 0; 
 
    letters[elem.lastLetter] ++; 
 
}); 
 

 
//Sort the letters object and take the first one 
 
let mostPresentLetter = Object.keys(letters).sort(function(a, b) { 
 
    return letters[b] - letters[a] 
 
})[0]; 
 

 
//Display the most present letter 
 
console.log("Most present letter : " + mostPresentLetter); 
 
//Display all the letters. map is used to display the word only 
 
console.log("Words : " + words.filter(function(item) { 
 
    return item.lastLetter === mostPresentLetter; 
 
}).map(function(o) { 
 
    return o.word; 
 
}));

+0

@Downvoter何か説明してください?なぜそれはdownvoteが必要ですか? – Weedoze

+0

あなたのコードを見直すと、 'word.slice(-1)'はコンマのような句読点をピックアップするように見えます。なぜなら、スペースによる 'split'は実際の文字で終わることを保証しないので、良いアルゴリズムではありません単語の検出のため – arhak

+0

@arhak答えが更新されました。それは今、正規表現を使用して – Weedoze

0

古いJS

var sentence = 'Down by the river there is a man that quiver and shiver, but he needs to deliver a packet that he think is a big racket and a packet of of gum.' 
 

 
var census = {}; 
 
var most; 
 

 
sentence.match(/(\w+)\b/g).forEach(function(word) { 
 
    var last = word.substr(-1); 
 
    most = most || last; 
 
    census[last] = census[last] || []; 
 
    // a word that appears more than once is counted each time 
 
    census[last].push(word); 
 
    if (census[last].length > census[most].length) { 
 
    most = last; 
 
    } 
 
}); 
 

 
//console.log(census); 
 
console.log('most repeated letter was: ' + most + ' (' + census[most].length + ' times)'); 
 
console.log('and corresponding words were: ' + census[most]);
と互換性があり、かなり簡単な方法を、持っています3210

関連する問題