2

I've been creating a Pig Latin translator for a group project for university (we don't have to actually make a translator, just manipulate a string in any way we'd like, and I chose this).

The input into my translator is a Latin prayer, the first two lines of which are:

credo in unum deum 
patrem omnipotentem 

I've created my translator with the following code:

public static void pigLatinify(String fname) throws IOException 
{
    File file = new File("projectdata.txt");

    try 
    {
        Scanner scan1 = new Scanner(file);
        while (scan1.hasNextLine()) 
        {
            Scanner scan2 = new Scanner(scan1.nextLine());
            boolean test2;
            while (test2 = scan2.hasNext())
            {
                String s = scan2.next();
                char firstLetter = s.charAt(0);
                if (firstLetter=='a' || firstLetter=='i' || firstLetter=='o' || firstLetter=='e' || 
                        firstLetter=='u' || firstLetter=='A' || firstLetter=='I' || firstLetter=='O' || 
                        firstLetter=='E' || firstLetter=='U')
                {
                    String output = s + "hay" + " ";
                    System.out.print(output);
                }
                    else
                    {
                        String restOfWord = s.substring(1);
                        String output = restOfWord + firstLetter + "ay" + " ";
                        System.out.print(output);
                    }
                }
                System.out.println("");
            }
            scan1.close();
        } 

        catch (FileNotFoundException e) 
        {
            e.printStackTrace();
        }
    }
}

It outputs the entire prayer well, with the following output for the first two lines:

redocay inhay unumhay eumday 
atrempay omnipotentemhay

However, in true Pig Latin, monosyllabic words stay the same and have "-hay" added to the end, so "it" becomes "ithay", "egg" becomes "egghay", but multi syllabic words have "-way" added to the end instead, so "archery" becomes "archeryway" and "ending" becomes "endingway".

Is there a way for Java (and the scanner class I'm using) to detect if a word is monosyllabic?

At this point I'll also point out I'm only a beginner programmer, so if there is but it is extraordinarily complicated, feel free to just say that!!

tehlexx
  • 2,821
  • 16
  • 29
Andrew Martin
  • 5,619
  • 10
  • 54
  • 92
  • Related: [http://stackoverflow.com/a/405179/1225328](http://stackoverflow.com/a/405179/1225328) – sp00m Nov 15 '12 at 13:05

3 Answers3

0

I don't think your difficulties will lie in writing Java but in establishing foolproof rules for counting the syllables in a word. For your language I'd be inclined to count one syllable for each continuous run of vowels in a word, but don't count terminal e as evidence of a syllable.

So,

eat has one syllable, only one run of vowels;

ate has one, two runs of vowels, less one for the terminal e

eight has one syllable

eighteen has two

funicular has four

etc

I'm quite sure you'll find counterexamples for this simple set of rules, but perhaps they're enough to get you started.

High Performance Mark
  • 77,191
  • 7
  • 105
  • 161
0

If you want to do it properly you will have to find a Latin dictionary with syllabification. Latin is fairly regular but there are exceptions. A dictionary such as http://athirdway.com/glossa/ has scansion

crēdo, dĭdi, dĭtum

but is only available one word at a time. You would also have to write a parser for the syllables. I mention this because people thing languages are easy to parse and interpret - they usually aren't!

peter.murray.rust
  • 37,407
  • 44
  • 153
  • 217
0

What about getting the number of syllables like this:

/**
 * Get the number of syllables for a given word
 * @param s the given word
 * @return the number of syllables
 */
public static int getNumberOfSyllables(String s) {
    s = s.trim();
    if (s.length() <= 3) {
        return 1;
    }
    s = s.toLowerCase();
    s = s.replaceAll("[aeiouy]+", "a");
    s = "x" + s + "x";
    return s.split("a").length - 1;
}
sp00m
  • 47,968
  • 31
  • 142
  • 252
  • I like this, but I can see what peter.murray.rust meant below. Your method will return the Latin word "deum" as one vowel, rather than two - which would be incorrect as Latin rules are slightly different Still, I'm a novice programmer and I think this will do just fine! – Andrew Martin Nov 15 '12 at 13:56