import sys
import re

def main(argv):
    # Message to perform sentiment analysis on
    message = argv[0] if len(argv) > 0 else ""

    if message == "":
        print("Usage: python analyze.py [message]")
        sys.exit(1)

    # Load the positive and negative words
    words = {}
    with open("words/positive.txt") as file:
        for line in file:
            words[line.rstrip()] = 1

    with open("words/negative.txt") as file:
        for line in file:
            words[line.rstrip()] = -1

    # Perform the sentiment analysis
    score = 0
    found = 0
    for w in message.split():
        # Only keep alphanumeric characters and some punctuation.
        w = re.sub(r'[^\-\'+\w]', '', w).lower()
        if w in words:
            score += words[w]
            found += 1

    print(round(score / float(found) if found != 0 else 0, 2))

if __name__ == "__main__":
    main(sys.argv[1:])

import sys
import re

def main(argv):
    # Load the positive and negative words
    words = {}
    with open("words/positive.txt") as file:
        for line in file:
            words[line.rstrip()] = 1

    with open("words/negative.txt") as file:
        for line in file:
            words[line.rstrip()] = -1

    # Perform the sentiment analysis
    for message in sys.stdin:
        score = 0
        found = 0
        for w in message.split():
            # Only keep alphanumeric characters and some punctuation.
            w = re.sub(r'[^\-\'+\w]', '', w).lower()
            if w in words:
                score += words[w]
                found += 1

        print(round(score / float(found) if found != 0 else 0, 2))

if __name__ == "__main__":
    main(sys.argv[1:])

Read from standard input and perform on each line
Read from standard input and perform on each line

The analyze script can now be run with, for example
- echo "Message" | python analyze.py
- cat | python analyze.py (enter messages and end with Ctrl-D)
- python analyze.py < filename
- MapReduce (at some point)

  import sys
  import re
  
  def main(argv):
-     # Message to perform sentiment analysis on
-     message = argv[0] if len(argv) > 0 else ""
- 
-     if message == "":
-         print("Usage: python analyze.py [message]")
-         sys.exit(1)
- 
      # Load the positive and negative words
      words = {}
      with open("words/positive.txt") as file:
          for line in file:
              words[line.rstrip()] = 1
  
      with open("words/negative.txt") as file:
          for line in file:
              words[line.rstrip()] = -1
  
      # Perform the sentiment analysis
+     for message in sys.stdin:
-     score = 0
+         score = 0
-     found = 0
+         found = 0
-     for w in message.split():
+         for w in message.split():
-         # Only keep alphanumeric characters and some punctuation.
+             # Only keep alphanumeric characters and some punctuation.
-         w = re.sub(r'[^\-\'+\w]', '', w).lower()
+             w = re.sub(r'[^\-\'+\w]', '', w).lower()
-         if w in words:
+             if w in words:
-             score += words[w]
+                 score += words[w]
-             found += 1
+                 found += 1
  
-     print(round(score / float(found) if found != 0 else 0, 2))
+         print(round(score / float(found) if found != 0 else 0, 2))
  
  if __name__ == "__main__":
      main(sys.argv[1:])
  
Read from standard input and perform on each line
## Code Before:
import sys
import re

def main(argv):
    # Message to perform sentiment analysis on
    message = argv[0] if len(argv) > 0 else ""

    if message == "":
        print("Usage: python analyze.py [message]")
        sys.exit(1)

    # Load the positive and negative words
    words = {}
    with open("words/positive.txt") as file:
        for line in file:
            words[line.rstrip()] = 1

    with open("words/negative.txt") as file:
        for line in file:
            words[line.rstrip()] = -1

    # Perform the sentiment analysis
    score = 0
    found = 0
    for w in message.split():
        # Only keep alphanumeric characters and some punctuation.
        w = re.sub(r'[^\-\'+\w]', '', w).lower()
        if w in words:
            score += words[w]
            found += 1

    print(round(score / float(found) if found != 0 else 0, 2))

if __name__ == "__main__":
    main(sys.argv[1:])

## Instruction:
Read from standard input and perform on each line
## Code After:
import sys
import re

def main(argv):
    # Load the positive and negative words
    words = {}
    with open("words/positive.txt") as file:
        for line in file:
            words[line.rstrip()] = 1

    with open("words/negative.txt") as file:
        for line in file:
            words[line.rstrip()] = -1

    # Perform the sentiment analysis
    for message in sys.stdin:
        score = 0
        found = 0
        for w in message.split():
            # Only keep alphanumeric characters and some punctuation.
            w = re.sub(r'[^\-\'+\w]', '', w).lower()
            if w in words:
                score += words[w]
                found += 1

        print(round(score / float(found) if found != 0 else 0, 2))

if __name__ == "__main__":
    main(sys.argv[1:])
