Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Acitvity
- package ca.roumani.piglatin;
- import android.speech.tts.TextToSpeech;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
- import java.util.Locale;
- public class PigLatinActivity extends AppCompatActivity implements TextToSpeech.OnInitListener
- {
- PigLatinModel model = new PigLatinModel();
- TextToSpeech tts;
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- this.tts = new TextToSpeech(this, this);
- new PigLatinModel();
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_pig_latin);
- }
- public void buttonClicked(View v)
- {
- say(model.getPig());
- }
- public void buttonClicked2(View v)
- {
- ((EditText) findViewById(R.id.input)).setText("");
- }
- public void buttonClicked3(View v)
- {
- String word = ((EditText) findViewById(R.id.input)).getText().toString();
- model.setEnglish(word);
- String result = model.getPig();
- ((TextView) findViewById(R.id.convertedText)).setText(result);
- say(model.getEnglish());
- }
- @Override
- public void onInit(int initStatus)
- {
- if (initStatus != TextToSpeech.ERROR)
- {
- if (tts.isLanguageAvailable(Locale.US) == TextToSpeech.LANG_AVAILABLE)
- {
- tts.setLanguage(Locale.US);
- }
- } else
- {
- Toast.makeText(this, "Text To Speech failed...", Toast.LENGTH_LONG).show();
- }
- }
- public void say(String s)
- {
- if (tts != null)
- {
- Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
- tts.speak(s, TextToSpeech.QUEUE_FLUSH, null);
- }
- }
- }
- Model
- package ca.roumani.piglatin;
- import java.util.StringTokenizer;
- /**
- * Created by user on 3/14/17.
- */
- public class PigLatinModel
- {
- private String english;
- private String pig;
- public PigLatinModel(){ this.setEnglish("");}
- public PigLatinModel(String text){this.setEnglish(text);}
- public String getEnglish(){return this.english;}
- public void setEnglish(String text){
- this.english = text.toLowerCase();
- this.translate();
- }
- public String getPig(){ return this.pig;}
- public void translate(){
- StringTokenizer st = new StringTokenizer(this.english);
- String result = "";
- while(st.hasMoreTokens())
- {
- String word = st.nextToken();
- String pig = this.translate(word);
- if(result.length()==0)
- {
- result = pig;
- }else{
- result = result + " " + pig;
- }
- this.pig = result;
- }
- }
- private String translate(String word){
- if(word.isEmpty())return word;
- String result = word;
- String[] vowels = {"a","e","i","o","u"};
- boolean Vowelcheck = false;
- int index = -1;
- for(int i = 0;i<vowels.length;i++){
- if(result.contains(vowels[i])){
- if(index >= 0){
- if (index > result.indexOf(vowels[i])){
- index = result.indexOf(vowels[i]);
- }
- }else{
- index = result.indexOf(vowels[i]);
- }
- Vowelcheck = true;
- }
- }
- if(Vowelcheck){
- if(index == 0){
- result = word +"way";
- }else{
- result = word.substring(index) + word.substring(0,index)+ "ay";
- }
- }else{
- result = word +"ay";
- }
- return result;
- }
- public static void main(String[] args) {
- PigLatinModel pigLatinModel = new PigLatinModel();
- System.out.println(pigLatinModel.translate("this"));
- System.out.println(pigLatinModel.translate("is"));
- System.out.println(pigLatinModel.translate("a"));
- System.out.println(pigLatinModel.translate("test"));
- System.out.println(pigLatinModel.translate("This is a test"));
- }
- }
- XML
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- tools:context="ca.roumani.piglatin.PigLatinActivity">
- <EditText
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/input"
- android:layout_alignParentTop="true"
- android:layout_alignParentRight="true"
- android:layout_alignParentEnd="true"
- android:layout_marginTop="170dp"
- android:layout_alignParentLeft="true"
- android:layout_alignParentStart="true"/>
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/speak"
- android:id="@+id/speakButton"
- android:layout_alignParentBottom="true"
- android:layout_alignParentLeft="true"
- android:layout_alignParentStart="true"
- android:onClick="buttonClicked"/>
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/clear"
- android:id="@+id/button"
- android:layout_alignParentBottom="true"
- android:layout_centerHorizontal="true"
- android:onClick="buttonClicked2"/>
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/convert"
- android:id="@+id/convertButton"
- android:layout_alignBottom="@+id/button"
- android:layout_alignParentRight="true"
- android:layout_alignParentEnd="true"
- android:onClick="buttonClicked3"/>
- <LinearLayout
- android:orientation="horizontal"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_below="@+id/input"
- android:layout_centerHorizontal="true"
- android:layout_above="@+id/speakButton">
- <ScrollView
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:id="@+id/scrollView"
- android:fillViewport="true">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/convertedText"/>
- </ScrollView>
- </LinearLayout>
- </RelativeLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement