Home | History | Annotate | Download | only in include
      1 /*
      2  * Copyright (C) 2009 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of 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,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef PINYINIME_INCLUDE_SPLPARSER_H__
     18 #define PINYINIME_INCLUDE_SPLPARSER_H__
     19 
     20 #include "./dictdef.h"
     21 #include "./spellingtrie.h"
     22 
     23 namespace ime_pinyin {
     24 
     25 class SpellingParser {
     26  protected:
     27   const SpellingTrie *spl_trie_;
     28 
     29  public:
     30   SpellingParser();
     31 
     32   // Given a string, parse it into a spelling id stream.
     33   // If the whole string are sucessfully parsed, last_is_pre will be true;
     34   // if the whole string is not fullly parsed, last_is_pre will return whether
     35   // the last part of the string is a prefix of a full spelling string. For
     36   // example, given string "zhengzhon", "zhon" is not a valid speling, but it is
     37   // the prefix of "zhong".
     38   //
     39   // If splstr starts with a character not in ['a'-z'] (it is a split char),
     40   // return 0.
     41   // Split char can only appear in the middle of the string or at the end.
     42   uint16 splstr_to_idxs(const char *splstr, uint16 str_len, uint16 splidx[],
     43                         uint16 start_pos[], uint16 max_size, bool &last_is_pre);
     44 
     45   // Similar to splstr_to_idxs(), the only difference is that splstr_to_idxs()
     46   // convert single-character Yunmus into half ids, while this function converts
     47   // them into full ids.
     48   uint16 splstr_to_idxs_f(const char *splstr, uint16 str_len, uint16 splidx[],
     49           uint16 start_pos[], uint16 max_size, bool &last_is_pre);
     50 
     51   // Similar to splstr_to_idxs(), the only difference is that this function
     52   // uses char16 instead of char8.
     53   uint16 splstr16_to_idxs(const char16 *splstr, uint16 str_len, uint16 splidx[],
     54                         uint16 start_pos[], uint16 max_size, bool &last_is_pre);
     55 
     56   // Similar to splstr_to_idxs_f(), the only difference is that this function
     57   // uses char16 instead of char8.
     58   uint16 splstr16_to_idxs_f(const char16 *splstr16, uint16 str_len,
     59                             uint16 splidx[], uint16 start_pos[],
     60                             uint16 max_size, bool &last_is_pre);
     61 
     62   // If the given string is a spelling, return the id, others, return 0.
     63   // If the give string is a single char Yunmus like "A", and the char is
     64   // enabled in ShouZiMu mode, the returned spelling id will be a half id.
     65   // When the returned spelling id is a half id, *is_pre returns whether it
     66   // is a prefix of a full spelling string.
     67   uint16 get_splid_by_str(const char *splstr, uint16 str_len, bool *is_pre);
     68 
     69   // If the given string is a spelling, return the id, others, return 0.
     70   // If the give string is a single char Yunmus like "a", no matter the char
     71   // is enabled in ShouZiMu mode or not, the returned spelling id will be
     72   // a full id.
     73   // When the returned spelling id is a half id, *p_is_pre returns whether it
     74   // is a prefix of a full spelling string.
     75   uint16 get_splid_by_str_f(const char *splstr, uint16 str_len, bool *is_pre);
     76 
     77   // Splitter chars are not included.
     78   bool is_valid_to_parse(char ch);
     79 
     80   // When auto-correction is not enabled, get_splid_by_str() will be called to
     81   // return the single result. When auto-correction is enabled, this function
     82   // will be called to get the results. Auto-correction is not ready.
     83   // full_id_num returns number of full spelling ids.
     84   // is_pre returns whether the given string is the prefix of a full spelling
     85   // string.
     86   // If splstr starts with a character not in [a-zA-Z] (it is a split char),
     87   // return 0.
     88   // Split char can only appear in the middle of the string or at the end.
     89   // The caller should guarantee NULL != splstr && str_len > 0 && NULL != splidx
     90   uint16 get_splids_parallel(const char *splstr, uint16 str_len,
     91                              uint16 splidx[], uint16 max_size,
     92                              uint16 &full_id_num, bool &is_pre);
     93 };
     94 }
     95 
     96 #endif  // PINYINIME_INCLUDE_SPLPARSER_H__
     97