Skip to content

Commit

Permalink
Fix calculation of vector norm, re Issue #522. Need to consolidate th…
Browse files Browse the repository at this point in the history
…e calculations into a helper function.
  • Loading branch information
honnibal committed Oct 23, 2016
1 parent a0a4ada commit 2c3a67b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
9 changes: 5 additions & 4 deletions spacy/tokens/doc.pyx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
cimport cython
from libc.string cimport memcpy, memset
from libc.stdint cimport uint32_t
from libc.math cimport sqrt

import numpy
import numpy.linalg
import struct
cimport numpy as np
import math
import six
import warnings

Expand Down Expand Up @@ -251,11 +251,12 @@ cdef class Doc:
if 'vector_norm' in self.user_hooks:
return self.user_hooks['vector_norm'](self)
cdef float value
cdef double norm = 0
if self._vector_norm is None:
self._vector_norm = 1e-20
norm = 0.0
for value in self.vector:
self._vector_norm += value * value
self._vector_norm = math.sqrt(self._vector_norm)
norm += value * value
self._vector_norm = sqrt(norm) if norm != 0 else 0
return self._vector_norm

def __set__(self, value):
Expand Down
9 changes: 5 additions & 4 deletions spacy/tokens/span.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ from collections import defaultdict
import numpy
import numpy.linalg
cimport numpy as np
import math
from libc.math cimport sqrt
import six

from ..structs cimport TokenC, LexemeC
Expand Down Expand Up @@ -136,11 +136,12 @@ cdef class Span:
if 'vector_norm' in self.doc.user_span_hooks:
return self.doc.user_span_hooks['vector'](self)
cdef float value
cdef double norm = 0
if self._vector_norm is None:
self._vector_norm = 1e-20
norm = 0
for value in self.vector:
self._vector_norm += value * value
self._vector_norm = math.sqrt(self._vector_norm)
norm += value * value
self._vector_norm = sqrt(norm) if norm != 0 else 0
return self._vector_norm

property text:
Expand Down

0 comments on commit 2c3a67b

Please sign in to comment.