java - Basically I want to replace the unwanted repeated characters words with their correctly spelled words -


i want take text file input , read words , check each word englishwordslist , if words misspelled (like unwanted repeated characters)then replace word correct word in same file.

if word beautifullllll code works fine..it writes beautiful file if word beautttifulll code not work properly.first i'm removing repeated characters in word cross-checking wordslist if not present allowing 1 successive repetition of character.since beautttifulll has more 1 character repeated output beauttifull not in dictionary.

please me this.

import java.io.*;  public class ownspell { public static string result(string input, boolean doubleletter){     string pattern = null;     if(doubleletter) pattern = "(.)(?=\\1{2})";     else pattern = "(.)(?=\\1)"; return input.replaceall(pattern, ""); } public static int checkingdic(string word) throws ioexception,filenotfoundexception {       fileinputstream fstream=new fileinputstream("g:/englishwordslist.txt");       datainputstream in = new datainputstream(fstream);       bufferedreader br = new bufferedreader(new inputstreamreader(in));       string sample=" ";       word.tolowercase();       int flag=1;       while((sample=br.readline())!=null && flag==1)        {           if(sample.equalsignorecase(word))            {                 flag=0;            }        }       fstream.close();       return flag; } public static void addtofile(string old,string newv) throws ioexception , filenotfoundexception {      file file = new file("g:/1.txt");     bufferedreader reader = new bufferedreader(new filereader(file));     string sample = "", oldtext = "";     while((sample = reader.readline()) != null)         {         oldtext += sample + "\r\n";     }     reader.close();     // replace word in file     string newtext = oldtext.replaceall(old, newv);        filewriter writer = new filewriter("g:/1.txt");     writer.write(newtext);writer.close();  } public static void main(string[] args) throws ioexception , filenotfoundexception {     try{          fileinputstream f1stream=new fileinputstream("g:/1.txt");         datainputstream in1 = new datainputstream(f1stream);         bufferedreader br1 = new bufferedreader(new    inputstreamreader(in1));         string sample1=" ";         while((sample1=br1.readline())!=null)         {             string a[]=sample1.split(" ");             for(int i=0;i<a.length;i++)             {                 int flag;             if(a[i].length()>0)             {                 flag=checkingdic(a[i].tolowercase());                 if(flag==0)                {                    system.out.println(a[i]+" :word found");                }                else                {                   // system.out.println(a[i]+" :word not found");                    string output=result(a[i],false);                    flag=checkingdic(output.tolowercase());                    if(flag==1)                    {                       output=result(a[i],true);                       flag=checkingdic(output.tolowercase());                       if(flag==0)                       {                            addtofile(a[i],output);                            system.out.println(a[i]+" :word found");                       }                       else                       {                             system.out.println(a[i]+" :word not found");                       }                    }                          else                    {                       addtofile(a[i],output);                       system.out.println(a[i]+" :word found");                    }                    }                }                         }             }        f1stream.close();     }     catch(exception e)     {         system.out.println("exception");     }   } } 

ex :

1.txt : boook beautttifullll

desired output : book beautiful

output code gives : book beautttifullll

it doesnot correct word beautttifullll because function result gives beauttifull output since not there in wordslist..the word not corrected.

interesting task... did notice code contains

    word.tolowercase(); 

i think want

    word = word.tolowercase(); 

to normalize both text strings comparison.

you use pattern/matcher make text changes (compiling pattern in class rather @ runtime), or iterate on each substring character - duplicates character @ position x equals 1 @ x+1 - can tell remove character.


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -