19
I have a text inside a StringBuffer
and I need to check and mark the words that appear more than once. At first I used a circular row of 10 positions, because I am interested only repeated words in a "radius" of 10 words.
It is worth noting that the marking of repeated words can only occur if the repeated words are within a radius of 10 words between them. If the repeated words are within a "distance" of more than 10 words, they should not be marked.
The method Contem
returns null
if there is no repetition or returns the word that has repetition.
String
is only the variable containing the full text.
StringBuffer stringProximas = new StringBuffer();
String has = "";
Pattern pR = Pattern.compile("[a-zA-Zà-úÀ-Ú]+");
Matcher mR = pR.matcher(string);
while(mR.find()){
word = mR.group();
nextWord.Inserir(word);//inserir na lista
has = nextWord.Contem();//verifica se há palavras iguais na lista
//um if pra verificar se has é null ou nao
//e aqui marca a palavra repetida, se has for diferente de null
mR.appendReplacement(stringProximas, "");
stringProximas.append(has);
}
public void Inserir(String palavra){
if(this.list[9].equals("null")){
if(this.list[0].equals("null")){
this.list[this.fim]=palavra;
}else{
this.fim++;
this.list[this.fim] = palavra;
}
}else{
//inverte o apontador fim para a posição 0
if(this.inicio == 0 && this.fim == 9){
this.inicio++;
this.fim = 0;
this.list[this.fim] = palavra;
}else if(this.inicio == 9 && this.fim == 8){//inverte o apontador inicio para posição 0
this.inicio = 0;
this.fim++;
this.list[this.fim] = palavra;
}else{
this.inicio++;
this.fim++;
this.list[this.fim] = palavra;
}
}
}
public String Contem() throws Exception{
for(int i=0;i<this.list.length;i++){
for(int j=i+1;j<this.list.length;j++){
if(this.list[i].equals(this.list[j]) && (!this.list[i].equals("null") || !this.list[j].equals("null"))){
//nao pegar a mesma repetição mais de uma vez
if(!this.list[i].equals("?")){
this.list[i] = "?";//provavelmente será retirado isso
return this.list[j];
}
}
}
}
return "null";
}
My big problem: if I find repeated words, I can only mark the second occurrence because even the first one is in the queue, the variable word
will be the second and because while
I can’t make the second.
I’m using this text as an example:
Nowadays, it is necessary to be smart. Our day to day is complicated.
The method should return for example (put as bold here, but not necessarily the way to mark):
Today in day, is need to be smart. Our day to day is complicated.
You can provide some code on the methods
Inserir
andContem
and on the variablesstring
,stringProximas
andhas
so we can try to replicate the workings of your code to help you?– Victor Stafusa
I edited it, check it out @Victor
– Pacíficão
How do you initialize the variable
list
? I don’t think so"null"
with quotes and that"?"
.– Victor Stafusa
@Victor this is only for test case. It does not influence much and will not be the definitive. Already the
list
is started in the class thus:private String list[];
, recalling that size 10.– Pacíficão
take a look at the issue I posted, I made some edits and now I think it’s the way you need it!
– Leonardo Bosquett