Android Open Source - OpenHSK C S V Parser






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  ava  2s.c  o  m*/
import static edu.openhsk.repository.DatabaseMetadata.T_WORDS;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.Locale;
import java.util.StringTokenizer;

import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;
import edu.openhsk.models.Hanzi;
import edu.openhsk.repository.DatabaseHelper;

public class CSVParser { //TODO refactor all or replace with csv4j
  private static final String LOG_TAG = CSVParser.class.getName();
  private static final int ISNOTLEARNED = 0;
  
  public static final char SEMICOLON_DELIMITER = ';';
  public static final char COMMA_DELIMITER = ',';

  private final DatabaseHelper dbh;

  public CSVParser(DatabaseHelper dbh) {
    this.dbh = dbh;
  }

  public void parseWordListCSV(BufferedReader br, int wordListId, char delimiter) {
    try {
      SQLiteDatabase db = null;
      int lineNumber = 0;
      try {
        dbh.clearWordList(wordListId);
        db = dbh.getWritableDatabase();
        SQLiteStatement compiledStatement = db.compileStatement("INSERT INTO " + T_WORDS + "(wordlistid, word, pinyin, definition, searchkey, islearned) VALUES(?,?,?,?,?,?);");
        db.beginTransaction();
        while (true) {
          String line = br.readLine();
          if (line == null || line.isEmpty()) {
            break;
          }
          
          StringTokenizer st = new StringTokenizer(line, String.valueOf(delimiter));
          int tokenNumber = 0;
          String word = null;
          String pinyin = null;
          String definition = null;
          String soundfile = null;
          String searchKey = null;
          while (st.hasMoreTokens()) {
            String token = st.nextToken();
            switch (tokenNumber) {
              case 0: //word
                word = token;
                break;
              case 1: //pinyin and searchkey (generated)
                pinyin = PinyinReplacer.convertNumberedToneMarksToVisual(token);
                searchKey = PinyinReplacer.removeAllToneMarks(token.toLowerCase(Locale.US));
                break;
              case 2: //definition
                definition = token.replace(SEMICOLON_DELIMITER, COMMA_DELIMITER);
                break;
              case 3: //soundfile
                soundfile = token;
                break;
              default:
                Log.d(LOG_TAG, "Token not in recognized group: " + token);
                break;
            }
            tokenNumber++;
          }
          Hanzi h = new Hanzi(word, pinyin, definition, searchKey, false, soundfile);
          lineNumber++;
          compiledStatement.bindLong(1, wordListId);
          compiledStatement.bindString(2, h.getWord());
          compiledStatement.bindString(3, h.getPinyin());
          compiledStatement.bindString(4, h.getDefinition());
          compiledStatement.bindString(5, h.getSearchKey());
          compiledStatement.bindLong(6, ISNOTLEARNED);
          compiledStatement.executeInsert();
          compiledStatement.clearBindings();
          Log.d(LOG_TAG, lineNumber + ": " + h.toString());
        }
        db.setTransactionSuccessful();
        Log.d(LOG_TAG, "Successfully imported word list");
      } finally {
        if (db != null) {
          db.endTransaction();
          DatabaseHelper.closeDatabase(db);
        }
      }
      Log.d(LOG_TAG, "Characters read from file: " + lineNumber);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

}




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