pytries / marisa-trie

Static memory-efficient Trie-like structures for Python based on marisa-trie C++ library.
https://marisa-trie.readthedocs.io/en/latest/
MIT License
1.04k stars 92 forks source link

how to insert new node? #74

Open ArlanCooper opened 3 years ago

ArlanCooper commented 3 years ago

if i want to insert new nodes,how can i insert into the trie? i can't find the methord of insert or add in the class?

wyk201722 commented 2 years ago

Found a way to update the trie tree node We could find the way to initailize the trie tree is adding a list of words to the marisa_trie.Trie Just appending the new node(word) to the list, and update trie. This is how it works for me

if i want to insert new nodes,how can i insert into the trie? i can't find the methord of insert or add in the class?

BoboTiG commented 2 years ago

@wyk201722 would you mind providing a minimal code :pray: ?

wyk201722 commented 2 years ago

No sure I got what the "insert new node" mean in this question, but I tried in this way. 5 6

just attached a simple test here, there is no "balabala" in the word_list or trie initially. We appended it in to the word_list and then we "reinitialize" or "insert" the word_list with "balabala" to the trie. In the second prefix_search, we could find "balabala".

I was wondering whether the index of item in word_list would change the key value of the node in trie. Therefore, I tried another test insert another word "foo" into the second place in the word_list, and then update the trie. The result shows the key value of the node in trie is not related with the position/index of the word in word_list, but the sequence/order of word insertion.

Just modifing(insert/delete) the item in the list would update the nodes in trie. Hope this works for you.

qixiang109 commented 1 year ago

Found a way to update the trie tree node We could find the way to initailize the trie tree is adding a list of words to the marisa_trie.Trie Just appending the new node(word) to the list, and update trie. This is how it works for me

if i want to insert new nodes,how can i insert into the trie? i can't find the methord of insert or add in the class?

maybe not the best way. We need marisa-trie mostly because we can not afford for the memory usage of a keeping plain list.