Static spell checking behavior - android

I am trying to implement behavior similar to spell check, however not exactly same. By static, I mean only on certain words.
So let's say I have an array of 10 strings.
String[] originalWords = new String[] {
"Apple", "Banana", "Orange", "Pear", "Grape",
"Pineapple", "Lemon", "Mango", "Cherry", "Peach"
};
Now, I have an EditText where user can type in something. After user finishes typing (they press next, etc.), I want to perform a check on what they typed in.
String userInput = String.valueOf(editText.getText()).trim();
Let's say userInput value is appla. That means what user typed in is only one letter off the world Apple (which is in the array).
That's the check I want to implement. Whatever the user types in, if one SIMILAR (1 or 2 letters off) word exists in the array, I want to get that word. How would I go about implementing that?
Result examples:
aoole ==> Apple
orenge ==> Orange
Cheery ==> Cherry

I was able to figure this out. It doesn't go exactly the way I wanted (1 or 2 letters off) but it does the job because it gives the of percentage of similarity.
I used this library.
// array of original words to compare against
String[] originalWords = new String[] {
"Apple", "Banana", "Orange", "Pear", "Grape",
"Pineapple", "Lemon", "Mango", "Cherry", "Peach"
};
// convert String array to ArrayList
ArrayList<String> originals = (ArrayList<String>) Arrays.asList(originalWords);
ExtractedResult extractedResult = FuzzySearch.extractOne("aoole", originals);
if(extractedResult.getScore() >= 75) {
// the word provided (aoole) is at least 75% similar to one of the original words.
// To get the original word:
String result = extractedResult.getString();
}

Related

Search for number in array, Android

I'm searching for numbers in my app, but it does not work, where is the problem please ?!
numrat is int[] and fint is a number from fytyratint[]...
int fint = fytyratint[nrRendor];
int nse = Arrays.binarySearch(numrat, fint);
if (nse <0 ){
pS++;
tvS.setText("Sakt: "+ Integer.toString(pS));
}
else
{
pG++;
tvG.setText("Gabimet: " + Integer.toString(pG));
}
So nse must be negative if number does not exist in int[] numrat and must be positive if fint exists on int[] numrat..
This is what I read on internet..
but in my example it is always negative.. ??!!
If you want Arrays.binarySearch() to work you should sort your array like written here:
binarySearch() ...
Performs a binary search for value in the ascending sorted array
array. Searching in an unsorted array has an undefined result. It's
also undefined which element is found if there are multiple
occurrences of the same element.
You may do it like this:
Arrays.sort(numrat);
Arrays.binarySearch(numrat, fint);

Most efficient way of comparing long arrays of strings

I'm using the speech recognizer to get a voice input from the user, it returns an array of 5 strings which I pass to this method
public int analyzeTag(ArrayList<String> voiceResults,Editor editor, Context context){
for (String match : voiceResults) {
Log.d(TAG, match);
if (match.equalsIgnoreCase(context.getResources().getString(R.string.first_tag))){
editor.append(context.getResources().getString(R.string.first_tag));
return 1;
}
else if (match.equalsIgnoreCase(context.getResources().getString(R.string.second_tag))){
editor.append(context.getResources().getString(R.string.second_tag));
return 1;
}
//etc....(huge list of tags)
//Some tags might also have acceptable variations, example:
else if (match.equalsIgnoreCase("img") || match.equalsIgnoreCase("image")
{
editor.append("img"); //the string to append is always taken from the first variation
}
}
return 0;
}
This method compares the results with a list of tags, the tag list will be pretty big with hundreds of tags so I would like to find the most efficient way to do this operation.
I need help with:
1.Is my way of comparing results the most efficient? Is there a better way? (from the user experience perspective, I don't want users waiting a long time to get a result).
The voice input will be a big part of my app so this method will be called quite often
2.I have a long list of tags, obviously the if(), elseIf() route is gonna be quite repetitive, is there a way to iterate this? Considering the fact that some tags might have variations (even more than 1)and that the variation 1 ("img") will be the same for everyone, but other variations will be locale/language sensitive example: "image" for english users "immagini" for italian users etc.
Text appended to the editor will be always taken from the first variation
How about puting tags in a StringArray and then iterate though the array ?
String[] tags = context.getResources().getStringArray(R.array.tags);
for (String match : voiceResults) {
for (int index = 0; index < tags.length; index++ ) {
if (match.equalsIgnoreCase(tags[index]) {
editor.append(tags[index]);
}
}
}
Here's the doc on StringArray

Separating the words after the last integer in a large String

I've seen many people do similar to this in order to get the last word of a String:
String test = "This is a sentence";
String lastWord = test.substring(test.lastIndexOf(" ")+1);
I would like to do similar but get the last few words after the last int, it can't be hard coded as the number could be anything and the amount of words after the last int could also be unlimited. I'm wondering whether there is a simple way to do this as I want to avoid using Patterns and Matchers again due to using them earlier on in this method to receive a similar effect.
Thanks in advance.
I would like to get the last few words after the last int.... as the number could be anything and the amount of words after the last int could also be unlimited.
Here's a possible suggestion. Using Array#split
String str = "This is 1 and 2 and 3 some more words .... foo bar baz";
String[] parts = str.split("\\d+(?!.*\\d)\\s+");
And now parts[1] holds all words after the last number in the string.
some more words .... foo bar baz
What about this one:
String test = "a string with a large number 1312398741 and some words";
String[] parts = test.split();
for (int i = 1; i < parts.length; i++)
{
try
{
Integer.parseInt(parts[i])
}
catch (Exception e)
{
// this part is not a number, so lets go on...
continue;
}
// when parsing succeeds, the number was reached and continue has
// not been called. Everything behind 'i' is what you are looking for
// DO YOUR STUFF with parts[i+1] to parts[parts.length] here
}

String input distributed to array (Android)?

I've been looking for answer regarding my question but I cannot find one or maybe I'm not just using the right terms when searching.
My question is, how can I distribute string entered in an editText to an array?
This is about my porter2 stemming project. I need to distribute the strings entered in the EditText field to an array so i can scan whether x in array[x] is vowel or not.
Ex.
String in EditText field = "dog".
Array should be:
array[0] = d
array[1] = o
array[2] = g
I'm sorry that i cannot give a code because I really don't have any idea how to code this one. Thank you so much everyone. :)
You can use use .split() with an empty ("") string input:
String text = yourEditText.getText().toString();
String[] letters = text.split(""); // Split by empty string to be in an array
// letters == { "", "d", "o", "g" }
// ^ Note that this has an empty string element at the front; that's just a byproduct of how split() works.
If you want a char array instead, it's much easier to use .toCharArray():
String text = yourEditText.getText().toString();
char[] letters = text.toCharArray();
// letters == { 'd', 'o', 'g' };
Personally I'd use the second one; letter == 'a' is a much faster operation than letter.equals("a").

How can I pull individual characters from a string and convert them to numbers?

What I'm trying to do is find a way I can take the word "camel" for example from a EditText field and make for instance c=2 a=1 m=4 e=5 l=3. Is there anyway I can pull the individual characters from a string and convert them to numbers?
I've tried using "split" to separate each character into an array but I can't figure out how to convert the letters into numbers
so I can do something like:
a=1
b=2
c=3
int temp = (int)(array[1]+array[2]+array[3]+etc...)
using the example of "camel" would equal 15
This is what I have so far:
String name = inputarea.getText().toString();
String[] array = name.split("");
for(int i =0; i < array.length ; i++)
The biggest problem I keep having is if I try to pull from the 7th position in the array and nothing is there. (camel only has 5 characters) then I get a nice big error.
Thank you for any help that can be provided.
Edit: I figured it out after a few hours of playing with it here is my working code:
String firstname = inputarea.getText().toString();
char[] array = firstname.toCharArray();
final char[] array2 = new char[15];
System.arraycopy(array, 0, array2, 0, array.length);
if (array2[0] == 'A' ) {
array2[0] = '1';
}
suggestion:
first, need define all letter, from a-z (A-Z), the ASCII code 'a' to 'z' is 97 to 122, if you want support the upper letter, you need add A-Z.
then, get the letter in the string, u can use this:
for(int i=0;i<string.length();i++){
int number = string.charAt(i);
}
when you get the number size, you can reduce to the base number('a' is 97), you will get the individual number
Does String.charAt() works for you?
As for converting to number, if the numbers are consecutive you can define a fixed string with all the characters you want to map and use String.indexOf(). If not, you can have a parallel array with ints or use a Map.

Categories

Resources