Question: Submit Levenshtein .java Write a program that computes the edit distance (also called the Levenshtein distance) between two words. The edit distance between two strings

Submit Levenshtein.java

Write a program that computes the edit distance (also called the Levenshtein distance) between two words. The edit distance between two strings is the minimum number of operations that are needed to transform one string into the other. For this program, an operation is a substitution of a single character, such as from brisk to brick. The edit distance between the words dog and cat is 3, following the chain of dot, cot, and cat to transform dog into cat. When you compute the edit distance between two words, each intermediate word must be an actual valid word. Edit distances are useful in applications that need to determine how similar two strings are, such as spelling checkers.

Read your input from a test.txt file or a dictionary.text file. From this file, compute a map from every word to its immediate neighbors, that is, the words that have an edit distance of 1 from it. Once this map is built, you can walk it to find paths from one word to another.

A good way to process paths to walk the neighbor map is to use a linked list of words to visit, starting with the beginning word, such as dog. Your algorithm should repeatedly remove the front word of the list and add all of its neighbors to the end of the list, until the ending word (such as cat) is found or until the list becomes empty, which indicates that no path exists between the two words.

Your Levenshtein class has to have a private Map> field so the following test code works:

Levenshtein.java

// section of code to read data file Scanner file = new Scanner(new File("test.txt")); ArrayList words = new ArrayList(); while (file.hasNext()) words.add(file.next()); file.close(); // section of code to test your data structure Levenshtein structure = new Levenshtein(words); // builds Map from List of words System.out.println(structure.getMap()); // displays the Map from above System.out.println(structure.getMap().size()); // size of above Map System.out.println(structure.getPath("dog","cat")); // displays path as described in text System.out.println(structure.getDistance("dog","cat")); // the "distance" as described in text

test.txt

dog dot cot cat fat mat rat rut

Submit Levenshtein.java Write a program that computes the edit distance (also called

The first line is the whole Map, which has a size of 8 key items (second line).

And the third line is the "path of how to change "dog" into a "cat" which is 3 steps.

Additional considerations:

  • If we pass two words with different lengths, then define path as -1 and an empty path
  • getPath should return a List
  • getDistance returns an int that is one less than the size from getPath
  • Above test is with test.txtthe Levenshtein distance) between two words. The edit distance between two strings but I will use dictionary.txtis the minimum number of operations that are needed to transform one for my final testing, results will vary
  • Using dictionary.txtstring into the other. For this program, an operation is a substitution in above example, you should get a Map size of 19911 and path:
    [dog, cog, cot, cat]

Transcribed image text

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!