Android Open Source - OpenHSK Pinyin Replacer






From Project

Back to project page OpenHSK.

License

The source code is released under:

This work is licensed under a Creative Commons Attribution 3.0 Unported License. Original author of word lists: http://lingomi.com/ Original author of definitions: http://cc-cedict.org Original autho...

If you think the Android project OpenHSK listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package edu.openhsk.utils;
//  w w w  .  j a v  a2s. c o  m
/**
 * A hideous but effective class for replacing tone numbers with with tone marks in pinyin.
 * @author Admin
 */
public class PinyinReplacer {

  /**
   * Replacement lookup table for pinyin tone markers.
   * DO NOT CHANGE THE ORDER OF THE ROWS!
   */
  private static final String[][] replacementTable = {
    {"a1","??"},
    {"a2",""},
    {"a3","?"},
    {"a4",""},
    {"a5","a"},
    {"e1","?"},
    {"e2",""},
    {"e3","?"},
    {"e4",""},
    {"e5","e"},
    {"i1","?"},
    {"i2",""},
    {"i3","??"},
    {"i4",""},
    {"i5","i"},
    {"o1","??"},
    {"o2",""},
    {"o3","?"},
    {"o4",""},
    {"o5","o"},
    {"u1","?"},
    {"u2",""},
    {"u3","?"},
    {"u4",""},
    {"u5","u"},
    {"1","?"},
    {"2","?"},
    {"3","?"},
    {"4","?"},
    {"5",""},
    {"an1","??n"},
    {"an2","n"},
    {"an3","?n"},
    {"an4","n"},
    {"an5","an"},
    {"ang1","??ng"},
    {"ang2","ng"},
    {"ang3","?ng"},
    {"ang4","ng"},
    {"ang5","ang"},
    {"en1","?n"},
    {"en2","n"},
    {"en3","?n"},
    {"en4","n"},
    {"en5","en"},
    {"eng1","?ng"},
    {"eng2","ng"},
    {"eng3","?ng"},
    {"eng4","ng"},
    {"eng5","eng"},
    {"in1","?n"},
    {"in2","n"},
    {"in3","??n"},
    {"in4","n"},
    {"in5","in"},
    {"ing1","?ng"},
    {"ing2","ng"},
    {"ing3","??ng"},
    {"ing4","ng"},
    {"ing5","ing"},
    {"ong1","??ng"},
    {"ong2","ng"},
    {"ong3","?ng"},
    {"ong4","ng"},
    {"ong5","ong"},
    {"un1","?n"},
    {"un2","n"},
    {"un3","?n"},
    {"un4","n"},
    {"un5","un"},
    {"er1","?r"},
    {"er2","r"},
    {"er3","?r"},
    {"er4","r"},
    {"er5","er"},
    {"r5","er"},
    {"a??","??o"},
    {"a","o"},
    {"a?","?o"},
    {"a","o"},
    {"o?","??u"},
    {"o","u"},
    {"o?","?u"},
    {"o","u"},
    {"a?","??i"},
    {"a","i"},
    {"a??","?i"},
    {"a","i"},
    {"e?","?i"},
    {"e","i"},
    {"e??","?i"},
    {"e","i"}
  };

  /**
   * Replaces numbered tone marks with visual tone marks. 
   * @param input The pinyin string containing numbered tone marks.
   * @return The resulting string containing visual tone marks.
   */
  public static String convertNumberedToneMarksToVisual(String input) {
    if (input.contains(" ")) {
      String[] strings = input.split("\\s");
      StringBuilder sb = new StringBuilder();
      for (String string : strings) {
        sb.append((" " + replace(string)));
      }
      return sb.toString().trim();
    } else {
      input = replace(input);
    }
    return input;
  }

  private static String replace(String input) {
    for (int i = 0; i < replacementTable.length; i++) {
      if (input.contains(replacementTable[i][0])) {
        input = input.replace(replacementTable[i][0], 
          replacementTable[i][1]);
      }
    }
    return input;
  }

  /**
   * Removes tone mark numbering from the included pinyin string.
   * @param input The pinyin string to be modified.
   * @return The new pinyin string with removed tone mark numbers.
   */
  public static String removeNumericToneMarks(String input) {
    return input.replaceAll("\\d", "");
  }
  
  public static String removeVisualToneMarks(String string) {
    for (int i = 0; i < replacementTable.length; i++) {
      if (string.contains(replacementTable[i][1])) {
        string = string.replace(replacementTable[i][1], replacementTable[i][0]);
      }
    }
    return string;
  }
  
  public static String removeAllToneMarks(String string) {
    return removeNumericToneMarks(removeVisualToneMarks(string)).trim();
  }
}




Java Source Code List

edu.openhsk.CharacterListActivity.java
edu.openhsk.CharacterViewActivity.java
edu.openhsk.CreateWordActivity.java
edu.openhsk.CreateWordListActivity.java
edu.openhsk.ExamActivity.java
edu.openhsk.ExamResultActivity.java
edu.openhsk.MainActivity.java
edu.openhsk.QuizActivity.java
edu.openhsk.WordListSelectionActivity.java
edu.openhsk.adapters.WordListViewBinder.java
edu.openhsk.adapters.WordViewBinder.java
edu.openhsk.models.ExamResult.java
edu.openhsk.models.Hanzi.java
edu.openhsk.models.QuizHanzi.java
edu.openhsk.models.WordList.java
edu.openhsk.repository.DatabaseHelper.java
edu.openhsk.repository.DatabaseMetadata.java
edu.openhsk.repository.HanziRepository.java
edu.openhsk.repository.HelpfulCursor.java
edu.openhsk.repository.QuizRepository.java
edu.openhsk.repository.SimpleCursorLoader.java
edu.openhsk.repository.WordListRepository.java
edu.openhsk.service.QuizService.java
edu.openhsk.utils.AsyncSoundPlayer.java
edu.openhsk.utils.CSVExporter.java
edu.openhsk.utils.CSVParser.java
edu.openhsk.utils.PinyinReplacer.java
edu.openhsk.utils.SoundManager.java
edu.openhsk.views.BarGraph.java
edu.openhsk.views.Bar.java
edu.openhsk.views.ExamResultPieChart.java