我尝试在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'
我的代码是:
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)
这是我第一次使用cython或与C语言有关。根据我的理解,似乎常规的python库无法在cython中导入,那么我如何获得集合或正则表达式库?
答案 0 :(得分:0)
要从Cython中导入Python模块或Cython模块(.pyx),请使用“import”语句 - 与Python中相同。要导入Cython的定义文件(.pxd),请使用“cimport”。
请参阅: