com.hankcs.hanlp.collection.trie
类 DoubleArrayTrie<V>

java.lang.Object
  继承者 com.hankcs.hanlp.collection.trie.DoubleArrayTrie<V>
所有已实现的接口:
ITrie<V>, Serializable

public class DoubleArrayTrie<V>
extends Object
implements Serializable, ITrie<V>

双数组Trie树

另请参见:
序列化表格

嵌套类摘要
 class DoubleArrayTrie.Searcher
          一个搜索工具(注意,当调用next()返回false后不应该继续调用next(),除非reset状态)
 
字段摘要
protected  int[] base
           
protected  int[] check
           
protected  int size
          base 和 check 的大小
protected  V[] v
           
 
构造方法摘要
DoubleArrayTrie()
           
 
方法摘要
 int build(List<String> _key, int[] _length, int[] _value, int _keySize)
          唯一的构建方法
 int build(List<String> key, List<V> value)
           
 int build(List<String> key, V[] value)
           
 int build(Set<Map.Entry<String,V>> entrySet)
          构建DAT
 int build(TreeMap<String,V> keyValueMap)
          方便地构造一个双数组trie树
 List<Integer> commonPrefixSearch(String key)
           
 List<Integer> commonPrefixSearch(String key, int pos, int len, int nodePos)
          前缀查询
 LinkedList<Map.Entry<String,V>> commonPrefixSearchWithValue(char[] keyChars, int begin)
          优化的前缀查询,可以复用字符数组
 LinkedList<Map.Entry<String,V>> commonPrefixSearchWithValue(String key)
          已过时。 最好用优化版的
 boolean containsKey(String key)
           
 int exactMatchSearch(char[] keyChars, int pos, int len, int nodePos)
          精确查询
 int exactMatchSearch(String key)
          精确匹配
 int exactMatchSearch(String key, int pos, int len, int nodePos)
           
 V get(char[] key)
           
 V get(int index)
          从值数组中提取下标为index的值
注意为了效率,此处不进行参数校验
 V get(String key)
          精确查询
 int[] getBase()
          获取base数组引用,不要修改base
 int[] getCheck()
          获取check数组引用,不要修改check
 int getNonzeroSize()
           
 DoubleArrayTrie.Searcher getSearcher(char[] text, int offset)
           
 DoubleArrayTrie.Searcher getSearcher(String text, int offset)
           
 int getSize()
           
 int getTotalSize()
           
 int getUnitSize()
           
 V[] getValueArray(V[] a)
           
 V getValueAt(int index)
          获取index对应的值
 boolean load(ByteArray byteArray, V[] value)
           
 boolean load(String path)
          载入双数组,但是不提供值,此时本trie相当于一个set
 boolean load(String path, List<V> value)
          从磁盘加载,需要额外提供值
 boolean load(String path, V[] value)
          从磁盘加载,需要额外提供值
 void open(String fileName)
           
 V output(int state)
          检查状态是否对应输出
 boolean save(DataOutputStream out)
          将base和check保存下来
 void save(ObjectOutputStream out)
           
 boolean save(String fileName)
           
 boolean serializeTo(String path)
          将自己序列化到
 boolean set(String key, V value)
          更新某个键对应的值
 int size()
          树叶子节点个数
 String toString()
           
protected  int transition(char[] path)
          沿着节点转移状态
protected  int transition(int current, char c)
          转移状态
protected  int transition(String path)
          沿着路径转移状态
 int transition(String path, int from)
          沿着路径转移状态
static
<T> DoubleArrayTrie<T>
unSerialize(String path)
           
 
从类 java.lang.Object 继承的方法
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

字段详细信息

check

protected int[] check

base

protected int[] base

size

protected int size
base 和 check 的大小


v

protected V[] v
构造方法详细信息

DoubleArrayTrie

public DoubleArrayTrie()
方法详细信息

getUnitSize

public int getUnitSize()

getSize

public int getSize()

getTotalSize

public int getTotalSize()

getNonzeroSize

public int getNonzeroSize()

build

public int build(List<String> key,
                 List<V> value)

build

public int build(List<String> key,
                 V[] value)

build

public int build(Set<Map.Entry<String,V>> entrySet)
构建DAT

参数:
entrySet - 注意此entrySet一定要是字典序的!否则会失败
返回:

build

public int build(TreeMap<String,V> keyValueMap)
方便地构造一个双数组trie树

指定者:
接口 ITrie<V> 中的 build
参数:
keyValueMap - 升序键值对map
返回:
构造结果

build

public int build(List<String> _key,
                 int[] _length,
                 int[] _value,
                 int _keySize)
唯一的构建方法

参数:
_key - 值set,必须字典序
_length - 对应每个key的长度,留空动态获取
_value - 每个key对应的值,留空使用key的下标作为值
_keySize - key的长度,应该设为_key.size
返回:
是否出错

open

public void open(String fileName)
          throws IOException
抛出:
IOException

save

public boolean save(String fileName)

save

public boolean save(DataOutputStream out)
将base和check保存下来

指定者:
接口 ITrie<V> 中的 save
参数:
out -
返回:

save

public void save(ObjectOutputStream out)
          throws IOException
抛出:
IOException

load

public boolean load(String path,
                    List<V> value)
从磁盘加载,需要额外提供值

参数:
path -
value -
返回:

load

public boolean load(String path,
                    V[] value)
从磁盘加载,需要额外提供值

参数:
path -
value -
返回:

load

public boolean load(ByteArray byteArray,
                    V[] value)
指定者:
接口 ITrie<V> 中的 load

load

public boolean load(String path)
载入双数组,但是不提供值,此时本trie相当于一个set

参数:
path -
返回:

serializeTo

public boolean serializeTo(String path)
将自己序列化到

参数:
path -
返回:

unSerialize

public static <T> DoubleArrayTrie<T> unSerialize(String path)

exactMatchSearch

public int exactMatchSearch(String key)
精确匹配

参数:
key - 键
返回:

exactMatchSearch

public int exactMatchSearch(String key,
                            int pos,
                            int len,
                            int nodePos)

exactMatchSearch

public int exactMatchSearch(char[] keyChars,
                            int pos,
                            int len,
                            int nodePos)
精确查询

参数:
keyChars - 键的char数组
pos - char数组的起始位置
len - 键的长度
nodePos - 开始查找的位置(本参数允许从非根节点查询)
返回:
查到的节点代表的value ID,负数表示不存在

commonPrefixSearch

public List<Integer> commonPrefixSearch(String key)

commonPrefixSearch

public List<Integer> commonPrefixSearch(String key,
                                        int pos,
                                        int len,
                                        int nodePos)
前缀查询

参数:
key - 查询字串
pos - 字串的开始位置
len - 字串长度
nodePos - base中的开始位置
返回:
一个含有所有下标的list

commonPrefixSearchWithValue

public LinkedList<Map.Entry<String,V>> commonPrefixSearchWithValue(String key)
已过时。 最好用优化版的

前缀查询,包含值

参数:
key - 键
返回:
键值对列表

commonPrefixSearchWithValue

public LinkedList<Map.Entry<String,V>> commonPrefixSearchWithValue(char[] keyChars,
                                                                   int begin)
优化的前缀查询,可以复用字符数组

参数:
keyChars -
begin -
返回:

toString

public String toString()
覆盖:
Object 中的 toString

size

public int size()
树叶子节点个数

返回:

getCheck

public int[] getCheck()
获取check数组引用,不要修改check

返回:

getBase

public int[] getBase()
获取base数组引用,不要修改base

返回:

getValueAt

public V getValueAt(int index)
获取index对应的值

参数:
index -
返回:

get

public V get(String key)
精确查询

参数:
key - 键
返回:

get

public V get(char[] key)
指定者:
接口 ITrie<V> 中的 get

getValueArray

public V[] getValueArray(V[] a)
指定者:
接口 ITrie<V> 中的 getValueArray

containsKey

public boolean containsKey(String key)
指定者:
接口 ITrie<V> 中的 containsKey

transition

protected int transition(String path)
沿着路径转移状态

参数:
path -
返回:

transition

protected int transition(char[] path)
沿着节点转移状态

参数:
path -
返回:

transition

public int transition(String path,
                      int from)
沿着路径转移状态

参数:
path - 路径
from - 起点(根起点为base[0]=1)
返回:
转移后的状态(双数组下标)

output

public V output(int state)
检查状态是否对应输出

参数:
state - 双数组下标
返回:
对应的值,null表示不输出

getSearcher

public DoubleArrayTrie.Searcher getSearcher(String text,
                                            int offset)

getSearcher

public DoubleArrayTrie.Searcher getSearcher(char[] text,
                                            int offset)

transition

protected int transition(int current,
                         char c)
转移状态

参数:
current -
c -
返回:

set

public boolean set(String key,
                   V value)
更新某个键对应的值

参数:
key - 键
value - 值
返回:
是否成功(失败的原因是没有这个键)

get

public V get(int index)
从值数组中提取下标为index的值
注意为了效率,此处不进行参数校验

参数:
index - 下标
返回:


Copyright © 2014–2015 码农场. All rights reserved.