Advertisement
Guest User

Simple Word Counter in Python 3

a guest
Jul 9th, 2015
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.59 KB | None | 0 0
  1. #NOTE : I used Notepad++ as my text editor.
  2.  
  3. #Import
  4. import sys
  5. import random
  6. import os
  7.  
  8. #Functions
  9.  
  10. def startsWith(line):
  11.         b = list(line)
  12.         return str(b[0])
  13.  
  14. def isCommand(line):
  15.         if line == str(command[0]) or line == str(command[1]) or line == str(command[2]) or line == str(command[3]) or line == str(command[4]) or line == str(command[5]) or line == str(command[6]):
  16.                 return True
  17.         else:
  18.                 return False
  19.  
  20. #Variables
  21. command = ["/reset","/quit","/example", "/help", "/lastline", "/lines", "/about"]              
  22. all_lines = []
  23.  
  24. #Main Program
  25. print("Welcome to Word Counter 2.0")
  26. while True:
  27.     #Very important stuff
  28.     line = input("\n Enter a line : ")
  29.     words = line.split()
  30.     total_words = len(words)
  31.     AlfaNum_characters = list(line)
  32.     Symbol_characters = list(line)      #Notice how these three are the same. But why?
  33.     Space_characters = list(line)  
  34.    
  35.     #Things to remove (These will help us find the number of letters, numbers, symbols or spaces in a line. PS. )
  36.     Symbols_to_remove = list("!@#$%^&*()-=_+[]{}:;<>/,.|`~?\\")*len(line)
  37.     AN_to_remove = list("QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm1234567890")*len(line)
  38.     spaces_to_remove = [" "]*len(line)
  39.    
  40.     #Note : I know this is not the best way to count them, but that's your problem. *evil face*
  41.    
  42.     #Remove symbols form the letters and numbers list of our current line
  43.     for i in Symbols_to_remove:
  44.         try:
  45.             AlfaNum_characters.remove(i)
  46.         except:
  47.             continue
  48.            
  49.     #Remove letters and numbers form the symbols list of our current line
  50.     for i in AN_to_remove:
  51.         try:
  52.             Symbol_characters.remove(i)
  53.         except:
  54.             continue
  55.    
  56.     #Remove all spaces from both of those lists
  57.     for i in spaces_to_remove:
  58.         try:
  59.             AlfaNum_characters.remove(i)
  60.             Symbol_characters.remove(i)
  61.         except:
  62.             continue
  63.    
  64.     #Remove everything except for spaces
  65.     for i in AN_to_remove:
  66.         try:
  67.             Space_characters.remove(i)
  68.         except:
  69.             continue
  70.     for i in Symbols_to_remove:
  71.         try:
  72.             Space_characters.remove(i)
  73.         except:
  74.             continue
  75.            
  76.     if len(words) == 1:
  77.         SorP_W = "Word"
  78.     else:
  79.         SorP_W = "Words"
  80.    
  81.     #Not Commands  
  82.     if startsWith(line) != "/":
  83.         print(line)
  84.         print(total_words, SorP_W)
  85.         print(len(AlfaNum_characters), "Letter(s) and/or Number(s)")
  86.         print(len(Symbol_characters), "Symbol(s)")
  87.         print(len(Space_characters), "Space(s)")
  88.        
  89.         all_lines.append(line)
  90.        
  91.     #Commands  
  92.     if startsWith(line) == "/" and isCommand(line) == False:
  93.         print("The command you entered is not valid. Please use /help to view all the commands. @Creator")
  94.    
  95.     elif line == command[0]:
  96.         os.system('cls')
  97.         print("Welcome to Word Counter 2.0")
  98.         all_lines = []
  99.    
  100.     elif line == command[1]:
  101.         sys.exit()
  102.        
  103.     elif line == command[2]:
  104.         #Will count only words. You can add other ones if you want.
  105.         examples = ["Mary has a pig. It is named Mary.", "John met Steve and Steve met Dad.", "Dad is a Jew. EW!", "You are the 10000000th visitor!. This is no joke. >>>CLICK HERE TO FIND OUT MORE<<<", "Is he called Steve or Steven? Who cares, he's gay anyway.", "David, why are you so mean?, - said Ann. What do you mean?, - responded David", "There's something odd about this painting. It has an even number of bananas and peaches. Hmm..."]
  106.         ex_num = random.randint(0,len(examples))
  107.         ex_display = examples[ex_num]
  108.         ex_display_words_num = len(ex_display.split(" "))
  109.         if ex_display_words_num == 1:
  110.             SorP_W = "Word"
  111.         else:
  112.             SorP_W = "Words"
  113.         print(ex_display)
  114.         print(ex_display_words_num, SorP_W)
  115.        
  116.     elif line == command[3]:
  117.         print("Here's a list of our commands : /help, /reset, /quit, /example, /lastline, /lines, /about. To add : /file, /sentences, /??? (Top Secret), /jokes")
  118.    
  119.     elif line == command[4]:
  120.         try:
  121.             print("The last line you entered was :", all_lines[len(all_lines)-1])
  122.         except:
  123.             print("No previous line found.")
  124.    
  125.     elif line == command[5]:
  126.         if all_lines != []:
  127.             print("The lines you entered so far are :")
  128.             line_order = 1
  129.             for i in all_lines:
  130.                 print(str(line_order)+".", i)
  131.                 line_order += 1
  132.         else:
  133.             print("No previous line found.")
  134.    
  135.     elif line == command[6]:
  136.         print("This is a simple program that I wrote in Python. This is the second version (The first one was horrible). Some time after finishing the first one, I decided to make it once again starting from 0. It was quite fun making the code sexier. The program does exactly what you think it does, but also finds the number of letters, numbers and symbols (letters and numbers are counted together because I WANT SO). Feel free to use this program as you wish (OMG will you finish already?). \n KTHXBAI")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement