2

我一直在为大学的一个小组项目创建一个 Pig Latin 翻译器(我们不必实际制作翻译器,只需以我们喜欢的任何方式操作字符串,我选择了这个)。

我的翻译器输入的是拉丁文祈祷文,前两行是:

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”到最后,所以“archery”变成了“archeryway”,“ending”变成了“endingway”。

Java(以及我正在使用的扫描仪类)有没有办法检测一个单词是否是单音节的?

在这一点上我还要指出我只是一个初学者程序员,所以如果有但它非常复杂,请随意说!

4

3 回答 3

0

我认为您的困难不在于编写 Java,而在于建立计算单词中音节的万无一失的规则。对于您的语言,我倾向于为一个单词中每个连续运行的元音计算一个音节,但不要将终端e视为音节的证据。

所以,

eat有一个音节,只有一个元音;

ate有一个,两个元音,少一个用于终端e

eight有一个音节

eighteen有两个

funicular有四个

ETC

我很确定您会找到这套简单规则的反例,但也许它们足以让您入门。

于 2012-11-15T13:00:33.417 回答
0

如果你想正确地做到这一点,你将不得不找到一本带有音节的拉丁词典。拉丁语相当规律,但也有例外。像http://athirdway.com/glossa/这样的字典有scansion

crēdo, dĭdi, dĭtum

但一次只能使用一个单词。您还必须为音节编写解析器。我提到这一点是因为人们认为语言很容易解析和解释——它们通常不是!

于 2012-11-15T13:15:57.087 回答
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;
}
于 2012-11-15T13:34:44.590 回答