<commit_before>#! /usr/bin/env python
# vim: set encoding=utf-8

from ctypes import *

libtrie = cdll.LoadLibrary("./libtrie.so")
libtrie.trie_lookup.restype = c_char_p
libtrie.trie_get_last_error.restype = c_char_p


class Trie(object):
    def __init__(self, filename):
        self.ptr = libtrie.trie_load(filename)
        if self.ptr == 0:
            err = libtrie.trie_get_last_error()
            raise IOError(str(err))

    def lookup(self, key):
        res = libtrie.trie_lookup(self.ptr, c_char_p(key))
        if res:
            return [s.decode('utf8') for s in str(res).split('\n')]
        else:
            return []


t = Trie('prijmeni2.trie')
for s in t.lookup('Sedlář'):
    print s
print '%s' % t.lookup('blah')<commit_msg>Clean up memory<commit_after>#! /usr/bin/env python
# vim: set encoding=utf-8

from ctypes import *
import copy

libtrie = cdll.LoadLibrary("./libtrie.so")
libtrie.trie_load.argtypes = [c_char_p]
libtrie.trie_load.restype = c_void_p
libtrie.trie_lookup.argtypes = [ c_void_p, c_char_p ]
libtrie.trie_lookup.restype = c_void_p
libtrie.trie_get_last_error.restype = c_char_p
libtrie.trie_free_data.argtypes = [ c_void_p ]


class Trie(object):
    def __init__(self, filename):
        self.free_func = libtrie.trie_free
        self.ptr = libtrie.trie_load(filename)
        if self.ptr == 0:
            err = libtrie.trie_get_last_error()
            raise IOError(str(err))

    def __del__(self):
        if self:
            self.free_func(self.ptr)

    def lookup(self, key):
        res = libtrie.trie_lookup(self.ptr, key)
        if res:
            data = copy.deepcopy(c_char_p(res).value)
            libtrie.trie_free_data(res)
            return [s.decode('utf8') for s in data.split('\n')]
        else:
            return []


t = Trie('prijmeni5.trie')
for s in t.lookup('Sedlář'):
    print s
print '%s' % t.lookup('blah')
{'fraction_non_alphanumeric': 0.1293057763645999, 'fraction_numerical': 0.0042395336512983575, 'mean_word_length': 2.760956175298805, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3323449', 'n_tokens_mistral': 732, 'n_tokens_neox': 724, 'n_words': 134}