当将以下函数导入Ipython 1.1解释器时,会引发一个SyntaxError,告诉我" elif len(org)> = 1:"是无效的语法。我的直接想法是我的间距应该是不正确的,但事实并非如此。
如果我们从第3行的初始for循环遍历此函数,则会在第三组if / elif语句上引发错误。
鉴于这是一个相当单一的功能,最好将其分解为几个较小的功能。这应解决SyntaxError。
您是否同意将此功能分解为较小的功能是解决此错误的最佳方法?你知道为什么在出现间距和语法时出现错误,至少在我看来是正确的吗?
感谢您的时间!
def get_it(test_this):
find_full_word = []
for token in st.tag(test_this.split()):
name = []
org = []
loc = []
if len(token) == 2:
x,y = token
if y == 'PERSON':
name.append(x)
elif y == 'ORGANIZATION':
org.append(x)
elif y == 'LOCATION':
loc.append(x)
elif y == 'O':
if len(name) >= 1:
n_term = ""
for n_item in name:
if len(n_term) > 0:
n_term = n_term + " " + n_item
else:
n_term = n_item
while len(name) > 0:
name.pop()
find_full_word.append((n_term, "PERSON")
### error is raised here ####
elif len(org) >= 1:
# # # # # # # # # # # # # # #
o_term = ""
for o_item in org:
if len(o_term) > 0:
o_term = o_term + " " + o_item
else:
o_term = o_item
while len(org) > 0:
org.pop()
find_full_word.append(o_term,"ORGANIZATION")
elif len(loc) >= 1:
l_term = ""
for l_item in loc:
if len(l_term) > 0:
l_term = l_term + " " + l_item
else:
l_term = l_item
while len(loc) > 0:
loc.pop()
find_full_word.append(l_term, "LOCATION")
if len(name) == 1:
find_full_word.append(name)
elif len(org) == 1:
find_full_word.append(org)
elif len(loc) == 1:
find_full_word.append(loc)
else:
raise UserWarning("No PERSON, ORGANIZATION, LOCATION was found")
return find_full_word
答案 0 :(得分:5)
在这里遗漏了一个右括号:
find_full_word.append((n_term, "PERSON"))