Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.97 KB | None | 0 0
  1.  
  2.  
  3. Acitvity
  4.  
  5. package ca.roumani.piglatin;
  6.  
  7. import android.speech.tts.TextToSpeech;
  8. import android.support.v7.app.AppCompatActivity;
  9. import android.os.Bundle;
  10. import android.view.View;
  11. import android.widget.EditText;
  12. import android.widget.TextView;
  13. import android.widget.Toast;
  14.  
  15. import java.util.Locale;
  16.  
  17. public class PigLatinActivity extends AppCompatActivity implements TextToSpeech.OnInitListener
  18. {
  19. PigLatinModel model = new PigLatinModel();
  20. TextToSpeech tts;
  21.  
  22. @Override
  23. protected void onCreate(Bundle savedInstanceState)
  24. {
  25. this.tts = new TextToSpeech(this, this);
  26. new PigLatinModel();
  27. super.onCreate(savedInstanceState);
  28. setContentView(R.layout.activity_pig_latin);
  29. }
  30.  
  31. public void buttonClicked(View v)
  32. {
  33. say(model.getPig());
  34. }
  35.  
  36. public void buttonClicked2(View v)
  37. {
  38. ((EditText) findViewById(R.id.input)).setText("");
  39. }
  40. public void buttonClicked3(View v)
  41. {
  42. String word = ((EditText) findViewById(R.id.input)).getText().toString();
  43. model.setEnglish(word);
  44. String result = model.getPig();
  45. ((TextView) findViewById(R.id.convertedText)).setText(result);
  46. say(model.getEnglish());
  47. }
  48.  
  49. @Override
  50. public void onInit(int initStatus)
  51. {
  52. if (initStatus != TextToSpeech.ERROR)
  53. {
  54. if (tts.isLanguageAvailable(Locale.US) == TextToSpeech.LANG_AVAILABLE)
  55. {
  56. tts.setLanguage(Locale.US);
  57. }
  58. } else
  59. {
  60. Toast.makeText(this, "Text To Speech failed...", Toast.LENGTH_LONG).show();
  61. }
  62. }
  63. public void say(String s)
  64. {
  65. if (tts != null)
  66. {
  67. Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
  68. tts.speak(s, TextToSpeech.QUEUE_FLUSH, null);
  69. }
  70. }
  71. }
  72.  
  73.  
  74. Model
  75.  
  76. package ca.roumani.piglatin;
  77.  
  78. import java.util.StringTokenizer;
  79.  
  80. /**
  81. * Created by user on 3/14/17.
  82. */
  83. public class PigLatinModel
  84. {
  85. private String english;
  86. private String pig;
  87.  
  88. public PigLatinModel(){ this.setEnglish("");}
  89. public PigLatinModel(String text){this.setEnglish(text);}
  90. public String getEnglish(){return this.english;}
  91. public void setEnglish(String text){
  92. this.english = text.toLowerCase();
  93. this.translate();
  94. }
  95. public String getPig(){ return this.pig;}
  96. public void translate(){
  97. StringTokenizer st = new StringTokenizer(this.english);
  98. String result = "";
  99. while(st.hasMoreTokens())
  100. {
  101. String word = st.nextToken();
  102. String pig = this.translate(word);
  103. if(result.length()==0)
  104. {
  105. result = pig;
  106. }else{
  107. result = result + " " + pig;
  108. }
  109. this.pig = result;
  110. }
  111. }
  112. private String translate(String word){
  113. if(word.isEmpty())return word;
  114. String result = word;
  115. String[] vowels = {"a","e","i","o","u"};
  116. boolean Vowelcheck = false;
  117. int index = -1;
  118. for(int i = 0;i<vowels.length;i++){
  119. if(result.contains(vowels[i])){
  120. if(index >= 0){
  121. if (index > result.indexOf(vowels[i])){
  122. index = result.indexOf(vowels[i]);
  123. }
  124. }else{
  125. index = result.indexOf(vowels[i]);
  126. }
  127. Vowelcheck = true;
  128. }
  129. }
  130. if(Vowelcheck){
  131. if(index == 0){
  132. result = word +"way";
  133. }else{
  134. result = word.substring(index) + word.substring(0,index)+ "ay";
  135. }
  136. }else{
  137. result = word +"ay";
  138. }
  139. return result;
  140. }
  141. public static void main(String[] args) {
  142. PigLatinModel pigLatinModel = new PigLatinModel();
  143. System.out.println(pigLatinModel.translate("this"));
  144. System.out.println(pigLatinModel.translate("is"));
  145. System.out.println(pigLatinModel.translate("a"));
  146. System.out.println(pigLatinModel.translate("test"));
  147. System.out.println(pigLatinModel.translate("This is a test"));
  148. }
  149.  
  150. }
  151.  
  152.  
  153. XML
  154.  
  155. <?xml version="1.0" encoding="utf-8"?>
  156. <RelativeLayout
  157. xmlns:android="http://schemas.android.com/apk/res/android"
  158. xmlns:tools="http://schemas.android.com/tools"
  159. android:layout_width="match_parent"
  160. android:layout_height="match_parent"
  161. android:paddingBottom="@dimen/activity_vertical_margin"
  162. android:paddingLeft="@dimen/activity_horizontal_margin"
  163. android:paddingRight="@dimen/activity_horizontal_margin"
  164. android:paddingTop="@dimen/activity_vertical_margin"
  165. tools:context="ca.roumani.piglatin.PigLatinActivity">
  166.  
  167. <EditText
  168. android:layout_width="wrap_content"
  169. android:layout_height="wrap_content"
  170. android:id="@+id/input"
  171. android:layout_alignParentTop="true"
  172. android:layout_alignParentRight="true"
  173. android:layout_alignParentEnd="true"
  174. android:layout_marginTop="170dp"
  175. android:layout_alignParentLeft="true"
  176. android:layout_alignParentStart="true"/>
  177.  
  178. <Button
  179. android:layout_width="wrap_content"
  180. android:layout_height="wrap_content"
  181. android:text="@string/speak"
  182. android:id="@+id/speakButton"
  183. android:layout_alignParentBottom="true"
  184. android:layout_alignParentLeft="true"
  185. android:layout_alignParentStart="true"
  186. android:onClick="buttonClicked"/>
  187.  
  188. <Button
  189. android:layout_width="wrap_content"
  190. android:layout_height="wrap_content"
  191. android:text="@string/clear"
  192. android:id="@+id/button"
  193. android:layout_alignParentBottom="true"
  194. android:layout_centerHorizontal="true"
  195. android:onClick="buttonClicked2"/>
  196.  
  197. <Button
  198. android:layout_width="wrap_content"
  199. android:layout_height="wrap_content"
  200. android:text="@string/convert"
  201. android:id="@+id/convertButton"
  202. android:layout_alignBottom="@+id/button"
  203. android:layout_alignParentRight="true"
  204. android:layout_alignParentEnd="true"
  205. android:onClick="buttonClicked3"/>
  206.  
  207. <LinearLayout
  208. android:orientation="horizontal"
  209. android:layout_width="match_parent"
  210. android:layout_height="match_parent"
  211. android:layout_below="@+id/input"
  212. android:layout_centerHorizontal="true"
  213. android:layout_above="@+id/speakButton">
  214.  
  215. <ScrollView
  216. android:layout_width="match_parent"
  217. android:layout_height="match_parent"
  218. android:id="@+id/scrollView"
  219. android:fillViewport="true">
  220.  
  221. <TextView
  222. android:layout_width="wrap_content"
  223. android:layout_height="wrap_content"
  224. android:id="@+id/convertedText"/>
  225. </ScrollView>
  226. </LinearLayout>
  227.  
  228. </RelativeLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement