Java Tutorial: How To Create Words Correction Methods Like Google Translate On Netbeans Using Levenshtein Algorithm

in #utopian-io7 years ago (edited)

What Will I Learn?

  • You will learn how to create dictionary method whit java programming
  • You will learn how make a recommendation words method for an app
  • You will learn how to connect the two programs in one java package

Requirements

  • you must understand about the basic of java programming
  • you must know about OOP (object oriented programming)
  • you must know how to implement FOR looping in java programming

Difficulty

  • Intermediate

Tutorial Contents

okay this time I will share a tutorial how to create dictionary method or recommendation word like in google translate using FOR looping on java using levenshtein algorithm. here I will only make the basic method only.
okay we just go into the process:

  • first you have to create a new project on netbeans.
    image.png

  • then create a new class in the project.
    image.png

  • then type the following program code in the class you have created.
package levenshtein;
public class LevenshteinDistance {
    public static int distance(String s1, String s2)
    {
        int edits[][]=new int[s1.length()+1][s2.length()+1];
        for(int i=0;i<=s1.length();i++)
            edits[i][0]=i;
        for(int j=1;j<=s2.length();j++)
            edits[0][j]=j;
        for (int i=1;i<=s1.length();i++){
            for(int j=1;j<=s2.length();j++){
                int u=(s1.charAt(i-1)==s2.charAt(j-1)?0:1);
                edits[i][j]=Math.min(
                        edits[i-1][j]+1,Math.min(
                        edits[i][j-1]+1,
                        edits[i-1][j-1]+u
                        )
                        );
}
} 
    return edits[s1.length()][s2.length()];}
}
  • okay, I explain a little about the above program.
  • this is a package name declaration package levenshtein; equate to the package name that has been and abuat before.
  • this is the process of declaring the class name public class LevenshteinDistance later we will call this class in another class.
  • this is a mandatory element that must exist in java programming public static int distance (String s1, String s2). public means this class can be accessed on other classes that are in one package.
  • this is a two dimensional array declaration process int edits[][]=new int[s1.length()+1][s2.length()+1];.
  • then this is the main process that is the process of looping that will later mengulan a process with certain criteria as below.
for(int i=0;i<=s1.length();i++)
            edits[i][0]=i;
  • this part is for the process of declaration of u variable in which there is a mathematical operation.
int u=(s1.charAt(i-1)==s2.charAt(j-1)?0:1);
                edits[i][j]=Math.min(
                        edits[i-1][j]+1,Math.min(
                        edits[i][j-1]+1,
                        edits[i-1][j-1]+u)
  • return edits[s1.length()][s2.length()];} used when a method has a return value.
  • okay, then create another class with the name LevDis in the same package then type the code below.
package levenshtein;
public class LevDis {
    public  static void main (String args[])
    {
        LevenshteinDistance id=new LevenshteinDistance();
        String s1="stand";
        String s2="stan";
        int different= id.distance(s1,s2);
        System.out.print("different"+different);
    }
}
  • this is the example of manual procces:
    image.png

  • okay, the program above functions interconnected with the previous program, in this program we will fill in two strings that will be compared and will be determined how the differences in characters contained in both strings.

  • okay, both programs has been created, then we will run it.

  • enter two strings that are almost identical to find the difference.
    image.png


  • we only need to run the second program is the program LevDis, right click on the program LevDis then select run file.
    image.png

  • then we see the output.

    image.png

    okay, in the picture above only found one difference, you can see the difference between stand with stan word

    image.png

    this is an example of the implementation that I took on google translate

CONCLUSION:

with the LEVENSHTEIN algorithm we can create a method like that used on google translate is to check the word entered then if the word entered does not match the existing word in the database then the system will recommend another word similar to the word that we input, the way is the word we have input will be matched with the words in the database using the FOR looping process.

Curriculum



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

it is very interesting

Your contribution cannot be approved because it does not follow the Utopian Rules/Guidelines.

Violated Rule(s)/Guideline(s):

  • The writing style should be formal and informative.

  • The writing style should be professional and clear.

  • Submissions presenting content creation and simple on-screen instruction will be rejected.

  • Submissions containing substantial instruction in ubiquitous functions (Save, Open, Print, etc.) or basic programming concepts (variables, operators, loops, etc.) will be rejected.

My Opinion(s):

  • As it's stated in the rules which I marked above, this tutorial contains basic programming concepts, tutorial's writing style is informal, unprofessional and unclear; the concept is the simple on-screen content creation.

Need help? Write a ticket on https://support.utopian.io.

Chat with us on Discord.

[utopian-moderator]