Fahim Parkar
Fahim Parkar

Reputation: 31647

Check content in array is word or not

Below is what I have in my Array...

myArray = {"about","name","dsafasdf","fix"};

I want to find what are English words in this array.

Below should be output:

Words found are as below
about
name
fix

Thanks in advance!!!

Any example or link would work!!!

Actually I want to implement TextTwist Game. I have found possible words, however I would like to check whether the String found is WORD/ Grammar or not...

Update 1

Please don't advice me to create a file and put words in it and then search word in this file... It will be the WORSTEN program....

Upvotes: 0

Views: 1004

Answers (6)

fangsterr
fangsterr

Reputation: 3820

I've implemented a version of TextTwist in java myself, and I've found that reading in from a dictionary text file into a Set of String's works great.

Here's my code, a Java Eclipse project, in case you're interested. Note that I implemented it with multiplayer functionality in mind so the code is split between client/server. https://github.com/fangsterr/Multiplayer-Text-Twist

Upvotes: 0

amit
amit

Reputation: 178451

Instead of working with a static collection of words as suggested in other answers, I would use something much more dynamic - the web.

A good heuristic could be - search if the word you are seeking appears in a title of an article in wikipedia, and accept it if it does!

Note that the advantage is a dynamic growing "list" of words, without the need to store them in a dictionary.

Disadvantages: slow IO [constant internet usage], and the list is yet not full [some terms do not appear, even in wikipedia]. It also requires the user to be on-line to use this approach.

Have a look at wikipedia API to understand how to do it.

Another on-line source of information you can use is Bing Search API [which is free! though has some problems lately...]

Upvotes: 1

shift66
shift66

Reputation: 11958

You need a library with all english words. And you have to check every word.
And this is a similar question. And if you don't want to use a java library you should find a text file containing all words or something like that and write your own method to find a word. Note that your text file should be sorted so you could find word with divide and conquer algorithm. Otherwise searching will take very long time.
EDIT:
And you also have to remember that names are not "English words" as says @amit. An they can meet everywhere in text. You should check if word starts with upper case letter and isn't on the start of sentence.

Upvotes: 2

Savino Sguera
Savino Sguera

Reputation: 3572

You could lookup in a dictionary, or you can use specific libraries for that: have a look at How to check if a word is an English word with Python?

Upvotes: 0

Nick Garvey
Nick Garvey

Reputation: 3000

You will need to read in an English Library file and check against that. An example of such a file can be found here: http://wordlist.sourceforge.net/

Upvotes: 1

Urs Reupke
Urs Reupke

Reputation: 6921

First: Define your dictionary of english words.
Then: Put all those worlds in a Collection.
Finally: For each word in your array, check whether it is in the collection of english words.

This is not excessively performant, but it should do the job:

String[] englishWords = new String[]{"a", "all", "an",...};
Collection<String> dictionary = Arrays.asList(englishWords);
for (String candidate : myArray){
  if (dictionary.contains(candidate)){
    System.out.println(candidate);
  }
}

Upvotes: 0

Related Questions