We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › textcat and translation - enum - integration
Page Index Toggle Pages: 1
textcat and translation - enum - integration? (Read 1436 times)
textcat and translation - enum - integration?
Sep 15th, 2009, 11:10am
 
Hi, the program I am working on is supposed to categorize the language of each text input and then to translate that input into different languages. I have got 2 problems at the moment. First one is that I am not sure how to pass the text from the textfield and the language from the button into one method. The second one is that I think, if I understand it correctly, the google translate java api is using enum values to specify the languages for translation (http://code.google.com/p/google-api-translate-java/). I found out on this site that enum's are not supported by processing, so I am clueless on how to achieve a flexible method that basically takes the language category as the source language and the button value as the target language. Many thanks for your help.



import controlP5.*;

//http://code.google.com/p/google-api-translate-java/
import com.google.api.translate.Language;
import com.google.api.translate.Translate;

//http://sourceforge.net/projects/textcat/
import org.knallgrau.utils.textcat.TextCategorizer;
import java.io.FileOutputStream;
import java.io.File;
import org.knallgrau.utils.textcat.FingerPrint;


ControlP5 controlP5;
public int myColorBackground = 200;
Textfield myTextfield;


void setup()
{
 size(450,350);
 frameRate(25);
 background(myColorBackground);
 controlP5 = new ControlP5(this);

 controlP5.Button fre  = controlP5.addButton("FRE",10,360,40,20,20);
 fre.setColorBackground(0);
 fre.setId(1);
 fre.setColorActive(60);

 controlP5.Button ger  = controlP5.addButton("GER",20,400,40,20,20);
 ger.setColorBackground(0);
 ger.setId(2);
 ger.setColorActive(60);

 myTextfield = controlP5.addTextfield("texts",100,300,200,20);
 myTextfield.setFocus(true);
}

void draw() {
}

public void texts(String theText)
{
 categorizeThis(theText);
}


public void GER(int theValue)
{
 String language = "GERMAN";
 myTextfield.submit();
}

public void FRE(int theValue)
{
 String language = "FRENCH";
 myTextfield.submit();
}


void categorizeThis(String theText, String language)
{
 String category = ("please enter a command line argument.");
 println(category);
 TextCategorizer guesser = new TextCategorizer();

 if(category.length() > 0)
 {
   category = guesser.categorize(theText);
 }
 println(category);  

 translateThis(category, theText, language);
}



void translateThis(String category, String theText, String language)
{
 String mySourceLanguage= category.toUpperCase();
 String myTargetLanguage = language;


 try
 {
   //this works
   String translatedText = Translate.translate(theText, Language.ENGLISH, Language.FRENCH);
   
 //this doesn't works
   String translatedText = Translate.translate(theText, Language.mySourceLanguage, Language.myTargetLanguage);
   
   println(translatedText);
 }
 catch (Exception ex)
 {
   ex.printStackTrace();
 }
}


Re: textcat and translation - enum - integration?
Reply #1 - Sep 15th, 2009, 11:49am
 
Quote:
how to pass the text from the textfield and the language from the button

You have to check controlP5 documentation for that, but it is likely to be something like String s = myTextfield.getText(); and you have to associate the button to a method (if that's similar to Swing -- I haven't used controlP5 yet).

[EDIT] I was right for the button, see Button example, you use a controlEvent method (common to all buttons) and use the ID to know which button have been clicked.
Same for Textfield: println(myTextfield.getText());

Quote:
enum's are not supported by processing

Wrong. Well, half wrong... You cannot declare enums in a .pde file. But you can very well use them.

Example, reusing an old enum class I made to test this new functionality:
TestEnum.java Code:
public enum TestEnum
{
 Number1(1, new String[] { "ichi", "one", "un", "uno" }, false),
 Number2(2, new String[] { "ni", "two", "deux", "dos" }, true),
 Number3(3, new String[] { "san", "three", "trois", "tres" }, false),
 Number4(4, new String[] { "shi", "four", "quatre", "quatro" }, true);

 private final int m_value;
 private final String[] m_names;
 private final boolean m_bEven;

 TestEnum(int value, String[] names, boolean bEven)
 {
   m_value = value;
   m_names = names;
   m_bEven = bEven;
 }

 String[] GetNames()
 {
   return m_names;
 }

 String GetNameList()
 {
   if (m_names == null || m_names.length == 0)
return "";
   StringBuilder names = new StringBuilder();
   for (String name : m_names)
   {
names.append(name).append(", ");
   }
   return names.delete(names.length() - 2, names.length() - 1).toString();
 }

 int GetValue()
 {
   return m_value;
 }

 boolean IsEven()
 {
   return m_bEven;
 }
}

TestingEnums.pde Code:
  TestEnum ev = TestEnum.valueOf("Number1");
 System.out.printf("Enumeration: %s (%s) = %d -- %s (%b) = %d\n",
ev, ev.name(), ev.ordinal(), ev.GetNameList(), ev.IsEven(), ev.GetValue());
 
 TestEnum[] enums = TestEnum.values();
 for (int i = 0; i < enums.length; i++)
 {
   TestEnum te = enums[i];
   System.out.printf("%d) %s = %d -- %s (%b)\n",
 te.ordinal(), te.name(), te.GetValue(), te.GetNameList(), te.IsEven());
 }
Page Index Toggle Pages: 1