Cython编译失败'unicode'不是类型标识符

时间:2018-07-26 08:16:30

标签: python-2.7 cython

我正在尝试从.pyx文件导入函数。在此之前,我需要先编译代码,但会遇到错误。

.pyx文件如下,该示例摘自[https://github.com/dedupeio/dedupe/blob/master/src/cpredicates.pyx]

cpdef list ngrams(basestring field, int n):
    """ngrams returns all unique, contiguous sequences of n characters
    of a given field.

    :param field: the string to be 
    :param n: the number of characters to be included in each gram

    usage:
    >>> from dedupe.dedupe.predicated import ngrams
    >>> ngrams("deduplicate", 3)
    ('ded', 'edu', 'dup', 'upl', 'pli', 'lic', 'ica', 'cat', 'ate')
    """
    cdef unicode ufield = _ustring(field)

    cdef list grams = []
    cdef int i, j
    cdef int n_char = len(ufield)
    for i in range(n_char):
        for j in range(i+n, min(n_char, i+n)+1):
            grams.append(ufield[i:j])

    return grams

cpdef tuple initials(basestring field, int n):
    """predicate which returns first a tuple containing
    the first n chars of a field if and only if the
    field contains at least n characters, or an empty
    tuple otherwise.

    :param field: the string 
    :type n: int, default None

    usage:
    >>> initials("dedupe", 7)
    ('dedupe', )
    >>> initials("deduplication", 7)
    ('dedupli', )
    """
    cdef unicode ufield = _ustring(field)

    return (ufield[:n], )

cdef unicode _ustring(basestring s):
    if type(s) is unicode:
        # fast path for most common case(s)
        return <unicode>s
    else : # safe because of basestring
        return <char *>s

我在python终端中执行了以下操作,并且cython编译失败,并显示“ unicode”不是类型标识符。我可以知道该怎么解决吗?

>>> import pyximport
>>> pyximport.install()
(None, <pyximport.pyximport.PyxImporter object at 0x236bb50>)
>>> from cpredicates import ngrams, initials

Error compiling Cython file:
------------------------------------------------------------
...

    return (ufield[:n], )

cdef unicode _ustring(basestring s):
^
------------------------------------------------------------

cpredicates.pyx:48:5: 'unicode' is not a type identifier

Error compiling Cython file:
------------------------------------------------------------
...
    usage:
    >>> from dedupe.dedupe.predicated import ngrams
    >>> ngrams("deduplicate", 3)
    ('ded', 'edu', 'dup', 'upl', 'pli', 'lic', 'ica', 'cat', 'ate')
    """
    cdef unicode ufield = _ustring(field)
        ^
------------------------------------------------------------

cpredicates.pyx:16:9: 'unicode' is not a type identifier

Error compiling Cython file:
------------------------------------------------------------
...
>>> initials("dedupe", 7)
('dedupe', )
>>> initials("deduplication", 7)
('dedupli', )
"""
cdef unicode ufield = _ustring(field)
    ^
------------------------------------------------------------

cpredicates.pyx:42:9: 'unicode' is not a type identifier

Error compiling Cython file:
------------------------------------------------------------
...


cdef unicode _ustring(basestring s):
    if type(s) is unicode:
        # fast path for most common case(s)
        return <unicode>s
               ^
------------------------------------------------------------

cpredicates.pyx:51:16: 'unicode' is not a type identifier

0 个答案:

没有答案