Randomly Generating Sentences for Display - android

I have a few questions concerning the application I'm designing. I have many different routes I could try in order to get what I wanted, but I thought I would get suggestions instead of doing trial and error.
I'm developing a simple app that has one Game screen (activity) and a main menu to start the game. The game screen will have two buttons, "Next" and "Repeat". Every time "Next" is hit, a new sentence in a different language (with the english translation below it) will appear, audio will pronounce the sentence, and hopefully I can get a highlighter to highlight the part of the sentence being spoken. You can guess what the Repeat button does.
My question is, what do you guys think would be the best way to store these sentences so they can be randomly picked out? I thought about making an array of structures or classes with the English definition, audio, and sentence in each structure. Then using a random iterator to pick one out. However, it would take a long time to do this approach and I wanted to get some ideas before I tried it.
Also, I'm not sure how I would print the sentence and definition on the screen.
Thanks!

Using an array of structs/classes seems like that would be the normal way to go.
Not really sure what you mean by a random iterator, but when picking out random sentences from the array of sentences, you might want to avoid repeats until you've gone through all the elements. To do that, you can make a second array of indices, select one at random from those, use the element that corresponds to that index, and remove that number from the array of indices.
In Java, that would look something like
ArrayList<Sentence> sentences;
ArrayList<Integer> indices;
Random rand;
private void populateIndices() {
for(int i = 0; i < sentences.size(); i++)
indices.add(i);
}
public Sentence getNextSentence() {
if(indices.isEmpty())
populateIndices();
int idx = rand.nextInt(indices.size());
int val = indices.get(idx);
indices.remove(idx);
return sentences.get(val);
}

Quite frankly I would load out of copyright books from Project Gutenberg and randomly pull sentences from them. I would then pass the sentences into Google APIs to translate and pronounce the sentences. Relying on external services is at the very heart of what a connected OS like Android is made for. It would be a much more compelling use of the platform than a canned Rosetta Stone like CD solution and your ability to tap into a broader amount of content would be increased exponentially.

Related

Android Studio version of an Excel vlookup?

Ok I'm a massive noob and apart from following lots of tutorials I like to set myself a problem and then try to fix it with an app. Therefore I'm trying to make a little app that'll help me when I'm at work.
Basically it needs to breakdown a 4 character string into it's individual characters and then display them phonetically. So if I (the user) type in 5F9A then it'll display FIVE FOXTROT NINE ALPHA. At work we have an excel spreadsheet that does this all and I'm just trying to reverse engineer it. The spreadsheet itself has multiple stages, it reads the characters, converts them into ASCII and then performs a vlookup on a range of cells where each ASCII code is next to it's phonetic pronunciation. It looks for the number 53 (5 in ASCII) and then looks at the cell next to it which says FIVE.
I've managed to translate any user input into ASCII but I just don't know how to store and access this next set of data. I've been looking into SQLite but that is waaaaaay beyond me at the moment and seems far to complicated for something this simple?
Anyway, I know it's cheating asking for the answer, but maybe a push in the right direction?
The dummy way to do that would be:
Get every letter (char) of the word
Have a switch case that gives you the phonetic equivalent (you will have to do that by hand)
String word = yourWord;
String phonetic;
char currentChar;
for(i=0;i<=word.lenth();i++){
currentChar = word.substring(i, i+1);
phonetic = getPhonetic(currentChar)
}
String getPhonetic(char char){
switch char{
case a:
return alpha;
break;
case b:
....
}
}

Android - Randomly setting a button in a grid

this is an extension to this question: Android - Handling a grid
Basically, I am wondering how to make a grid of 4x4 buttons randomly change their features(text, color, etc.) I don't need help setting the actual change in text or color, I just need a way to go about something like this. I don't know if I should write an array and choose from there, or use the GridView. Just a little start to something is all I need, I'm not asking for lots of code. Thanks in advance, I really appreciate it.
If your 4 ids are sequential as is
btnId1 = 8006;
btnId2 = 8007;
btnId3 = 8008;
btnId4 = 8009;
Random rn = new Random();
int chooseId = btnId1 + rn.nextInt(btnId4 - btnId1 + 1);
You can generate a random number like so:
Random r=new Random();
r.nextInt(4);
this will give you a random number in the range [0,4).
Based on this random number, you change the properties of the appropriate tile/square which you can enumerate yourself from 0 to 3 since you have 4 tiles in total.
From both your questions it seems that you lack some basic programming knowledge of algorithms/approaches to problems. You should try to fill that gap, which will make you progress MUCH MUCH faster in your programming endeavors!

Android Text to Speech add speech text continuously

I'm currently developing an app for visually impaired people which will read .txt files. I'm thinking about loading texts in blocks with i.e. 50 chars, that will be something like "page". The problem is how to connect those "blocks" in TTS. I'm using method Tts.speak(speechText, TextToSpeech.QUEUE_ADD, null) and between blocks there is always a space. It's annoying when the word or sentence (because of intonation) is divided with speech space. Isn't there something like "stream" that allows to add speech text to tts continuously and that doesn't give speech spaces?
I know I could divide the text not into pages but to sentences, but not all texts are in sentences so I would have to define some good way how to divide the text. The solution with blocks with same count of chars seems better to me now.
Have you tried to initialize a new TextToSpeech for every 50 chars and start it when the first ends?
Did you define, for example, two different TextToSpeech variables correctly initialized? and though:
1) First 50 chars added to the first queue and at the same time the second 50 chars added to the second queue;
2) When the first queue ends to reproduce start the second one end rewrite the first one with the third 50 chars;
I think you should not have some delays. They are necessary when modify one queue but if you will start a new one it should be immediate.

Shuffling cards and assigning them to user and computer in android development

I am creating a basic top trumps app in android development. I only need 6 top trumps card which have been made by using a database. I already have the code for this and have successfully created the cards.
I am stuck on this bit
When the user selects Play you should randomly assign half of the cards in the database to the human player, and half of the cards to the computer.
And was wondering if anyone had any ideas on how to do this? Thanks everyone.
UPDATE
I have tried doing this
ArrayList<Integer> cards = new ArrayList<Integer>();
for(int i=1;i<=6;i++)
{
this.cards.add(i);
}
Collections.shuffle(this.cards);
To shuffle the cards but I'm unsure on how to assign the computer and user the cards.
I'm not sure what you are aiming for here, but would something like this do (after you have shuffled):
ArrayList<Integer> playerCards = new ArrayList<Integer>(cards.subList(0,2));
ArrayList<Integer> computerCards = new ArrayList<Integer>(cards.subList(3,5));
Of course, you should use variables instead of constants everywhere, but you get the idea.
I am not an android programmer, but once long ago I realized the way to deal shuffled cards is to store the cards in an array or whatever in their natural order rather than a shuffled order -- for regular poker cards, 2-spades to A-spades, then 2-clubs to A-clubs, etc. Then, instead of dealing cards in-order from the shuffled deck, deal the cards by random draw from the unshuffled deck. You card elements need a "used" setting or value so they don't get reused or used twice. Saves LOADS of time doing the shuffle. In your case you on;y need to draw three cards, then the other guy gets the rest.

Android TextViews. Is parametrization possible? Is binding to model possible?

I am new to both Android and Stack Overflow. I have started developing and Android App and I am wondering two things:
1) Is it possible to parametrize a TextView? Lets say I want to render a text message which states something like: "The user age is 38". Lets suppose that the user age is the result of an algorithm. Using some typical i18n framework I would write in my i18n file something like "The user age is {0}". Then at run time I would populate parameters accordingly. I haven't been able to figure out how to do this or similar approach in Android.
2) Let's suppose I have a complex object with many fields. Eg: PersonModel which has id, name, age, country, favorite video game, whatever. If I want to render all this information into a single layout in one of my activities the only way I have found is getting all needed TextViews by id and then populate them one by one through code.
I was wondering if there is some mapping / binding mechanism in which I can execute something like: render(myPerson, myView) and that automatically through reflection each of the model properties get mapped into each of the TextViews.
If someone has ever worked with SpringMVC, Im looking for something similar to their mechanism to map domain objects / models to views (e.g. spring:forms).
Thanks a lot in advanced for your help. Hope this is useful for somebody else =)
bye!
In answer to #1: You want String.format(). It'll let you do something like:
int age = 38;
String ageMessage = "The user age is %d";
myTextView.setText(String.format(ageMessage, age));
The two you'll use the most are %d for numbers and %s for strings. It uses printf format if you know it, if you don't there's a quicky tutorial in the Formatter docs.
For #2 I think you're doing it the best way there is (grab view hooks and fill them in manually). If you come across anything else I'd love to see it.

Categories

Resources