Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.94 KB | None | 0 0
  1. def isLegal(chr):
  2.     return ('A' <= chr <= 'Z') or ('a' <= chr <= 'z') or chr == "'"
  3.  
  4. def splitToWords(text):
  5.     word_list = list()
  6.     word = ""
  7.     for chr in text:
  8.         # add chr to current word if legal chr
  9.         if isLegal(chr):
  10.             word += chr
  11.             continue
  12.         # if not legal char, and word is not "", then add to word_list
  13.         if word:
  14.             word_list.append(word.lstrip("'"))
  15.         word = ""
  16.     # don't forget the last word (only happens if last chr is legal)
  17.     if word:
  18.         word_list.append(word.lstrip("'"))
  19.  
  20.     return word_list
  21.  
  22. def filter(word_list): # 4 different kind of filterring
  23.     # ususal filter if a character is uppercase
  24.     for words in word_list:  
  25.         for i in words:
  26.             if i.isupper():
  27.                 word_list.remove(words)
  28.                 break
  29.             break
  30.     # filter with string indexing if a character is uppercase
  31.     for words in word_list:
  32.         for i in range(len(words)):
  33.             if words[i].isupper():
  34.                 word_list.remove(words)
  35.                 break
  36.             break
  37.     # if the the 1st letter is not uppercase
  38.     for words in word_list:
  39.         if len(words) > 0:
  40.             if words[len(words)-1].isupper():
  41.                 word_list.remove(words)
  42.         # if the word is is empty
  43.         else:
  44.             word_list.remove(words)
  45.  
  46.     return word_list
  47.  
  48. def main():
  49.     filename = "/home/zulkifl/Desktop/ahamed/New Folder/h_7_starting/alice.txt"
  50.     text = open(filename, 'r').read()
  51.    
  52.     word_list = splitToWords(text)
  53.     word_list = filter(word_list)
  54.     word_list = filter(word_list)
  55.     word_list = filter(word_list) # filtering multiple times for the equracy
  56.    
  57.     # word_list3 = filter2(word_list2)
  58.     word_list = sorted(word_list)
  59.     counts = len(word_list)
  60.     for words in word_list:
  61.         print(words)
  62.     print(counts)
  63.  
  64. if __name__ == '__main__':
  65.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement