Java Stack Usage getSyllables(String pinyin)

Here you can find the source of getSyllables(String pinyin)

Description

Breaks a pinyin string on tone markers

License

Open Source License

Declaration

public static String[] getSyllables(String pinyin) 

Method Source Code


//package com.java2s;
/*/*  ww  w .j  a  va  2  s. co m*/
 Hanzi Helper, http://hanzihelper.sourceforge.net
 Copyright (C) 2005, Colin Jacobs
    
 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation; either
 version 2.1 of the License, or (at your option) any later version.
    
 This library is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 Lesser General Public License for more details.
    
 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class Main {
    /**
     * Breaks a pinyin string on tone markers
     */
    public static String[] getSyllables(String pinyin) {

        // Go through the pinyin.
        // A syllableFromStack ends with:
        // - A number
        // - the end of a vowel group
        // - n, ng, r
        // - the end of the string
        // All syllables contain a vowel group
        // I'm doing the stack stuff in the hope of making this more robust
        // in future, such as being able to divide the string "canguan" into "can, guan"
        // JDK 1.5
        //    List<String> syllables = new ArrayList<String>();
        //    Stack<Character> last = new Stack<Character>();
        //    Stack<Character> next = new Stack<Character>();
        List syllables = new ArrayList();
        Stack last = new Stack();
        Stack next = new Stack();
        for (int i = pinyin.length() - 1; i >= 0; i--) {
            //      next.push(pinyin.charAt(i));
            next.push(new Character(pinyin.charAt(i)));
        }

        do {

            //      char c = next.pop();
            //      last.push(c);
            char c = ((Character) next.pop()).charValue();
            if (' ' == c) {
                if (last.size() > 0) {
                    syllables.add(syllableFromStack(last));
                }
                continue;
            }

            last.push(new Character(c));
            if (Character.isDigit(c)) {
                syllables.add(syllableFromStack(last));
                continue;
            } else if (c == 'r' && (next.size() == 0 || Character.isDigit(((Character) next.peek()).charValue()))) {
                last.pop();
                if (((Character) last.peek()).charValue() == 'e') {
                    // Add another epicyle!
                    last.push(new Character('r'));
                    continue;
                }
                if (next.size() > 0) {
                    char nextChar = ((Character) next.pop()).charValue();
                    if (Character.isDigit(nextChar)) {
                        last.push(new Character(nextChar));
                    } else {
                        next.push(new Character(nextChar));
                    }
                    syllables.add(syllableFromStack(last));
                    syllables.add("er");
                }
            }

        } while (!next.isEmpty());

        if (!last.isEmpty()) {
            syllables.add(syllableFromStack(last));
        }

        return (String[]) syllables.toArray(new String[] {});

    }

    private static String syllableFromStack(Stack stack) {
        StringBuffer sb = new StringBuffer();
        while (!stack.isEmpty()) {
            sb.append(stack.pop());
        }
        String s = sb.reverse().toString();
        return s;
    }
}

Related

  1. getBinary(long input)
  2. getOperands(Stack stack, int nOperands)
  3. getPermutationsRec(List permutations, byte[] order, List> remaining, int index)
  4. getPostOrder(List inOrderList)
  5. getRelativePath(Stack pathStack)
  6. isDoubleQuote(Stack bufStack)
  7. normalizeAbsolutePath(String curDir)
  8. removeBackets(String cont)
  9. removeDotSegments(String relativePath)