17898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project/*
27898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project * Copyright (C) 2009 The Android Open Source Project
37898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project *
47898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
57898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project * you may not use this file except in compliance with the License.
67898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project * You may obtain a copy of the License at
77898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project *
87898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
97898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project *
107898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project * Unless required by applicable law or agreed to in writing, software
117898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
127898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
137898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project * See the License for the specific language governing permissions and
147898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project * limitations under the License.
157898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project */
167898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
177898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project#ifndef PINYINIME_INCLUDE_SPELLINGTRIE_H__
187898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project#define PINYINIME_INCLUDE_SPELLINGTRIE_H__
197898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
207898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project#include <stdio.h>
217898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project#include <stdlib.h>
227898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project#include "./dictdef.h"
237898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
247898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Projectnamespace ime_pinyin {
257898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
267898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Projectstatic const unsigned short kFullSplIdStart = kHalfSpellingIdNum + 1;
277898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
287898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project// Node used for the trie of spellings
297898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Projectstruct SpellingNode {
307898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  SpellingNode *first_son;
317898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // The spelling id for each node. If you need more bits to store
327898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // spelling id, please adjust this structure.
337898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  uint16 spelling_idx:11;
347898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  uint16  num_of_son:5;
357898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  char char_this_node;
367898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  unsigned char score;
377898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project};
387898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
397898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Projectclass SpellingTrie {
407898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project private:
417898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  static const int kMaxYmNum = 64;
427898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  static const size_t kValidSplCharNum = 26;
437898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
447898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  static const uint16 kHalfIdShengmuMask = 0x01;
457898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  static const uint16 kHalfIdYunmuMask = 0x02;
467898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  static const uint16 kHalfIdSzmMask = 0x04;
477898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
487898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // Map from half spelling id to single char.
497898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // For half ids of Zh/Ch/Sh, map to z/c/s (low case) respectively.
507898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // For example, 1 to 'A', 2 to 'B', 3 to 'C', 4 to 'c', 5 to 'D', ...,
517898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // 28 to 'Z', 29 to 'z'.
527898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // [0] is not used to achieve better efficiency.
537898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  static const char kHalfId2Sc_[kFullSplIdStart + 1];
547898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
557898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  static unsigned char char_flags_[];
567898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  static SpellingTrie* instance_;
577898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
587898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // The spelling table
597898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  char *spelling_buf_;
607898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
617898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // The size of longest spelling string, includes '\0' and an extra char to
627898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // store score. For example, "zhuang" is the longgest item in Pinyin list,
637898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // so spelling_size_ is 8.
647898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // Structure: The string ended with '\0' + score char.
657898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // An item with a lower score has a higher probability.
667898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  size_t spelling_size_;
677898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
687898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // Number of full spelling ids.
697898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  size_t spelling_num_;
707898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
717898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  float score_amplifier_;
727898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  unsigned char average_score_;
737898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
747898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // The Yunmu id list for the spelling ids (for half ids of Shengmu,
757898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // the Yunmu id is 0).
767898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // The length of the list is spelling_num_ + kFullSplIdStart,
777898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // so that spl_ym_ids_[splid] is the Yunmu id of the splid.
787898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  uint8 *spl_ym_ids_;
797898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
807898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // The Yunmu table.
817898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // Each Yunmu will be assigned with Yunmu id from 1.
827898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  char *ym_buf_;
837898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  size_t ym_size_;  // The size of longest Yunmu string, '\0'included.
847898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  size_t ym_num_;
857898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
867898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // The spelling string just queried
877898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  char *splstr_queried_;
887898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
897898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // The spelling string just queried
907898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  char16 *splstr16_queried_;
917898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
927898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // The root node of the spelling tree
937898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  SpellingNode* root_;
947898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
957898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // If a none qwerty key such as a fnction key like ENTER is given, this node
967898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // will be used to indicate that this is not a QWERTY node.
977898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  SpellingNode* dumb_node_;
987898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
997898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // If a splitter key is pressed, this node will be used to indicate that this
1007898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // is a splitter key.
1017898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  SpellingNode* splitter_node_;
1027898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
1037898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // Used to get the first level sons.
1047898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  SpellingNode* level1_sons_[kValidSplCharNum];
1057898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
1067898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // The full spl_id range for specific half id.
1077898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // h2f means half to full.
1087898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // A half id can be a ShouZiMu id (id to represent the first char of a full
1097898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // spelling, including Shengmu and Yunmu), or id of zh/ch/sh.
1107898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // [1..kFullSplIdStart-1] is the arrange of half id.
1117898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  uint16 h2f_start_[kFullSplIdStart];
1127898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  uint16 h2f_num_[kFullSplIdStart];
1137898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
1147898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // Map from full id to half id.
1157898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  uint16 *f2h_;
1167898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
1177898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project#ifdef ___BUILD_MODEL___
1187898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // How many node used to build the trie.
1197898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  size_t node_num_;
1207898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project#endif
1217898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
1227898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  SpellingTrie();
1237898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
1247898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  void free_son_trie(SpellingNode* node);
1257898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
1267898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // Construct a subtree using a subset of the spelling array (from
1277898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // item_star to item_end).
1287898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // Member spelliing_buf_ and spelling_size_ should be valid.
1297898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // parent is used to update its num_of_son and score.
1307898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  SpellingNode* construct_spellings_subset(size_t item_start, size_t item_end,
1317898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project                                           size_t level, SpellingNode *parent);
1327898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  bool build_f2h();
1337898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
1347898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // The caller should guarantee ch >= 'A' && ch <= 'Z'
1357898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  bool is_shengmu_char(char ch) const;
1367898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
1377898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // The caller should guarantee ch >= 'A' && ch <= 'Z'
1387898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  bool is_yunmu_char(char ch) const;
1397898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
1407898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project#ifdef ___BUILD_MODEL___
1417898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // Given a spelling string, return its Yunmu string.
1427898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // The caller guaratees spl_str is valid.
1437898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  const char* get_ym_str(const char *spl_str);
1447898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
1457898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // Build the Yunmu list, and the mapping relation between the full ids and the
1467898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // Yunmu ids. This functin is called after the spelling trie is built.
1477898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  bool build_ym_info();
1487898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project#endif
1497898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
1507898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  friend class SpellingParser;
1517898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  friend class SmartSplParser;
1527898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  friend class SmartSplParser2;
1537898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
1547898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project public:
1557898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  ~SpellingTrie();
1567898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
1577898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  inline static bool is_valid_spl_char(char ch) {
1587898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z');
1597898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  }
1607898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
1617898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // The caller guarantees that the two chars are valid spelling chars.
1627898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  inline static bool is_same_spl_char(char ch1, char ch2) {
1637898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    return ch1 == ch2 || ch1 - ch2 == 'a' - 'A' || ch2 - ch1 == 'a' - 'A';
1647898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  }
1657898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
1667898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // Construct the tree from the input pinyin array
1677898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // The given string list should have been sorted.
1687898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // score_amplifier is used to convert a possibility value into score.
1697898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // average_score is the average_score of all spellings. The dumb node is
1707898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // assigned with this score.
1717898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  bool construct(const char* spelling_arr, size_t item_size, size_t item_num,
1727898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project                 float score_amplifier, unsigned char average_score);
1737898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
1747898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // Test if the given id is a valid spelling id.
1757898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // If function returns true, the given splid may be updated like this:
1767898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // When 'A' is not enabled in ShouZiMu mode, the parsing result for 'A' is
1777898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // first given as a half id 1, but because 'A' is a one-char Yunmu and
1787898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // it is a valid id, it needs to updated to its corresponding full id.
1797898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  bool if_valid_id_update(uint16 *splid) const;
1807898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
1817898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // Test if the given id is a half id.
1827898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  bool is_half_id(uint16 splid) const;
1837898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
1847898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  bool is_full_id(uint16 splid) const;
1857898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
1867898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // Test if the given id is a one-char Yunmu id (obviously, it is also a half
1877898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // id), such as 'A', 'E' and 'O'.
1887898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  bool is_half_id_yunmu(uint16 splid) const;
1897898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
1907898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // Test if this char is a ShouZiMu char. This ShouZiMu char may be not enabled.
1917898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // For Pinyin, only i/u/v is not a ShouZiMu char.
1927898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // The caller should guarantee that ch >= 'A' && ch <= 'Z'
1937898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  bool is_szm_char(char ch) const;
1947898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
1957898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // Test If this char is enabled in ShouZiMu mode.
1967898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // The caller should guarantee that ch >= 'A' && ch <= 'Z'
1977898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  bool szm_is_enabled(char ch) const;
1987898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
1997898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // Enable/disable Shengmus in ShouZiMu mode(using the first char of a spelling
2007898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // to input).
2017898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  void szm_enable_shm(bool enable);
2027898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
2037898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // Enable/disable Yunmus in ShouZiMu mode.
2047898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  void szm_enable_ym(bool enable);
2057898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
2067898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // Test if this char is enabled in ShouZiMu mode.
2077898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // The caller should guarantee ch >= 'A' && ch <= 'Z'
2087898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  bool is_szm_enabled(char ch) const;
2097898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
2107898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // Return the number of full ids for the given half id.
2117898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  uint16 half2full_num(uint16 half_id) const;
2127898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
2137898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // Return the number of full ids for the given half id, and fill spl_id_start
2147898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // to return the first full id.
2157898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  uint16 half_to_full(uint16 half_id, uint16 *spl_id_start) const;
2167898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
2177898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // Return the corresponding half id for the given full id.
2187898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // Not frequently used, low efficient.
2197898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // Return 0 if fails.
2207898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  uint16 full_to_half(uint16 full_id) const;
2217898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
2227898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // To test whether a half id is compatible with a full id.
2237898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // Generally, when half_id == full_to_half(full_id), return true.
2247898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // But for "Zh, Ch, Sh", if fussy mode is on, half id for 'Z' is compatible
2257898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // with a full id like "Zhe". (Fussy mode is not ready).
2267898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  bool half_full_compatible(uint16 half_id, uint16 full_id) const;
2277898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
2287898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  static const SpellingTrie* get_cpinstance();
2297898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
2307898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  static SpellingTrie& get_instance();
2317898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
2327898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // Save to the file stream
2337898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  bool save_spl_trie(FILE *fp);
2347898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
2357898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // Load from the file stream
2367898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  bool load_spl_trie(FILE *fp);
2377898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
2387898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // Get the number of spellings
2397898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  size_t get_spelling_num();
2407898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
2417898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // Return the Yunmu id for the given Yunmu string.
2427898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // If the string is not valid, return 0;
2437898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  uint8 get_ym_id(const char* ym_str);
2447898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
2457898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // Get the readonly Pinyin string for a given spelling id
2467898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  const char* get_spelling_str(uint16 splid);
2477898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
2487898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // Get the readonly Pinyin string for a given spelling id
2497898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  const char16* get_spelling_str16(uint16 splid);
2507898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
2517898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // Get Pinyin string for a given spelling id. Return the length of the
2527898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // string, and fill-in '\0' at the end.
2537898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  size_t get_spelling_str16(uint16 splid, char16 *splstr16,
2547898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project                            size_t splstr16_len);
2557898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project};
2567898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project}
2577898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
2587898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project#endif  // PINYINIME_INCLUDE_SPELLINGTRIE_H__
259