Pydatrie
Pydatire is a pure python implementation of DARTS (Double ARray Trie System).
It was introduced by Aoe at 1989 and was used for main data structure of mecab, one of the most famous Japenese morpheme analyzer.
I ported java implementation of this
to python and added many features like searching keys and values by given prefix.
I started this project to make pure python Korean mecab analyzer which was named pecab.
But sadly I can't find pure python implementation of DARTS, so I also made this package.
You can see the pecab project here.
Installation
pip install pydatrie
Usages
The usages were inspired by datrie package, a python wrapper of libdatrie.
Please check the details from the following code script.
from pydatrie import DoubleArrayTrie
trie = DoubleArrayTrie(
{
"AB": "1",
"ABCD": "2",
"EF": "3",
"EFGH": "4",
}
)
trie["AB"]
trie["EF"]
trie.get("AB")
trie.get("EF")
trie.prefixes()
trie.prefixes("ABC")
trie.prefixes("ABCD")
trie.prefix_items()
trie.prefix_items("ABC")
trie.prefix_items("ABCD")
trie.longest_prefix()
trie.longest_prefix("ABC")
trie.longest_prefix_item()
trie.longest_prefix_item("ABC")
trie.shortest_prefix()
trie.shortest_prefix("EFG")
trie.shortest_prefix_item()
trie.shortest_prefix_item("EFG")
"EF" in trie
"EFG" in trie
trie.has_prefix("EF")
trie.has_prefix("EFG")
trie.has_keys_with_prefix("A")
trie.has_keys_with_prefix("X")
trie.keys()
trie.keys("A")
trie.keys("ABC")
trie.values()
trie.values("A")
trie.values("ABC")
trie.items()
trie.items("A")
trie.items("ABC")
trie.suffixes()
trie.suffixes("A")
len(trie)
trie.save("file.dat")
trie2 = DoubleArrayTrie.load("file.dat")
trie2.items()
trie == trie2
print(trie)
for key in trie:
print(key)
trie.modify_value("AB", "99")
trie.items()
Limitations
Current implementation doesn't support dynamical node insertion and update. So you need to add all inputs when you create the object.
License
Copyright 2022 Hyunwoong Ko.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.