2012-11-15 42 views
1

我已經創造了大學的一組項目的Pig Latin翻譯檢測音節(我們沒有真正讓翻譯,只需操縱任何我們想辦法一個字符串,我選擇了這個)。Scanner類:在Word

到我的翻譯輸入是一個拉丁祈禱,前兩行,其中有:

credo in unum deum 
patrem omnipotentem 

我已經建立了我的翻譯用下面的代碼:

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(); 
     } 
    } 
} 

它的輸出整個祈禱都很好,前兩行輸出如下:

redocay inhay unumhay eumday 
atrempay omnipotentemhay 

但是,在真豬拉丁文,單音節詞語保持不變,並在末尾加上「-hay」,所以「it」變成「ithay」,「egg」變成「egghay」,但是多音節詞彙在結尾加上「-way」所以「射箭」變成了「射箭」,「結局」變成了「終點」。

是否有一個Java的方式(和我使用的掃描器類)來檢測,如果一個詞是單音節?

在這一點上我也會指出我只是一個初學編程的,所以如果有,但它是非常複雜的,隨意只是說!

+0

相關:[http://stackoverflow.com/a/405179/1225328](http://stackoverflow.com/a/405179/ 1225328) – sp00m

回答

0

怎麼樣得到的音節數是這樣的:

/** 
* 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; 
} 
+0

我喜歡這個,但是我可以看到下面peter.murray.rust的含義。你的方法會返回拉丁單詞「deum」作爲一個元音,而不是兩個 - 這將是不正確的,因爲拉丁規則稍有不同 不過,我是一個新手程序員,我認爲這樣做會很好! –

+0

另外,你的代碼的s =「x」+ s +「x」行是做什麼的? –

0

我認爲你的困難不在於編寫Java,而在於建立用於統計一個單詞中的音節的萬無一失的規則。對於你的語言,我傾向於對一個單詞中的每個連續運行的元音計算一個音節,但不要把終端e作爲一個音節的證據。

所以,

eat只有一個音節,只有元音的一個運行;

ate具有一個,元音的兩個運行,少一個用於終端e

eight具有一個音節

eighteen具有兩個

funicular具有四個

我很肯定你會爲這套簡單的規則找到反例,但也許它們足以讓你開始。

0

如果要妥善做好它,你將不得不尋找與音節拉丁文字典。拉丁文比較規則,但也有例外。字典如http://athirdway.com/glossa/有scansion

crēdo, dĭdi, dĭtum 

但一次只能提供一個單詞。你還必須爲這些音節編寫解析器。我提到這是因爲人的語言很容易解析和解釋 - 他們通常不是!