• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

efficient way to code a spell checker

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the arabic language, sometimes you need to draw two tiny forward slashes on top of or under letters. For example, the word also in arabic has these slashes on top of the o so also//

now i want to write a spell checker in java that looks into things like this.

method 1:
- use a 2D array where column1 has all the words that require the slashes but don't include them in it so index00 will be also and column2 has the same words but with the slashes in the appropriate places so index01 will be also//
- use a for loop to go through each line where in each line, we use stringtokenizer to go thru each word comparing it to the words in column1. Once you find it, replace it with column2.

method 2: same 2D array but first you use a regular expression. i create one that has all the words in column1. then i write if (m.find()) change found to true (default false) and exit. then i check if found is true. if it is, i use stringtokenizer. you know the rest


I'm looking for the efficient way to do this as the title says.

Pros of method2 (Yes im starting with 2) using regular expressions saves you the trouble of having to go through the for loop as some lines might be correct the Big O notation of using a regular expressions is at least O(n) just like the while loop which means that it's efficient because a while loop with a for loop in it is On squared

Cons of method2 it might be efficient but the lines and their spelling depends on the user writing them so you might find that you're not really saving much time using this method

As for method1, there's not much to say. Well it depends on your opinion of the pros and cons of method2. You might surprise me and tell me that the notation of regular expressions is worse than O(n) so I might as well stick to the for loop. Only O(n) and I will be entering the for loop anyways.

What do you think?
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do I think? I think that speculating about the performance of code you haven't written yet is not an especially useful thing to do. And I think that the phrase "performance of regular expressions" is a particularly good example of why it isn't useful. Regular expressions covers a vast amount of territory, and trying to claim that everything in that territory has the similar performance (compared to nothing in particular) is just wrong.
 
Leo Max
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How am I supposed to test it? Write a timer function and see how long it takes? And I didn't claim anything. But from your response, I'm going to go with method 1. Thank you.
 
Paul Clapham
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Leo Max wrote:How am I supposed to test it? Write a timer function and see how long it takes?



Yes, that's right, if your concern for "efficiency" involves how long it takes for the code to run, then that would be the natural way to find out how long it takes for the code to run.
 
It's just like a fortune cookie, but instead of a cookie, it's pie. And we'll call it ... tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic