Simple Spell Checker

Hey. I'm pretty new to Java, but have to try and create a very simple spell checker. At the moment I have three classes and an interface, and I am to write a fourth and final class:
DictionaryLoader, whichprovidesastaticmethod+loadTheDictionary+which
readsthewordsfromafileandreturnsthemasanarrayofstringsinalphabetical
order.
SpellChecker, which stores the array of strings and provides a method called
check to search it. It also provides a zeroargument constructor, which calls
DictionaryLoader.loadTheDictionary.
ISpellChecker is an interface defining the check method of SpellChecker.
SpellCheckResultisaclassusedby+SpellChecker+toreturnresults.A
SpellCheckResultobjecthaspublicfieldsindicatingwhetherthewordwas
correctlyspeltand,ifnot,whatwerethewordsimmediatelybeforeandafterit.Ifit
wasbeforethefirstdictionaryentryorafterthelastonethenoneofthesefields
mightbenull.
SpellCheckerUIisaclasscontainingamainmethodwhichprovidestheuser
interfacetothesystem.
I have the code for all of these other than SpellChecker, which I have to write, except I have no clue what I'm supposed to do.
Here is the code for the four parts I have the code for:
DictionaryLoader:
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;
import java.util.Vector;
public class DictionaryLoader {
     public static final String dictFileName="/usr/share/dict/words";
     public static String[] loadTheDictionary() {
          Scanner s;
          try {
               s = new Scanner(new BufferedReader(new FileReader(dictFileName)));
          } catch (FileNotFoundException e) {
               System.err.println("Standard dictionary file "+dictFileName+" missing");
               return new String[0];
          Vector<String> dict = new Vector<String>();
          while (s.hasNext()) {
               dict.add(s.next());
          return dict.toArray(new String[0]);
}ISpellChecker:
public interface ISpellChecker {
    /* check a word w and return a SpellCheckResult object containing
       the outcome */
     public SpellCheckResult check(String w);
}SpellCheckResult:
public class SpellCheckResult {
    public boolean correct; /* true if the word was found */
    public String before;/* if the word was not found then this field
                 * contains the dictionary word before it, or
                 * null if there isn't one, else undefined */
    public String after;/* if the word was not found then this field
                * contains the dictionary word after it, or
                * null if there isn't one, else undefined */
    public SpellCheckResult(boolean result, String before, String after) {
     this.correct = result;
     this.before = before;
     this.after = after;
}SpellCheckerUI:
import java.util.Scanner;
public class SpellCheckerUI {
      * @param args
     public static void main(String[] args) {
          // TODO Auto-generated method stub
          ISpellChecker sc = new SpellChecker();
          Scanner s = new Scanner(System.in);
          System.out.println("\n\n******SIMPLE SPELL CHECKER******\n\nEnter a word you would like to check the spelling of.\nType 'quit' to exit the program.\n");
          while (true) {
               System.out.print("Word to check: ");
               String w = s.next();
               if (w.equals("quit"))
                    break;
               SpellCheckResult r = sc.check(w);
               if (r.correct)
                    System.out.println(w+" correct");
               else
                    System.out.print(w+" was not found. Nearest neighbour(s) ");
                    if (r.before != null)
                         System.out.print(r.before+" and ");
                    if (r.after != null)
                         System.out.println(r.after);
          System.out.println("Thank you for using this program. Goodbye\n\n");
}And so far, for the SpellChecker I have:
public class SpellChecker extends SpellCheckerUI implements ISpellChecker {
     public SpellChecker() {
          DictionaryLoader.loadTheDictionary();
     public SpellCheckResult check(String w) {
          return null;
     public static void main(String[] args){
}I have no idea what code to write in the SpellChecker, so if anybody can lend a hand at all, it'd be so helpful. Thank you.

tad2382 wrote:
Finding the w is a little bit trickier than it seems at first. At first, it might seem that you'd want to iterate through every entry in the array...but that would be horribly inefficient. A binary search would be better: O(log(n)) runtime instead of O(n).Only with "exact matches". But the OP would also need to find "near matches": making an ordered array with a binary search worthless. A radix tree would be the way to go.
[http://en.wikipedia.org/wiki/Radix_tree]
So basically, you'd check the middle word in the array. If it's what you want return it. If it's greater (Strings implement the comparable interface, just make sure you're ignoring case), check the lower half of the array, otherwise check the upper half. Then you'd recurse and do this for the upper/lower half of the array (your recursive method would take the min & max and look at the middle element). Your base case would be that you're left with the min & max as neighboring elements in the array (and you haven't found the word)--then you have your before & after and you're all set. Just be careful you're not skipping any entries in the array with a 1 off error.
That's the pseudocode - hopefully you can figure out the java behind it.... or just get it working in a way that is the most easiest to implement. And if need be, improve it.

Similar Messages

  • Since upgrading to ios5 spell check no longer works on my iPad 2

    I have tried turning spell check off and on again and rebooting the iPad but that did not help, what am I supposed to do? Reinstall ios5 because it took me 8 hours the first time round I'm not going through that again.

    I have not used the Support Communities before... meaning we pay our med care to answer Apple questions? And where is that answer or at least the... "we are working on it... " comment from Apple. The first post is OCTOBER how is that possible?? Did you see the last quarter Apple profits?
    Anyway I too have that problem... where is the red line? Maybe the promotion of writing errors and dumbing us down is... the goal? Obviously with the iPad "keyboard" the redline is needed and how was it left out in the first place?? Hope there is not some simple tick we are all missing... .

  • How can I enforce automatic spell checking for selected fields of data entered by form users?

    I'd like to be able to enforce spell checking of selected fields in the forms that I've created using LiveCycle Designer 8.2 at run time, as opposed to design time.  I understand the version LCD 8 has a nifty new spell checker for form designers.  But I want to spell check the data entered by users using Acrobat Reader.  And I want to enforce the spell checking automatically on selected fields only.
    Presently it seems that users filling in my forms, must know how to manually right-click on each field and select "Spell Check" from the resulting dialog box in order to check for spelling errors in the data the user has entered in the form.
    I would like to discover a way to enforce spell checking in selected fields, just as I am able to do in forms created using Acrobat Pro.  In Acrobat Pro, I can set a property for each field to require spell checking.  but that feature seems to be missing in Livecycle Designer.
    I've check the Object model for XFA forms hoping that I might find a method I can call with a Javascript, to check spelling based on an event such as onBlur.  But I haven't found a spell check method.
    Am I missing something simple?  Is there a way to set each field to be spell checked when a user is filling in the form using the free Acrobat Reader?
    Our users are not sophisticated and requiring them to spell check each field separately just won't cut it...
    Any help on this will be greatly appreciated.
    Thanks!
    -David Bartholomew

    Hi David,
    Two things...
    At design time set the locale of the form to one that Acrobat spell checks. For example English (US) and English (UK) locales have spell checkers; however English (Ireland) does not. Check the Warnings tab to see if spell checking is supported for your locale.
    If your form locale supports spell checking then you can go to the button script below.
    If you form locale does NOT support spell checking then Stephanie has a great work around to force spell checking (http://forums.adobe.com/message/2233945#2233945).
    Bring a regular button onto the form and in the click event have the following:
    app.execMenuItem("Spelling:Check Spelling");
    Which will open the spell checker for all fields.  Thanks to Paul for extracting all of the accessible menu items (http://forums.adobe.com/message/1912914#1912914).
    Good luck,
    Niall

  • Spell checking in other languages using Pages for iPad

    Does anybody know how to spell check a document in a language other than the primary language? For example the iPad is using English but I want to write a letter in German.
    This was simple to do using Pages on the Mac. So far I'm stumped.

    kennethfromtoronto wrote:
    From Settings General International. Select keyboards and add the language(s) of your choice. Tou will now have a spell checker and word predicter for the selected languages. 
    Did you actually try this with Pages?  In the past this app had a bug where switching the keyboard did not work, you had to switch the OS language, as was stated earlier in this discussion.  If that is now fixed, that is good to hear.

  • Spell check for the input text

    Hi,
    I am using ADF 11g. I have a input text to enter their comments and I need to have a spell checker. Is there any spell checker component in ADF 11g?
    Thanks
    Saru

    Hi John,
    Thanks for you reply.
    I am using ADF 11G
    Jspell version is Version 0811c
    it seems as though the data is not getting posted to the Jspell servlet.
    Following is the Request header for a simple test html that i wrote
    ===========================================
    Host     localhost:7101
    User-Agent     Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)
    Accept     text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    Accept-Language     en-us,en;q=0.5
    Accept-Encoding     gzip,deflate
    Accept-Charset     ISO-8859-1,utf-8;q=0.7,*;q=0.7
    Keep-Alive     115
    Connection     keep-alive
    Method     POST http://localhost:7101/jspellEvolution/abc HTTP/1.1
    Content-Type     application/x-www-form-urlencoded; charset=UTF-8
    Referer     http://localhost:7101/jspellEvolution/test/test.html
    Content-Length     44
    Cookie     jspellLearned=%21ghfoehdf%2C; JSESSIONID=3sjCLdTVfHbdMQty5s7r11FSkRdySGJmzKp1CT2TrKLS1J8qvSLm!1256583144
    ====================================================================================
    And following is request header of my ADF 11G application
    ======================================
    Host     127.0.0.1:7101
    User-Agent     Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)
    Accept     text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    Accept-Language     en-us,en;q=0.5
    Accept-Encoding     gzip,deflate
    Accept-Charset     ISO-8859-1,utf-8;q=0.7,*;q=0.7
    Keep-Alive     115
    Connection     keep-alive
    Content-Type     application/x-www-form-urlencoded; charset=UTF-8
    Adf-Rich-Message     true
    Referer     http://127.0.0.1:7101/TestEvent-ViewController-context-root/faces/test.jspx?_afrLoop=8177125195800&_afrWindowMode=0&_adf.ctrl-state=xqk2btm4s_9
    Content-Length     330
    Cookie     JSESSIONID=SJBfLdMSfjLXchBkwl3R3yPQv8TTVnPGZhx29k4ZcntRtLWhRj1N!1256583144
    ===============================================================
    It looks as if the spell checker servlet is not being invoked.
    Following is my .jspx page code
    <?xml version='1.0' encoding='UTF-8'?>
    <jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:af="http://xmlns.oracle.com/adf/faces/rich">
    <jsp:directive.page contentType="text/html;charset=UTF-8"/>
    <f:view>
    <af:document id="d1">
    <f:facet name="metaContainer">
    <af:group>
    <![CDATA[
    <script TYPE="text/javascript" SRC="http://localhost:7101/jspellEvolution/jspellSettings.js" CHARSET="ISO-8859-1"></script>
    <script TYPE="text/javascript" SRC="http://localhost:7101/jspellEvolution/jspellEvolution.js" CHARSET="ISO-8859-1"></script>
    <script LANGUAGE="JavaScript" TYPE="text/javascript">
         window.onload=jspellInit;
         function jspellPostInit() {
              // this function will be called when JSpell is completely done initializing and
              // attaching to existing text elements or iframe elements
         /* Implement this function to explicitly declare the */
         /* fields that you want to spell check. */
         function getSpellCheckArray()
              var fieldsToCheck=new Array();
              // Use DOM Element IDs to specify fields to spell check
              fieldsToCheck[fieldsToCheck.length]=[document,"it1::content"];
              return fieldsToCheck;
    </script>
    ]]>
    </af:group>
    </f:facet>
    <af:form id="f1" usesUpload="true">
    <af:inputText label="Label 1" id="it1" simple="true" autoSubmit="true"
    immediate="true" clientComponent="true"/>
    </af:form>
    </af:document>
    </f:view>
    </jsp:root>
    ==========================================
    Any suggestion would be greatly appriciated

  • Spell Check error

    I have been having a problem with e-mail -- that has since migrated to Safari as well. When sending a message or trying to enter a password and using a special character or carriage return I get an "Alert" that the "Spell Checker" can't be found (approx.) and the system essentially locks up.
    A search of the discussion groups attributed this to "Spell Catcher" and had a number of solutions that involved reinstalling the system.
    I used Spring Cleaning 10.0 to uninstall Spell Catcher and all linked files (extensions, etc.) and the problem appears to have been resolved.
    This is a much simpler solution.
    I am entering this here because the original discussion has been closed and no further posts were allowed over there.

    I am posting this as a service to others who may run into the same problem.

  • Spell Checker broken

    Ever since I upgraded to 10.4.5, the spell check doesnt work as well. Simple things that were caught before, like letters being transposed, are now not listed.
    Sometimes it finds the error, but doesnt have a suggestion whereas in the past it did.
    What gives?

    no answer at all, sort of like the functionality of the spell checker.

  • Emails get sent in Mail when pressing the space bar whilst spell checking

    Why do emails get sent in Mail when pressing the space bar whilst spell checking ?? This is beyond annoying. It's been happening for ages on my MBP. I have seen others complain and I can't understadn why this simple bug has not been fixed. Any help on this would be very much appreciated. thank you.

    Hi
    In composing I have Check Spelling "when I click send" selected. I tried on my iMac, my Mail account is sync'd via iCloud etc.. I got the same thing, when you hit send and review your spelling, I need to do that a lot, as well as insert missing words. As soon as you hit the space bar off goes the email. I have seen others complain of this on the google. I have now switched to check as I type but when i make a typo Mail just puts a red line under the word, it doesn't open up the Spell Checker dialog.
    Is there away of resetting my Mail Apps database ? MAybe something is corrupt.
    thanks.

  • Language packs seem to break spell-check language selection

    I suspect this is a bug, but maybe I am doing something wrong, so I'll ask here before submitting a bug report.
    I have Thunderbird 31.2.0 installed on OS X Mavericks. I am pretty sure it is the en-GB version, but I have several language packs installed, including the (presumably redundant) en-GB pack. I also have installed (and sometimes uninstalled) the add-ons "Quick Locale Switcher" and "Simple Locale Switcher" at various times to switch the user interface language. (The reason I have included the two is, I had the "Quick" switcher first, and suspected what I was seeing might be a bug in the add-on, so I switched to the other.)
    Now, the symptom. When the user interface language is set as en-GB, the spell-checker language choice in the "Preferences" menu (I guess "Options" in Windows, and I don't know what in Linux) is respected - I can type an email in one language, stop, switch the spell-checker language half way through, and the real-time spell-checking is then by the new language exactly as I would have expected. But, when the user interface language is anything other than en-GB (I haven't tried en-US, but I have tried German and Russian), the spell-check language follows the user interface language, and changing the spell-check language option in the "Preferences" menu has no effect whatsoever on anything.
    This problem persisted even after I removed the two language-switching add-ons. Then, I removed all the language packs (which forced my user interface back into en-GB), and magically the spell-check language selection started working again.
    So I am thinking the problem is driven by the user interface being from a language pack. When the user interface language matches the original install language, the spell-check language selection works; when the user interface language is something else, the spell-check language selection is ignored, and the actual spell-check language follows the user interface language.
    The "Simple Locale Switcher" does not seem to allow the user interface language and the spell-check language to be switched independently, but "Quick Locale Switcher" does. However, this feature doesn't seem to work, nor does switching the spell-check language through the Thunderbird Preferences rather than the add-on (unless the user interface language is the default en-GB).
    Has any one else seen a problem like this? Is there a way to fix it? (Maybe futz with the language settings on the other side of the "Here Be Dragons" warning?)

    Before chasing dragons, right click in the body of a mail in the compose window and set the spell check language there. I have no idea if it will work, but it might add to the over all body of knowledge for a bug report.
    Please post a link to any bug report you make.

  • Spell Check and changing multiple instances of the same word

    Hi, sorry if this has been brought up before.. I can't seem to find any reference, so here's my dilemma:
    My main writing language in Pages is Spanish and when it comes time for me to run the spell check, I usually have to change the same word a lot, because of missing accents and such. (I'd rather concentrate on writing and not worry about whether it has the right accent). Does Pages have any way to automatically detect when a word has been corrected, and pass the correction to any other instance of that word? I have tried using Find and Replace, but its not perfect, changing perfectly spelled words to include any corrections because the words, I think, appear similar. Is there a trick within Pages to do this? Does anyone know if the new version of Pages includes such a feature? Apple's iWork website and presentations fail to mention it...
    Any suggestions will be appreciated.

    I got it working, thanks es5f2000. I sat and thought about it and worked it out. (Pretty simple) Heres the code:
    public void findWord()
             x = iTab.getEditorList(iTab.getIndex());
             try
               int index = x.getDocument().getText(0, x.getDocument().getLength()).toLowerCase().indexOf(tFindText.getText().toLowerCase());
               int index2 = x.getDocument().getText(0, x.getDocument().getLength()).toLowerCase().indexOf(tFindText.getText().toLowerCase(), x.getCaretPosition());
               if (index >= 0)
                 System.out.println("found:)" + index);
                 x.requestFocus();
                 x.select(index, index + tFindText.getText().length());
                 if(x.getCaretPosition() > 0)
                   System.out.println("found:)" + index2);
                   x.requestFocus();
                   x.select(index2, index2 + tFindText.getText().length());
               else
                 System.out.println("Not found:(");
             catch(BadLocationException ie) {}
          }

  • Greek Spell Check and Auto-Correction

    Hi, I want to know if I can add a Greek dictionary that will have the same features as the English Language has on Lion.
    Why Apple didn't integrate such a simple thing. To make every user from different places in the world to have the convenience of Auto-Correction they have on iOS on OS X too. Greek Auto-Correction exist on iOS why not on Lion?

    rioshorty wrote:
    Hi, I want to know if I can add a Greek dictionary
    Yes, see
    http://m10lmac.blogspot.com/2011/06/extra-spell-checking-dictionaries-for.html
    Nobody can tell you why Apple hasn't included some feature that you want, but you can ask them for it here:
    http://www.apple.com/feedback/macosx.html

  • How to disable automatic spell checking on Firefox Mobile?

    Hello,
    I installed Firefox onto my N900, and its spell checking functionality annoys me very much. I usually type in a language that it doesn't support, and even in English it sometimes underlines words which it doesn't have in its dictionary.
    I know all I have to do is set "layout.spellcheckDefault" to 0 in about:config, but I can't do that... double clicking the setting name just zooms in and out, and there is no way to disable it (or I haven't found it) from the Settings UI.
    Finally, I did it by manually editing the following file:
    /home/opt/mozilla/fennec-1.0.0/defaults/preferences/mobile.js
    However, in my opinion, a simple solution should be presented for more casual users.
    == Extensions installed: ==
    Weave
    Adblock Plus

    Caitlin,
    Thanks for your answer! Since then, I figured out that I could change those settings in about:config by selecting them and pressing enter.
    By the way I installed the nightly Fennec build, and it seems more responsive than the Firefox-named release version. It doesn't support the Adblock plugin, so that may be reason enough to switch back to the 1.0 version.

  • OS X spell checker worthless; how can it be edited?

    The advice in Mac Help, "If you accidentally add a misspelled word to the dictionary, type the misspelling in the Spelling dialog, then click Forget" is ridiculous. How do I know how I misspelled it? I know I have misspelled words in my dictionary; the longer I use my spell checker, the less effective it becomes.
    Is there any way of actually seeing the list of words I've added? We used to be able to do that with a third-party spellchecker (I think Spell Catcher?), and editing out errors was simple.
    Example: "cstcher" is accepted as correct. Selecting it, going to Edit > Spelling > Spelling, 'guess' field is grayed out; writing "cstcher" in correct field activates Guess, clicking Guess produces nothing, clicking Forget seems to accomplish nothing; "cstcher" is still accepted as correct. "rediculous" is accepted as correct and does not produce any guesses unless I add it into the guess field.
    I need a spell checker that works! Most of the words that I misspell are not identified as misspelled.

    This is what I actually wrote; reply evidently corrupted three times in both Safari and Firefox. I’ve restarted; hope this posts.
    Francine, thank you. I opened ‘en’ with TextEdit, and there were only about 50 words (impossible to count accurately), and except for ‘√©s‘, and ‘
    So this solved my first problem: how to edit it. Surely an inferior editor.
    But I’ve still got the spell checker malfunctioning...
    Oh well, I did upgrade to Spell Catcher X 10.2.2, as Matthew Whiting first suggested; SC X seems better now than it was when I quit using it a couple of years ago. Perhaps between the two... But why Apple’s spell checker doesn’t inform me of misspellings is still a mystery. The words I’d mentioned (cstcher - rediculous) are not in the ‘en’ file, and interestingly enough, they both have red lines under them in this post...
    Thanks for taking the time to find tis answer for me... (Hey, know what? I just spelled ‘this’ as ‘tis’, and no red underline. And it’s not in the dictionary or the ‘en’ file...)
    And Spell Checker didn’t catch it either. Says it is correct, in the Proximity/Franklin U.S. English Thesaurus, so I unchecked that reference.
    Daru

  • How do you change the Spell Check language

    I have a question, how do you change the spell check language so that it spell checks in a different language... such as French?

    Here is a copy of an e-mail that I sent to Yvan asking about this (translated from French):
    +Hello Yvan,+
    +I saw your answer SirCram's message (http://discussions.apple.com/thread.jspa?messageID=7526551&#7526551) in which you showed a dialogue box called "text"+
    +Sadly, I have only recently started using a Mac (and I am therefore unfamiliar with Pages) so I find no way to open said box.+
    +Could you please make me a list of instructions or show me an Apple Support page that would assist this recent Mac convert in learning this simple action?+
    +Thank-you in advance,+
    +Jean-Paul J+

  • Spell check option to skip master pages

    I'm still running CS3 so perhaps this has already been solved for but I need a spell check option to ignore the master pages. I'm a magazine designer and our master pages contain placeholder text so we can work our layouts before we have final copy. Problem is we can never run an effective spell check because there is no option to ignore the master pages so a spell check tries to check the lorum ipsum text rendering it useless. It would be wildy helpful if in the search dropdown option you could select "no masters" or something of the sort. Similar to the option in the Find/Change search dropdown, where you can choose "story" instead of "document"; in fact, this same functionality in Find/Change would be great as well. Also, "Story" is a good option, the whole document minus the master pages would be even better. That way we could check all of the active text in the document, including folios, rubrics and captions that are divided from the story.
    Let me know if I can explain this better.

    Spring Step Creative wrote:
    Peter,
    I'm familiar with the workaround. Trouble is when you place or paste 
    your text in later, you have to remember to take it off of "no 
    language" or it will continue to ignore the text. We're dealing with 
    editors here, with very quick turnarounds, and the whole idea is not 
    to make a mistake. Just think how easy it would be to forget to turn 
    off "no language", run a spell check and think you're good to go to 
    press. ... and don't say InCopy has the solution, because its not a 
    useful product for us; it's cumbersome and doesn't serve us well at 
    all. I really think adding functionality that would allow you to 
    simply filter out the master pages in a spell check would be an 
    excellent help.
    Have you tried creating customized placeholder text, something like "Replace this placeholder text with real text and tag the replacement text with such-and-such paragraph style." instead of lorem ipsum? You do this by creating a text file named "placeholder.txt" that contains your customized text, and saving it in the main InDesign directory. When you perform Type > Fill With Placeholder Text, the customized text appears with instructions.
    You can create object styles for the placeholder text frames that apply the no-language paragraph style. You can make the no-language paragraph style a color, bold, etc, to call even more attention to it.
    A simpler option would be to assure that all the text in your customized placeholder.text file is spelled correctly, so it doesn't get a hit when spell checking.
    Being able to omit Master pages from spell checks is a good idea. Would you want it to be a scope option in the spell checker, or a property of a master page? If it were a master page property, you could specify which master pages are skipped. You can enter a formal feature request at
    https://www.adobe.com/cfusion/mmform/index.cfm?name=wishform.
    HTH
    Regards,
    Peter
    Peter Gold
    KnowHow ProServices

Maybe you are looking for