I found this code in the nltk documentation (http://www.nltk.org/_modules/nltk/sentiment/vader.html)
if (i < len(words_and_emoticons) - 1 and item.lower() == "kind" and \
words_and_emoticons[i+1].lower() == "of") or \
item.lower() in BOOSTER_DICT:
sentiments.append(valence)
continue
Can someone explain what this if condition means?
To insert characters that are illegal in a string, use an escape character. An escape character is a backslash \ followed by the character you want to insert.
The double slash (//) operator is used in python for different purposes. One use of this operator is to get the division result. The division result of two numbers can be an integer or a floating-point number.
In Python, you use the double slash // operator to perform floor division. This // operator divides the first number by the second number and rounds the result down to the nearest integer (or whole number).
In short, to match a literal backslash, one has to write '\\\\' as the RE string, because the regular expression must be "\\", and each backslash must be expressed as "\\" inside a regular Python string literal.
A backslash at the end of a line tells Python to extend the current logical line over across to the next physical line. See the Line Structure section of the Python reference documentation:
2.1.5. Explicit line joining
Two or more physical lines may be joined into logical lines using backslash characters (
\
), as follows: when a physical line ends in a backslash that is not part of a string literal or comment, it is joined with the following forming a single logical line, deleting the backslash and the following end-of-line character. For example:if 1900 < year < 2100 and 1 <= month <= 12 \ and 1 <= day <= 31 and 0 <= hour < 24 \ and 0 <= minute < 60 and 0 <= second < 60: # Looks like a valid date return 1
There is also the option to use implicit line joining, by using parentheses or brackets or curly braces; Python will not end the logical line until it finds the matching closing bracket or brace for each opening bracket or brace. This is the recommended code style, the sample you found should really be written as:
if ((i < len(words_and_emoticons) - 1 and item.lower() == "kind" and
words_and_emoticons[i+1].lower() == "of") or
item.lower() in BOOSTER_DICT):
sentiments.append(valence)
continue
See the Python Style Guide (PEP 8) (but note the exception; some Python statements don't support (...)
parenthesising so backslashes are acceptable there).
Note that Python is not the only programming language using backslashes for line continuation; bash, C and C++ preprocessor syntax, Falcon, Mathematica and Ruby also use this syntax to extend lines; see Wikipedia.
In this case, the \
is escaping the following new line character. Because Python cares about whitespace, this code is using this to allow code to be continued on a new line.
It is used as a line break so the if
condition can be written in the next line.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With