Advertisement
Guest User

Untitled

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