From 480ef8bfc8b92b2de6c4960070c9269cfa505c4f Mon Sep 17 00:00:00 2001 From: Matthew Honnibal Date: Wed, 31 May 2017 07:14:29 -0500 Subject: [PATCH] Add compat function to normalize dict keys --- spacy/compat.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/spacy/compat.py b/spacy/compat.py index 2a551a831..b3019f45b 100644 --- a/spacy/compat.py +++ b/spacy/compat.py @@ -6,6 +6,8 @@ import ftfy import sys import ujson +import thinc.neural.util + try: import cPickle as pickle except ImportError: @@ -32,6 +34,7 @@ copy_reg = copy_reg CudaStream = CudaStream cupy = cupy fix_text = ftfy.fix_text +copy_array = thinc.neural.util.copy_array is_python2 = six.PY2 is_python3 = six.PY3 @@ -71,3 +74,16 @@ def is_config(python2=None, python3=None, windows=None, linux=None, osx=None): (windows == None or windows == is_windows) and (linux == None or linux == is_linux) and (osx == None or osx == is_osx)) + + +def normalize_string_keys(old): + '''Given a dictionary, make sure keys are unicode strings, not bytes.''' + new = {} + for key, value in old: + if isinstance(key, bytes_): + new[key.decode('utf8')] = value + else: + new[key] = value + return new + +