1. 程式人生 > >Java對英文句子進行倒序排序

Java對英文句子進行倒序排序

轉自:http://www.sharejs.com/codes/java/6831

/**
 * 工具類
 * @author Administrator
 *
 */
public class Util {
    /**
     * 判斷一個字元是否是字母
     * @param c
     * @return
     */
    public static boolean isChar(char c) {
        return c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z';
    }
 
    /**
     * 判斷一個數字是否是數字
     * @param c
     * @return
     */
    public static boolean isNumber(char c) {
        return c >= '0' && c <= '9';
    }
 
    /**
     * 將List中的元素拼接成字串,拼接符為split
     * @param list
     * @param split
     * @return
     */
    public static <T> String list2String(List<T> list, String split) {
        StringBuffer sb = new StringBuffer();
        for (T t : list) {
            sb.append(t.toString());
            sb.append(split);
        }
        return sb.substring(0, sb.length() - split.length());
    }
 
    /**
     * 將List中的元素反序厚拼接成字串,拼接符為split
     * @param list
     * @param split
     * @return
     */
    public static <T> String list2String_reverse(List<T> list, String split) {
        StringBuffer sb = new StringBuffer();
        for (int i = list.size() - 1; i >= 0; --i) {
            sb.append(list.get(i).toString());
            sb.append(split);
        }
        return sb.substring(0, sb.length() - split.length());
    }
}
 
 
/**
 * 句子倒序 單詞的約束:字母或者數字,中間可以有一個連線符- 
 *          單詞間的分隔符可以是:一個或者多個空格,多餘一個的連線符-,
 *          其他非字母或者數字的字元
 * 
 * @param input:一個若干個單詞和間隔符組成的字串
 * @return:按照單詞倒序的句子。單詞間的間隔使用一個空格
 */
public class SentenceReverser {
    private static final int BEGIN = 0;
    private static final int SPLIT = 1;
    private static final int WORD = 2;
    private static final int WORDENDWITH_ = 3;
     
    private int status = BEGIN;
 
    public String reverse(String sentence) {
        if (sentence == null) {
            return null;
        }
        if (sentence.isEmpty()) {
            return "";
        }
 
        return _reverse02(sentence);
    }
 
    private String _reverse02(String sentence) {
        int size = sentence.length();
        char[] array = new char[size];
        sentence.getChars(0, size, array, 0);
 
        List<String> retStringList = new ArrayList<String>();
 
        String token = "";
        status = BEGIN;
        for (char c : array) {
            if (status == BEGIN) {
                token = processBEGIN(token, c);
            } else if (status == WORD) {
                token = processWORD(retStringList, token, c);
            } else if (status == SPLIT) {
                token = processSPLIT(retStringList, token, c);
            } else if (status == WORDENDWITH_) {
                token = processWORDWITH_(retStringList, token, c);
            }
        }
        if (!token.isEmpty()) {
            processLastToken(retStringList, token);
        }
 
        return Util.list2String_reverse(retStringList, "")
                .replaceAll(" +", " ").trim();
 
    }
 
    private void processLastToken(List<String> retStringList, String token) {
        if (status == WORD) {
            addWord(retStringList, token);
        } else if (status == SPLIT) {
            addSplit(retStringList, token);
        } else if (status == WORDENDWITH_) {
            String word = token.substring(0, token.length() - 1);
            addWord(retStringList, word);
            addSplit(retStringList, "-");
        }
    }
 
    private String processWORDWITH_(List<String> retStringList, String token, char c) {
        if (this.isCharOrNumber(c)) {
            token += c;
            status = WORD;
        } else {
            String word = token.substring(0, token.length() - 1);
            addWord(retStringList, word);
            token = "-";
            token += c;
            status = SPLIT;
        }
        return token;
    }
 
    private String processSPLIT(List<String> retStringList, String token, char c) {
        if (this.isCharOrNumber(c)) {
            addSplit(retStringList, token);
            token = "";
            token += c;
            status = WORD;
        } else {
            token += c;
        }
        return token;
    }
 
    private String processWORD(List<String> retStringList, String token, char c) {
        if (this.isCharOrNumber(c)) {
            token += c;
            status = WORD;
        } else if (c == '-') {
            token += c;
            status = WORDENDWITH_;
        } else {
            addWord(retStringList, token);
            token = "";
            token += c;
            status = SPLIT;
        }
        return token;
    }
 
    private String processBEGIN(String token, char c) {
        if (this.isCharOrNumber(c)) {
            token += c;
            status = WORD;
        } else {
            token += c;
            status = SPLIT;
        }
        return token;
    }
 
    private void addWord(List<String> list, String word) {
        list.add(word);
    }
 
    private void addSplit(List<String> list, String split) {
        list.add(" ");
        // list.add(split);
    }
 
    private boolean isCharOrNumber(char c) {
        return Util.isChar(c) || Util.isNumber(c);
    }
}
 
/**
 * 測試程式碼
 */
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
 
public class ReverseSentenceTest {
    private SentenceReverser process;
 
    @Before
    public void before() {
        process = new SentenceReverser();
    }
 
    @Test
    public void test001() {
        String input = "How are you";
        String expectResult = "you are How";
 
        String actualResult = process.reverse(input);
 
        assertEquals(expectResult, actualResult);
    }
 
    @Test
    public void test002() {
        String input = "How are  you";
        String expectResult = "you are How";
 
        String actualResult = process.reverse(input);
 
        assertEquals(expectResult, actualResult);
    }
 
    @Test
    public void test003() {
        String input = "How are--you";
        String expectResult = "you are How";
 
        String actualResult = process.reverse(input);
 
        assertEquals(expectResult, actualResult);
    }
 
    @Test
    public void test004() {
        String input = "How ar-e--you";
        String expectResult = "you ar-e How";
 
        String actualResult = process.reverse(input);
 
        assertEquals(expectResult, actualResult);
    }
 
    @Test
    public void test005() {
        String input = "How?ar-e--you";
        String expectResult = "you ar-e How";
 
        String actualResult = process.reverse(input);
 
        assertEquals(expectResult, actualResult);
    }
 
    @Test
    public void test006() {
        String input = "How are- you";
        String expectResult = "you are How";
 
        String actualResult = process.reverse(input);
 
        assertEquals(expectResult, actualResult);
    }
 
    @Test
    public void test007() {
        String input = "How";
        String expectResult = "How";
 
        String actualResult = process.reverse(input);
 
        assertEquals(expectResult, actualResult);
    }
 
    @Test
    public void test008() {
        String input = "How ";
        String expectResult = "How";
 
        String actualResult = process.reverse(input);
 
        assertEquals(expectResult, actualResult);
    }
 
    @Test
    public void test009() {
        String input = " How ";
        String expectResult = "How";
 
        String actualResult = process.reverse(input);
 
        assertEquals(expectResult, actualResult);
    }
 
    @Test
    public void test010() {
        String input = "";
        String expectResult = "";
 
        String actualResult = process.reverse(input);
 
        assertEquals(expectResult, actualResult);
    }
 
    @Test
    public void test011() {
        String input = null;
        String expectResult = null;
 
        String actualResult = process.reverse(input);
 
        assertEquals(expectResult, actualResult);
    }
 
}

//該程式碼片段來自於: http://www.sharejs.com/codes/java/6831