spaCy/spacy/serialize.pyx

101 lines
2.7 KiB
Cython
Raw Normal View History

2015-07-11 16:23:35 +03:00
from libcpp.vector cimport vector
from libc.stdint cimport uint32_t
from libc.stdint cimport int64_t
from libc.stdint cimport uint64_t
import numpy
cdef struct Node:
float prob
int left
int right
2015-07-11 21:01:10 +03:00
cpdef list huffman_encode(float[:] probs):
2015-07-11 16:23:35 +03:00
assert len(probs) >= 3
output = numpy.zeros(shape=(len(probs),), dtype=numpy.uint64)
cdef int size = len(probs)
cdef vector[Node] nodes
cdef int i = size - 1
cdef int j = 0
append_two(nodes, i, i-1, probs[i] + probs[i-1])
i -= 2
append_one(nodes, 0, i, probs[i])
j += 1
i -= 1
2015-07-11 21:01:10 +03:00
while i >= 0 or (j+1) < len(nodes):
2015-07-11 16:23:35 +03:00
if i < 0:
append_zero(nodes, j)
j += 2
elif j >= len(nodes):
append_two(nodes, i, i-1, probs[i]+probs[i-1])
2015-07-11 21:01:10 +03:00
i -= 2
2015-07-11 16:23:35 +03:00
elif i >= 1 and (j == len(nodes) or probs[i-1] < nodes[j].prob):
append_two(nodes, i, i-1, probs[i] + probs[i-1])
i -= 2
elif (j+1) < len(nodes) and nodes[j+1].prob < probs[i]:
append_zero(nodes, j)
j += 2
else:
append_one(nodes, j, i, probs[i])
i -= 1
j += 1
2015-07-11 21:01:10 +03:00
output = ['' for _ in range(len(probs))]
assign_codes(nodes, output, len(nodes) - 1, b'')
2015-07-11 16:23:35 +03:00
return output
2015-07-11 21:01:10 +03:00
cdef int assign_codes(vector[Node]& nodes, list codes, int i, bytes path) except -1:
left_path = path + b'0'
right_path = path + b'1'
# Assign down left branch
2015-07-11 11:57:30 +03:00
if nodes[i].left >= 0:
2015-07-11 21:01:10 +03:00
assign_codes(nodes, codes, nodes[i].left, left_path)
2015-07-11 16:23:35 +03:00
else:
2015-07-11 21:01:10 +03:00
# Leaf on left
codes[-(nodes[i].left + 1)] = left_path
# Assign down right branch
2015-07-11 11:57:30 +03:00
if nodes[i].right >= 0:
2015-07-11 21:01:10 +03:00
assign_codes(nodes, codes, nodes[i].right, right_path)
2015-07-11 16:23:35 +03:00
else:
2015-07-11 21:01:10 +03:00
# Leaf on right
codes[-(nodes[i].right + 1)] = right_path
2015-07-11 11:57:30 +03:00
2015-07-11 16:23:35 +03:00
cdef int append_zero(vector[Node]& nodes, int j) nogil:
cdef Node node
node.left = j
node.right = j+1
node.prob = nodes[j].prob + nodes[j+1].prob
nodes.push_back(node)
2015-07-11 11:57:30 +03:00
2015-07-11 16:23:35 +03:00
cdef int append_one(vector[Node]& nodes, int j, int id_, float prob) except -1:
cdef Node node
2015-07-11 11:57:30 +03:00
# Encode leaves as negative integers, where the integer is the index of the
# word in the vocabulary.
2015-07-11 16:23:35 +03:00
leaf_id = - <int64_t>(id_ + 1)
2015-07-11 11:57:30 +03:00
new_prob = prob + nodes[j].prob
if prob < nodes[j].prob:
2015-07-11 16:23:35 +03:00
node.left = leaf_id
node.right = j
node.prob = new_prob
nodes.push_back(node)
2015-07-11 11:57:30 +03:00
else:
2015-07-11 16:23:35 +03:00
node.left = j
node.right = leaf_id
node.prob = new_prob
nodes.push_back(node)
2015-07-11 11:57:30 +03:00
2015-07-11 16:23:35 +03:00
cdef int append_two(vector[Node]& nodes, int id1, int id2, float prob) except -1:
cdef Node node
node.left = -<int64_t>(id1 + 1)
node.right = -<int64_t>(id2 + 1)
node.prob = prob
nodes.push_back(node)