Word most often in a string from a size

Asked

Viewed 115 times

1

I’m trying to solve a problem, where I need to see the word more often in a String. The problem is that these words are assembled from a given size. For example:

"5tardenoitemanhãtardenoitetarde"

I would need to take the first number, which is the length of the words I need to search in the string that comes after that number, and return the word more often.

tarde
noite
manhã
tarde
noite
tarde

In that case it would be the word "late".

Would anyone have any idea how they could do, in the most "clean" way possible, plus have a check if there are no repeated words?

  • 1

    What have you tested?

  • It’s the trivial, it doesn’t have much to do, or move through the list of matching words or use regular expressions.

  • 1

    @Augustovasques is not so trivial :)

  • @Sergio trivial what I say is not to abbreviate the code using algorithms that generate shortcuts in similar problems with for example Ichtenstein distance to evaluate similarities between strings.

  • @Augustovasques I’m going to give an answer, I thought I could use a type regex /(\d+)(\D{\1})+ but the capture group does not get what it was looking for. If you have another idea of a simpler answer, also put!

  • @Sergio I’m just writing an answer, but it won’t come out much different than what you did.

Show 1 more comment

2 answers

2

This takes several steps...

I leave a suggestion. Basically:

  • separates the numerical part of the rest
  • interprets the value of the number
  • creates a regex to catch the expected length
  • creates an object with a counter
  • uses a reduce to know which word is most used...

const string = "5tardenoitemanhãtardenoitetarde";
const numericPart = string.match(/\d+/)[0];
const rest = string.slice(numericPart.length);
const nrs = Number(numericPart);
const regex = new RegExp(`\\D{${nrs}}`, 'g');
const words = rest.match(regex);
const count = words.reduce((obj, word) => {
  if (!obj[word]) obj[word] = 0;
  obj[word]++;
  return obj;
}, {});

const max = Object.keys(count).reduce((mostCommonWord, word) => {
  if (!mostCommonWord) return word;
  return (count[mostCommonWord] < count[word]) ? word : mostCommonWord;
}, null);

console.log('Mais usada:', max);
console.log('Todas:', count);

1

I only got the amount of letters per word using the regular expression ^\d+ that searches for one or more digits that start the phrase and then removed the input digits with Slice().

I created a regular expression based on the size previously obtained and then used it to fracture the input into fixed-sized words.

So he just cataloged and counted the words and then checked which one came up more.

let str = "5tardenoitemanhãtardenoitetarde";

let tamanho = str.match(/^\d+/).join();       //Obtem a quantidade de letras
str = str.slice(tamanho.length);              //Remove a parte numérica
let reg = new RegExp(`.{1,${tamanho}}`, "g");
let palavras = str.match(reg);                //Quebra a string em palavras do mesmo tamanho

let resultados = {};

//Conta a ocorrencia de cada palavra
palavras.map((palavra) => {
  if (resultados[palavra] == null) resultados[palavra] = 0; //Se a palavra ainda não foi catalogada inicializa seu contador
  resultados[palavra] = resultados[palavra] + 1;            //A cada ocorrência incrementa o contador da palavra 
});

let maximo = {
  palavra: "",
  ocorrencias: 0
};

//Obtém a palavra que mais se repetiu.
Object.entries(resultados).forEach((palavra) => {
  if (palavra[1] > maximo.ocorrencias) maximo = {
    palavra: palavra[0],
    ocorrencias: palavra[1]
  };
});

console.log("============ ocorrências ============");
console.log(resultados);
console.log("========= maior ocorrência ==========");
console.log(maximo);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.