Tengo una pregunta que me pide:
Limpia el siguiente texto y encuentra la palabra más frecuente: " const sentence = '%I $am@% a %tea@cher%, &and& I lo%#ve %te@a@ching%;. The@re $is no@th@ing; &as& mo@re rewarding as educa@ting &and& @emp%o@weri@ng peo@ple. ;I found tea@ching m%o@re interesting tha@n any ot#her %jo@bs. %Do@es thi%s mo@tiv#ate yo@u to be a tea@cher!? %Th#is 30#Days&OfJavaScript &is al@so $the $resu@lt of &love& of tea&ching' "
Usé regEx para limpiar la cadena de esta manera:
const sentence = '%I $am@% a %tea@cher%, &and& I lo%#ve %te@a@ching%;. The@re $is no@th@ing; && mo@re rewarding than educa@ting &and& @emp%o@weri@ng peo@ple. ;I find tea@ching m%o@re interesting tha@n any ot#her %jo@bs. %Do@es thi%s mo@tiv#ate yo@u to be a tea@cher!? %Th#is 30#Days&OfJavaScript &is al@so $the $resu@lt of &love& of tea&ching'; const sentReg = /\W(?<!1)/g; let sent = sentence.replace(/ /g, "1"); let finalSent = sent.replace(sentReg, ""), finalfinalSent = finalSent.replace(/1/g, " "); Me di cuenta de que no sé cómo usar la función match() para buscar una cadena por palabra (o puede que no haya forma de hacerlo), así que intenté dividirla en una matriz como:
let senArr = finalfinalSent.split(" "), wordOccur = []; for (const x of senArr) { var re = new RegExp(x, "g"); var y = finalfinalSent.match(re); wordOccur = wordOccur.concat([y.length]); };... y ahora estoy atascado porque no sé cómo buscar una matriz en JavaScript, solo en Python, y siento que la forma de buscar en una cadena sería mucho más fácil que esto. Agradecería algunos consejos.
No cambiaría los espacios a "1". En su lugar, use una expresión regular que no elimine espacios durante la limpieza.
Luego puede llamar a match en la cadena limpia y usar reduce para comenzar a contar palabras y mantener una referencia a la más frecuente:
const sentence = '%I $am@% a %tea@cher%, &and& I lo%#ve %te@a@ching%;. The@re $is no@th@ing; && mo@re rewarding than educa@ting &and& @emp%o@weri@ng peo@ple. ;I find tea@ching m%o@re interesting tha@n any ot#her %jo@bs. %Do@es thi%s mo@tiv#ate yo@u to be a tea@cher!? %Th#is 30#Days&OfJavaScript &is al@so $the $resu@lt of &love& of tea&ching'; let word = sentence.replace(/[^\w\s]/g, "") .match(/\w+/g) .reduce((acc, word) => { acc[word] = (acc[word] || 0) + 1; if (!(acc[word] < acc[acc.$])) acc.$ = word; return acc; }, {}).$; console.log(word); Observe que acc será un "diccionario" de palabras, donde los valores correspondientes son las cuentas. Se crea una entrada $ especial en ese mismo diccionario, que contendrá la palabra más frecuente.
Si hay más de una palabra que tiene la frecuencia máxima y desea obtener todas estas palabras, no solo una, devuelva una matriz en lugar de una cadena:
const sentence = '%I $am@% a %tea@cher%, &and& I lo%#ve %te@a@ching%;. The@re $is no@th@ing; && mo@re rewarding than educa@ting &and& @emp%o@weri@ng peo@ple. ;I find tea@ching m%o@re interesting tha@n any ot#her %jo@bs. %Do@es thi%s mo@tiv#ate yo@u to be a tea@cher!? %Th#is 30#Days&OfJavaScript &is al@so $the $resu@lt of &love& of tea&ching'; let word = sentence.replace(/[^\w\s]/g, "") .match(/\w+/g) .reduce((acc, word) => { acc[word] = (acc[word] || 0) + 1; if (!(acc[word] <= acc[acc.$])) acc.$ = [word]; else if (acc[word] === acc[acc.$]) acc.$.push(word); return acc; }, {}).$; console.log(word);