I was going through a code of tokenizing a tweet in python when I encountered the following piece of code. Note that tokens_re and emoticons_re are regex objects. Since tokenize(s) returns a list therefore tokens is a list. I am kind of new to python and I am not sure is the if-else running on the elements of list. The ternary operator as mentioned doesnt have the same syntax.
def tokenize(s):
return tokens_re.findall(s)
def preprocess(s, lowercase=False):
tokens = tokenize(s)
if lowercase:
tokens = [token if emoticon_re.search(token) else token.lower() for token in tokens]
return tokens
tokenloops overtokens, and for each value oftokenwe have a ternary expression, the result of which is stored on the resulting list. Imagine parentheses around the termary.foris part of the list comprehension. The ternary operator has precedence over that.