저는 비슷한 질문을 한 것 같습니다. 그러나이 질문과 비슷한 것은 없을 것이라고 생각합니다. 하지만 어쩌면 내가 틀렸어 그래서 내가 여기 온거야. 이것은 Mac에 있습니다. 파이썬 버전은 2.7.6입니다.NameError : 글로벌 이름 'isdigit'이 정의되어 있지 않습니다.
줄의 모든 단어에 대해 음절을 단어로 가져 오려고합니다.
from nltk.corpus import cmudict as cmud
import nltk
d=cmud.dict()
def parse_line(line):
return nltk.word_tokenize(line)
def ns(word):
if word.lower() not in d:
return -1
else:
# print lcase
return [len(list(y for y in x if isdigit(y[-1]))) for x in d[word.lower()]]
그리고 주 프로그램 test.py이 있습니다 :
이가있는 모듈 'wordfunc.py'를 쓴#!/usr/bin/env python
import wordfunc
import sys
if len(sys.argv) == 1:
print "No arguments"
exit()
## read a stream first
try:
with open(sys.argv[1]) as entrywound:
for line in entrywound:
words = wordfunc.parse_line(line)
## print word and number of syllables per word
for w in words:
print 'Word: '+w
print 'Sysllables: '
print wordfunc.ns(w)
except IOError:
print "Unable to open file. Exiting!"
exit(1)
내가 파일을 사용하여이 프로그램을 실행, I 이 오류가 발생합니다 :
./test.py /tmp/test
Word: quick
Sysllables:
Traceback (most recent call last):
File "./test.py", line 19, in <module>
print wordfunc.ns(w)
File "/Users/sid/Cave/scripts/wordfunc.py", line 14, in ns
return [len(list(y for y in x if isdigit(y[-1]))) for x in d[word.lower()]]
File "/Users/sid/Cave/scripts/wordfunc.py", line 14, in <genexpr>
return [len(list(y for y in x if isdigit(y[-1]))) for x in d[word.lower()]]
NameError: global name 'isdigit' is not defined
무엇이 잘못 되었나요? 내 말은 isdigit
이미 글로벌 기능이 아닌가요?
감사합니다. 그 매력처럼 작동합니다. isdigit()의 사용법이 파이썬의 어느 시점에서 변경되었거나 항상 이와 같이 되었습니까? –
@SidCarter 나는 그런 것을 보지 못했고, 2.x 일찍부터 파이썬을 사용 해왔다. 그러나 인간의 기억은 잘못되었다. 그리고 기계 기억 장치는 (특히 그들이 당신과 비슷한 질문을내는) . –
nltk와 함께 사용하면 어디서나이 사용법을 볼 수 있기 때문에 물어 봅니다. 한 예 - https://groups.google.com/forum/#!msg/nltk-users/mCOh_u7V8_I/HsBNcLYM54EJ 감사합니다. –