adding prefix to words in EditText - android

I have a List of Strings and i want to compare every i write in an EditText with that list. If there is a match then i have to add a "-" character as a prefix for that word.
I am using a TextWatcher and this is my code so far:
#Override
public void afterTextChanged(Editable s) {
String tmp = s.toString();
words = tmp.split(" ");
for (int i = 0; i < words.length; i++) {
for (Iterator iterator = myList.iterator(); iterator
.hasNext();) {
String str = (String) iterator.next();
if (str.equalsIgnoreCase(words[i])) {
if (!words[i].contains("-")) {
tmp = tmp.replace(words[i], "-" + words[i]);
}
editMain.setText(tmp);
editMain.setSelection(tmp.length());
}
}
}
}
It works but if i type the same word twice in my EditText, the first ocurrence gets two "--".
For example:
hello this is -android (works ok)
hello this is --android -android (does not work ok)
And the desired result should be:
hello this is -android android (because the repeated word already exists)
Any help? thanks in advance

Your question is not very clear. Maybe you mean android word has already been found and then it should not be prefixed by a -.
If that's the case, just remove a mathcing word from mylist. For that use a listIterator.

try to set a counter. If the counter is bigger than 1, then don't write the -

Related

How to extract particular text from Image

From the following image, I want to extract number below text Arzt-Nr (654321161).
I've used OCR reader but it is extracting texts randomly not in a sequence, making it difficult to add a logic to extract no below "Arzt-Nr".
I've used following code but texts are not in sequence.
Is there any way to achieve this?
String text = "";
for (int i = 0; i < detectedItems.size(); i++) {
TextBlock item = detectedItems.valueAt(i);
String detectedText = item.getValue();
List<Line> lines = (List<Line>) item.getComponents();
for (Line line : lines) {
List<Element> elements = (List<Element>) line.getComponents();
for (Element element : elements) {
String word = element.getValue();
text = text + " " + word;
}
text += "\n";
}
}
Try to check a fixed length to the words after "Arzt-Nr" position, try also to check the pattern of the word founded.. for example if you need only numbers ecc...
Extract tsv output of image using tesseract and find the nearest text below the location of keyword. Also have a look at page segmentation modes of tesseract.
Link to Generating tsv
Link to use page segmentation

How to substract single string from a sparseArray?

I've been working with Android Mobile Vision OCR API for a while. Everything is work perfectly until i found that i need to extract just single words from the whole SparseArray (Mobile Vision API default return is a TextBlocks which defined in a SparseArray)
SparseArray<TextBlock> textBlocks = textRecognizer.detect(imageFrame);
for (int i = 0; i < textBlocks.size(); i++) {
TextBlock textBlock = textBlocks.get(textBlocks.keyAt(i));
List<Line> lines = (List<Line>) textBlock.getComponents();
for (Line line : lines) {
List<Element> elements = (List<Element>)
line.getComponents();
for (Element element : elements) {
word = element.getValue();
Log.d(TAG, "word Read : " + word);
}
}
}
When i check
Log.d(TAG, "word Read : " + word);
it print out repeatedly all element in the SparseArray
It seems that i'm asking a not-so-obvious question. But can i extract just a single or couple word from those "words" printed above ? For example, i want to extract the word which has character above 12 and has number in it.
Any help or hints will much Appreciated.
You could add logical expression to filter result like below:
word = element.getValue();
if (word .length() > 12 && word .matches("[0-9]+")) {
Log.d(TAG, "word Read : " + word);
}
You are running word in a loop that's why it's printing all the values. When you run it only once according to the answer of #navylover you will get a single string. Just remove the for loop

Android edit/insert into string

I was wondering how I could programmatically edit strings in android. I am displaying strings from my device to my website, and the apostrophes ruin the PHP output. so in order to fix this, I needed to add character breaks, ie: the backslash '\'.
For example, if I have this string: I love filiberto's!
I need android to edit it to: I love filiberto\'s!
However, each string is going to be different, and there will also be other characters that I have to escape from . How can I do this?
I was wondering how I could programmatically edit strings in android. I am displaying strings from my device to my website, and the apostrophes ruin the PHP output. so in order to fix this, I needed to add character breaks, ie: the backslash '\'.
This is what I have so far, thanks to ANJ for base code...:
if(title.contains("'")) {
int i;
int len = title.length();
char[] temp = new char[len + 1]; //plus one because gotta add new
int k = title.indexOf("'"); //location of apostrophe
for (i = 0; i < k; i++) { //all the letters before the apostrophe
temp[i] = title.charAt(i); //assign letters to array based on index
}
temp[k] = 'L'; // the L is for testing purposes
for (i = k+1; i == len; i++) { //all the letters after apostrophe, to end
temp[i] = title.charAt(i); //finish the original string, same array
}
title = temp.toString(); //output array to string (?)
Log.d("this is", title); //outputs gibberish
}
Which outputs random characters.. not even similar to my starting string. Does anyone know what could be causing this? For example, the string "Lol'ok" turns into >> "%5BC%4042ed0380"
I am assuming you are storing the string somewhere. Lets say the string is: str.
You can use a temporary array to add the '/'. For a single string:
int len = str.length();
char [] temp = new char[len+1]; //Temporary Array
int k = str.indexOf("'"), i; //Finding index of "'"
for(i=0; i<k-1; i++)
{
temp[i] = str.charAt(i); //Copying the string before '
}
temp[k] = '/'; //Placing "/" before '
for(i=k; j<len; j++)
{
temp[i+1] = str.charAt(i); //Copying rest of the string
}
String newstr = temp.toString(); //Converting array to string
You can use the same for multiple strings. Just make it as a function and call it whenever you want.
The String API has a number of API calls that could help, for example String.replaceAll. But...
apostrophes ruin the PHP output
Then fix the PHP code rather than require "clean" input. Best option would be to select a well supported transport format (say JSON or XML) and let the Json API on each end handle escape code.

Android BreakIterator hyphenated words?

I using breakIterator to get each word from a sentence and there is problem when a sentence like "my mother-in-law is coming for a visit" where i am not able to get mother-in-law as a single word.
BreakIterator iterator = BreakIterator.getWordInstance(Locale.ENGLISH);
for (int end = iterator.next(); end != BreakIterator.DONE; start = end, end = iterator.next())
{
String possibleWord = sentence.substring(start, end);
if (Character.isLetterOrDigit(possibleWord.charAt(0)))
{
// grab the word
}
}
As I'm seeing in your code what are you trying to do is to check if the first character in every word are a character or a digit. Every time you use the BreakIterator.getWordInstance() you will always get all the words depending on the boundary rules of the Locale and it is a little hard to accomplish what you want to do with the use of this class until I know, so my advice is this:
String text = "my mother-in-law is coming for a visit";
String[] words = text.split(" ");
for (String word : words){
if (Character.isLetterOrDigit(word.charAt(0))){
// grab the word
}
}

Use AutoCompleteTextView but hide characters on the keyboard

I'm using AutoCompleteTextView that the user can see the opportunities.
So when I tap on two characters e.g. "ba" I will see "Bahamas", "Bahrain","Azerbaijan" etc - this works!
But if I don't have a country starting with the letter "z" I will hide the z on the keyboard. And if I tap the two characters "ba", I will only see "h" on my keyboard.
How can I do that? And how can I realize it if I still want to tap "ba" and will get "bahamas" AND "azerbaijan"?
Thanks everyone!
as Cata pointed out, you won't be able to hide the keys on the softkeyboard unless you write your own keyboard - you do have a couple of other options however:
you could put a text watcher on the the edit text, and if they key they type isn't in any of the words, you could delete it
you could use setKeyListener http://developer.android.com/reference/android/widget/TextView.html#setKeyListener%28android.text.method.KeyListener%29 to and validate their entries.
Both of these will not hide keys from the keyboard but you could use them to prevent the user from typing an invalid key.
As for your second question about matching the user input to the middle of a string, this is not in the code of the autocomplete:
Relevant code form the android source:
for (int i = 0; i < count; i++) {
final T value = values.get(i);
final String valueText = value.toString().toLowerCase();
// First match against the whole, non-splitted value
if (valueText.startsWith(prefixString)) {
newValues.add(value);
} else {
final String[] words = valueText.split(" ");
final int wordCount = words.length;
for (int k = 0; k < wordCount; k++) {
if (words[k].startsWith(prefixString)) {
newValues.add(value);
break;
}
}
}
}
you can see it will only match the first characters of any individual word in an item. So to achieve your second goal, you would have to write your own adapter that implements filterable.
here is a tutorial that might get you started on that: http://thinkandroid.wordpress.com/2010/02/08/writing-your-own-autocompletetextview/

Categories

Resources