cs 240 week 2
DESCRIPTION
CS 240 Week 2. Introduction to Project 2. Command Line Syntax java Spell word -- source file hard wired into program Run Demo. Basic Algorithm. Look up word in dictionary Comparison is case insensitive If found List the word Else if a similar word is found - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: CS 240 Week 2](https://reader035.vdocument.in/reader035/viewer/2022072013/56812bc8550346895d901658/html5/thumbnails/1.jpg)
CS 240Week 2
![Page 2: CS 240 Week 2](https://reader035.vdocument.in/reader035/viewer/2022072013/56812bc8550346895d901658/html5/thumbnails/2.jpg)
Introduction to Project 2
• Command Line Syntaxjava Spell word
-- source file hard wired into program• Run Demo
![Page 3: CS 240 Week 2](https://reader035.vdocument.in/reader035/viewer/2022072013/56812bc8550346895d901658/html5/thumbnails/3.jpg)
Basic Algorithm
• Look up word in dictionary– Comparison is case insensitive
• If foundList the word
Else if a similar word is foundList the most similar word
ElseIndicate no word found
• See project description for syntax• The input word must consist of 1 or more letters
![Page 4: CS 240 Week 2](https://reader035.vdocument.in/reader035/viewer/2022072013/56812bc8550346895d901658/html5/thumbnails/4.jpg)
Dictionary Must Be Represented as Trie
• A tree like structure defined recursively– Root node– Internal nodes (sub-Tries)
• Leaf nodes have no sub-Tries
• Each node contains– A count
• A value > 0 represents the number times a word in the dictionary (represented by a path through the tree to the current node) appears in the dictionary.
– 26 Sub-Tries having the values ‘a’..’z’• The Trie (or root of the Trie) contains
– The number of words in the dictionary– The number of nodes in the dictionary
![Page 5: CS 240 Week 2](https://reader035.vdocument.in/reader035/viewer/2022072013/56812bc8550346895d901658/html5/thumbnails/5.jpg)
Trie Operations
• Constructor• Methods– add(String word)– find(String word); may return null– int getWordCount()– Int getNodeCount()
![Page 6: CS 240 Week 2](https://reader035.vdocument.in/reader035/viewer/2022072013/56812bc8550346895d901658/html5/thumbnails/6.jpg)
What Does It Mean for a Word to Be Similar to Another Word
• Edit Distance– Deletion distance– Transposition distance– Alteration distance– Insertion distance
• Edit Distance 1 and 2
![Page 7: CS 240 Week 2](https://reader035.vdocument.in/reader035/viewer/2022072013/56812bc8550346895d901658/html5/thumbnails/7.jpg)
Making the Output Deterministic
• One word is “more similar” than another wordBased on the following priorities1. A word that is has an edit distance of one has a higher
priority than one that is has an edit distance of 22. If both words have the same edit distance then select
the one that appears more frequently in the dictionary3. If two words appear equally often in the dictionary
then pick the one the is alphabetically first.
![Page 8: CS 240 Week 2](https://reader035.vdocument.in/reader035/viewer/2022072013/56812bc8550346895d901658/html5/thumbnails/8.jpg)
Additional String Operations
• s.toLowerCase()• s.toUpperCase()• StringBuilder– Building strings using “+” is expensive– StringBuilder strBuilder = new StringBuilder()
• strBuilder.append(String)– Can be anything that has a toString method or atomic type
• strBuilder.delete(int, int)• strBuilder.setCharAt(int,char)
• Code Examples
![Page 9: CS 240 Week 2](https://reader035.vdocument.in/reader035/viewer/2022072013/56812bc8550346895d901658/html5/thumbnails/9.jpg)
Interfaces
• Interaction contract– At any place in a java program you an have variables of the
interface type– Object in variables must be instance of an implementing class
• Keyword “implements”• Like .h file in concept (conceptually similar to abstract
class)• Contains
– Constants (static and final)– Method signatures
• You must provide an implementing class
![Page 10: CS 240 Week 2](https://reader035.vdocument.in/reader035/viewer/2022072013/56812bc8550346895d901658/html5/thumbnails/10.jpg)
Interface Example
• StudentListInterface.java: The interface– ArrayStudentList.java: implements
StudentListInterface.java– ArrayListStudentList.java: implements
StudentListInterface.java• TestingStudentListInterface.java
![Page 11: CS 240 Week 2](https://reader035.vdocument.in/reader035/viewer/2022072013/56812bc8550346895d901658/html5/thumbnails/11.jpg)
Interesting Addition of For Loops
• for(String s = “”; s != null; s = f.readLine()) …• Use of finally• Example Code– testFile – put in same directory as Example Code