matx.runtime.trie module

class matx.runtime.trie.Trie(dic=None)[源代码]

基类:Object

Double Array Trie Object

参数:

dic (Dict[str, int]) – The key is word and the value is id

示例

>>> import matx
>>> tree = matx.Trie({"hello": 1, "hello w": 2, "good": 3})
>>> tree
Trie(addr: 0x5601feb2aca0)
>>> tree = matx.Trie()
>>> tree
Trie(addr: 0x5601feba5b90)
__init__(dic=None)[源代码]
load(file_path: str)[源代码]

Find the longest substring of w[pos:] in the trie tree

参数:
  • w (str) – The input word

  • pos (int, optional) – The start position

返回:

Return the length and id of the matched substring.

If not found, return (0, -1)

返回类型:

Tuple(int, int)

示例

>>> import matx
>>> tree = matx.Trie({"hello": 1, "hello w": 2, "good": 3})
>>> tree.prefix_search("hello world")
(7, 2)
>>> tree.prefix_search("hellf")
(0, -1)
prefix_search_all(w, pos=0)[源代码]

Find all substring of w[pos:] in the trie tree

参数:
  • w (str) – The input word

  • pos (int, optional) – The start position

返回:

Return a list of the length and id of the matched substring.

If not found, return []

返回类型:

List[Tuple(int, int)]

示例

>>> import matx
>>> trie = matx.Trie({'hello': 1, 'hello world': 2, 'hello hello world': 3})
>>> trie.prefix_search_all("hello hello world")
[(5, 1), (17, 3)]
save(file_path: str)[源代码]
update(w, index=-1)[源代码]

Insert a word and corresponding id into the trie tree

参数:
  • w (str) – The input word

  • index (int, optional) – id, -1 for default

示例

>>> import matx
>>> tree = matx.Trie()
>>> tree.update("hello", 1)
>>> tree.update("hello w", 2)
>>> tree.update("good", 3)
>>> tree.prefix_search("hello world")
(7, 2)
>>> tree.prefix_search("hellf")
(0, -1)
>>> tree.update("hell", 10)
>>> tree.prefix_search("hellf")
(4, 10)