When importing the following function to the Ipython 1.1 interpreter a SyntaxError is raised telling me that "elif len(org) >= 1:" is invalid syntax. My immediate thought is that my spacing should be incorrect but that does not appear to be the case.
If we walk through this function from the initial for loop on line 3, the error is raised on the third set of if/elif statements.
Given that this is a rather unpythonic function it may be best to break this up into several smaller functions. That should resolve the SyntaxError.
Would you agree that breaking this function into smaller functions is the best way to resolve this error? Do you know why the error was raised when the spacing and syntax appears, at least to me, to be correct?
Thank you for your time!
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
y == 'O'branch into a separate function, to make it easier to read. - jfs