Home | History | Annotate | Download | only in latin
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
      5  * use this file except in compliance with the License. You may obtain a copy of
      6  * the License at
      7  *
      8  * http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
     12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
     13  * License for the specific language governing permissions and limitations under
     14  * the License.
     15  */
     16 
     17 package com.android.inputmethod.latin;
     18 
     19 import android.test.AndroidTestCase;
     20 import android.util.Log;
     21 import com.android.inputmethod.latin.tests.R;
     22 import java.io.InputStreamReader;
     23 import java.io.InputStream;
     24 import java.io.BufferedReader;
     25 import java.util.StringTokenizer;
     26 
     27 public class SuggestPerformanceTests extends AndroidTestCase {
     28     private static final String TAG = "SuggestPerformanceTests";
     29 
     30     private String mTestText;
     31     private SuggestHelper sh;
     32 
     33     @Override
     34     protected void setUp() {
     35         // TODO Figure out a way to directly using the dictionary rather than copying it over
     36 
     37         // For testing with real dictionary, TEMPORARILY COPY main dictionary into test directory.
     38         // DO NOT SUBMIT real dictionary under test directory.
     39         //int[] resId = new int[] { R.raw.main0, R.raw.main1, R.raw.main2 };
     40 
     41         int[] resId = new int[] { R.raw.test };
     42 
     43         sh = new SuggestHelper(TAG, getTestContext(), resId);
     44         loadString();
     45     }
     46 
     47     private void loadString() {
     48         try {
     49             InputStream is = getTestContext().getResources().openRawResource(R.raw.testtext);
     50             BufferedReader reader = new BufferedReader(new InputStreamReader(is));
     51             StringBuilder sb = new StringBuilder();
     52             String line = reader.readLine();
     53             while (line != null) {
     54                 sb.append(line + " ");
     55                 line = reader.readLine();
     56             }
     57             mTestText = sb.toString();
     58         } catch (Exception e) {
     59             e.printStackTrace();
     60         }
     61     }
     62 
     63     /************************** Helper functions ************************/
     64     private int lookForSuggestion(String prevWord, String currentWord) {
     65         for (int i = 1; i < currentWord.length(); i++) {
     66             if (i == 1) {
     67                 if (sh.isDefaultNextSuggestion(prevWord, currentWord.substring(0, i),
     68                         currentWord)) {
     69                     return i;
     70                 }
     71             } else {
     72                 if (sh.isDefaultNextCorrection(prevWord, currentWord.substring(0, i),
     73                         currentWord)) {
     74                     return i;
     75                 }
     76             }
     77         }
     78         return currentWord.length();
     79     }
     80 
     81     private double runText(boolean withBigrams) {
     82         StringTokenizer st = new StringTokenizer(mTestText);
     83         String prevWord = null;
     84         int typeCount = 0;
     85         int characterCount = 0; // without space
     86         int wordCount = 0;
     87         while (st.hasMoreTokens()) {
     88             String currentWord = st.nextToken();
     89             boolean endCheck = false;
     90             if (currentWord.matches("[\\w]*[\\.|?|!|*|@|&|/|:|;]")) {
     91                 currentWord = currentWord.substring(0, currentWord.length() - 1);
     92                 endCheck = true;
     93             }
     94             if (withBigrams && prevWord != null) {
     95                 typeCount += lookForSuggestion(prevWord, currentWord);
     96             } else {
     97                 typeCount += lookForSuggestion(null, currentWord);
     98             }
     99             characterCount += currentWord.length();
    100             if (!endCheck) prevWord = currentWord;
    101             wordCount++;
    102         }
    103 
    104         double result = (double) (characterCount - typeCount) / characterCount * 100;
    105         if (withBigrams) {
    106             Log.i(TAG, "with bigrams -> "  + result + " % saved!");
    107         } else {
    108             Log.i(TAG, "without bigrams  -> "  + result + " % saved!");
    109         }
    110         Log.i(TAG, "\ttotal number of words: " + wordCount);
    111         Log.i(TAG, "\ttotal number of characters: " + mTestText.length());
    112         Log.i(TAG, "\ttotal number of characters without space: " + characterCount);
    113         Log.i(TAG, "\ttotal number of characters typed: " + typeCount);
    114         return result;
    115     }
    116 
    117 
    118     /************************** Performance Tests ************************/
    119     /**
    120      * Compare the Suggest with and without bigram
    121      * Check the log for detail
    122      */
    123     public void testSuggestPerformance() {
    124         assertTrue(runText(false) <= runText(true));
    125     }
    126 }
    127