简体   繁体   English

实施Norvig拼写检查器时出错

[英]Errors in implementing Norvig Spell Checker

I've tried implementing Peter Norvig's spell checker in cython, and I get the following errors when I try to compile: 我尝试在cython中实现Peter Norvig的拼写检查器,并且在尝试编译时出现以下错误:

Error compiling Cython file:
------------------------------------------------------------
...
cimport cython
cimport re
       ^
------------------------------------------------------------

spell.pyx:2:8: 're.pxd' not found

Error compiling Cython file:
------------------------------------------------------------
...
cimport cython
cimport re
cimport collections
   ^
------------------------------------------------------------

spell.pyx:3:8: 'collections.pxd' not found

Error compiling Cython file:
------------------------------------------------------------
...
cdef char* words(char* text): return re.findall('[a-z]+', text.lower()) 

cdef char* train(char* features):
    cdef char f
    cdef char* model    
    model = collections.defaultdict(lambda: 1)
                              ^
------------------------------------------------------------

spell.pyx:10:35: Storing unsafe C derivative of temporary Python reference

Error compiling Cython file:
------------------------------------------------------------
...
   cdef char* splits
   cdef char* deletes
   cdef char* transposes
   cdef char* replaces
   cdef char* inserts
   splits     = [(word[:i], word[i:]) for i in range(len(word) + 1)]
               ^
------------------------------------------------------------

spell.pyx:30:16: Storing unsafe C derivative of temporary Python reference

Error compiling Cython file:
------------------------------------------------------------
...
   cdef char* deletes
   cdef char* transposes
   cdef char* replaces
   cdef char* inserts
   splits     = [(word[:i], word[i:]) for i in range(len(word) + 1)]
   deletes    = [a + b[1:] for a, b in splits if b]
               ^
------------------------------------------------------------

spell.pyx:31:16: Storing unsafe C derivative of temporary Python reference

Error compiling Cython file:
------------------------------------------------------------
...
   cdef char* transposes
   cdef char* replaces
   cdef char* inserts
   splits     = [(word[:i], word[i:]) for i in range(len(word) + 1)]
   deletes    = [a + b[1:] for a, b in splits if b]
   transposes = [a + b[1] + b[0] + b[2:] for a, b in splits if len(b)>1]
               ^
------------------------------------------------------------

spell.pyx:32:16: Storing unsafe C derivative of temporary Python reference

Error compiling Cython file:
------------------------------------------------------------
...
   cdef char* replaces
   cdef char* inserts
   splits     = [(word[:i], word[i:]) for i in range(len(word) + 1)]
   deletes    = [a + b[1:] for a, b in splits if b]
   transposes = [a + b[1] + b[0] + b[2:] for a, b in splits if len(b)>1]
   replaces   = [a + c + b[1:] for a, b in splits for c in alphabet if b]
               ^
------------------------------------------------------------

spell.pyx:33:16: Storing unsafe C derivative of temporary Python reference

Error compiling Cython file:
------------------------------------------------------------
...
   cdef char* inserts
   splits     = [(word[:i], word[i:]) for i in range(len(word) + 1)]
   deletes    = [a + b[1:] for a, b in splits if b]
   transposes = [a + b[1] + b[0] + b[2:] for a, b in splits if len(b)>1]
   replaces   = [a + c + b[1:] for a, b in splits for c in alphabet if b]
   inserts    = [a + c + b     for a, b in splits for c in alphabet]
           ^
------------------------------------------------------------

spell.pyx:34:16: Storing unsafe C derivative of temporary Python reference

Error compiling Cython file:
------------------------------------------------------------
...
   return set(deletes + transposes + replaces + inserts)

cdef char* known_edits2(char* word):
    cdef char* e2 
    cdef char* e1
    return set(e2 for e1 in edits1(word) for e2 in edits1(e1) if e2 in NWORDS)
                                                       ^
------------------------------------------------------------

spell.pyx:40:60: Cannot assign type 'char' to 'char *'

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

cdef char* correct(char* word):

    cdef char*  candidates;
    cdef char* key;
    candidates = known([word]) or known(edits1(word)) or known_edits2(word) or [word]
                       ^
------------------------------------------------------------

spell.pyx:50:28: Cannot assign type 'char *' to 'char'

My code is: 我的代码是:

cimport cython
cimport re
cimport collections

cdef char* words(char* text): return re.findall('[a-z]+', text.lower()) 

cdef char* train(char* features):
    cdef char f
    cdef char* model    
    model = collections.defaultdict(lambda: 1)
    for f in features:
        model[f] += 1
    return model

cdef char* NWORDS
NWORDS = train(words(file('big.txt')).read())

cdef char* alphabet = 'abcdefghijklmnopqrstuvwxyz'

cdef char* edits1(char* word):
   cdef int i
   cdef char* a
   cdef char* b
   cdef char* c
   cdef char* splits
   cdef char* deletes
   cdef char* transposes
   cdef char* replaces
   cdef char* inserts
   splits     = [(word[:i], word[i:]) for i in range(len(word) + 1)]
   deletes    = [a + b[1:] for a, b in splits if b]
   transposes = [a + b[1] + b[0] + b[2:] for a, b in splits if len(b)>1]
   replaces   = [a + c + b[1:] for a, b in splits for c in alphabet if b]
   inserts    = [a + c + b     for a, b in splits for c in alphabet]
   return set(deletes + transposes + replaces + inserts)

cdef char* known_edits2(char* word):
    cdef char* e2 
    cdef char* e1
    return set(e2 for e1 in edits1(word) for e2 in edits1(e1) if e2 in NWORDS)

cdef char* known(char* words): 
    cdef char* w
    return set(w for w in words if w in NWORDS)

cdef char* correct(char* word):

    cdef char*  candidates;
    cdef char* key;
    candidates = known([word]) or known(edits1(word)) or known_edits2(word) or [word]
    return max(candidates, key=NWORDS.get)

This is the first time I've worked in cython or anything to do with the C language. 这是我第一次使用cython或与C语言有关的东西。 By my understanding, it seems like the regular python libraries cannot be imported in cython, so how would I get the collections or regex library? 据我了解,似乎常规python库无法在cython中导入,那么我将如何获取集合或regex库?

To import a Python module or a Cython module (.pyx) from within Cython you use the "import" statement - the same one as in Python. 要从Cython中导入Python模块或Cython模块(.pyx),请使用“ import”语句-与Python中的语句相同。 To import Cython's definition file (.pxd) you use "cimport". 要导入Cython的定义文件(.pxd),请使用“ cimport”。

See: 看到:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM