Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.03 KB | None | 0 0
  1. # html-to-freq-2.py
  2. import fileinput
  3. import argparse
  4. import re
  5. from collections import defaultdict
  6.  
  7. parser = argparse.ArgumentParser(description='Count word frequencies')
  8. parser.add_argument('files', metavar='file.txt', nargs='+',
  9.                     help='files to read')
  10. parser.add_argument('--word', dest='word',
  11.                     help='word to lookup')
  12.  
  13. parser.add_argument('--bigram', dest='bigram',
  14.                     help='bigram to lookup')
  15.  
  16. args = parser.parse_args()
  17.  
  18. stopwords = ['a', 'about', 'above', 'across', 'after', 'afterwards']
  19. stopwords += ['again', 'against', 'all', 'almost', 'alone', 'along']
  20. stopwords += ['already', 'also', 'although', 'always', 'am', 'among']
  21. stopwords += ['amongst', 'amoungst', 'amount', 'an', 'and', 'another']
  22. stopwords += ['any', 'anyhow', 'anyone', 'anything', 'anyway', 'anywhere']
  23. stopwords += ['are', 'around', 'as', 'at', 'back', 'be', 'became']
  24. stopwords += ['because', 'become', 'becomes', 'becoming', 'been']
  25. stopwords += ['before', 'beforehand', 'behind', 'being', 'below']
  26. stopwords += ['beside', 'besides', 'between', 'beyond', 'bill', 'both']
  27. stopwords += ['bottom', 'but', 'by', 'call', 'can', 'cannot', 'cant']
  28. stopwords += ['co', 'computer', 'con', 'could', 'couldnt', 'cry', 'de']
  29. stopwords += ['describe', 'detail', 'did', 'do', 'done', 'down', 'due']
  30. stopwords += ['during', 'each', 'eg', 'eight', 'either', 'eleven', 'else']
  31. stopwords += ['elsewhere', 'empty', 'enough', 'etc', 'even', 'ever']
  32. stopwords += ['every', 'everyone', 'everything', 'everywhere', 'except']
  33. stopwords += ['few', 'fifteen', 'fifty', 'fill', 'find', 'fire', 'first']
  34. stopwords += ['five', 'for', 'former', 'formerly', 'forty', 'found']
  35. stopwords += ['four', 'from', 'front', 'full', 'further', 'get', 'give']
  36. stopwords += ['go', 'had', 'has', 'hasnt', 'have', 'he', 'hence', 'her']
  37. stopwords += ['here', 'hereafter', 'hereby', 'herein', 'hereupon', 'hers']
  38. stopwords += ['herself', 'him', 'himself', 'his', 'how', 'however']
  39. stopwords += ['hundred', 'i', 'ie', 'if', 'in', 'inc', 'indeed']
  40. stopwords += ['interest', 'into', 'is', 'it', 'its', 'itself', 'keep']
  41. stopwords += ['last', 'latter', 'latterly', 'least', 'less', 'ltd', 'made']
  42. stopwords += ['many', 'may', 'me', 'meanwhile', 'might', 'mill', 'mine']
  43. stopwords += ['more', 'moreover', 'most', 'mostly', 'move', 'much']
  44. stopwords += ['must', 'my', 'myself', 'name', 'namely', 'neither', 'never']
  45. stopwords += ['nevertheless', 'next', 'nine', 'no', 'nobody', 'none']
  46. stopwords += ['noone', 'nor', 'not', 'nothing', 'now', 'nowhere', 'of']
  47. stopwords += ['off', 'often', 'on','once', 'one', 'only', 'onto', 'or']
  48. stopwords += ['other', 'others', 'otherwise', 'our', 'ours', 'ourselves']
  49. stopwords += ['out', 'over', 'own', 'part', 'per', 'perhaps', 'please']
  50. stopwords += ['put', 'rather', 're', 's', 'same', 'see', 'seem', 'seemed']
  51. stopwords += ['seeming', 'seems', 'serious', 'several', 'she', 'should']
  52. stopwords += ['show', 'side', 'since', 'sincere', 'six', 'sixty', 'so']
  53. stopwords += ['some', 'somehow', 'someone', 'something', 'sometime']
  54. stopwords += ['sometimes', 'somewhere', 'still', 'such', 'system', 'take']
  55. stopwords += ['ten', 'than', 'that', 'the', 'their', 'them', 'themselves']
  56. stopwords += ['then', 'thence', 'there', 'thereafter', 'thereby']
  57. stopwords += ['therefore', 'therein', 'thereupon', 'these', 'they']
  58. stopwords += ['thick', 'thin', 'third', 'this', 'those', 'though', 'three']
  59. stopwords += ['three', 'through', 'throughout', 'thru', 'thus', 'to']
  60. stopwords += ['together', 'too', 'top', 'toward', 'towards', 'twelve']
  61. stopwords += ['twenty', 'two', 'un', 'under', 'until', 'up', 'upon']
  62. stopwords += ['us', 'very', 'via', 'was', 'we', 'well', 'were', 'what']
  63. stopwords += ['whatever', 'when', 'whence', 'whenever', 'where']
  64. stopwords += ['whereafter', 'whereas', 'whereby', 'wherein', 'whereupon']
  65. stopwords += ['wherever', 'whether', 'which', 'while', 'whither', 'who']
  66. stopwords += ['whoever', 'whole', 'whom', 'whose', 'why', 'will', 'with']
  67. stopwords += ['within', 'without', 'would', 'yet', 'you', 'your']
  68. stopwords += ['yours', 'yourself', 'yourselves']
  69.  
  70.  
  71.  
  72. def stripNonAlphaNum(text):
  73.     #the characters between the [] will be stripped from the text, single quotes and dashes are not included
  74.     #since they're in the middle of compount words or contractions
  75.     return re.compile(r'[.,"?!@#$%^&*()]', re.UNICODE).sub('', text)
  76.  
  77. def splitWords(text):
  78.     return re.compile(r'\s+', re.UNICODE).split(re.compile(r'\n', re.UNICODE).sub(' ', text))
  79.  
  80. def wordListToFreqDict(wordlist):
  81.     #change the .2 to change the number of decimal places displayed
  82.     wordfreq = ['{:.4%} ({}/{})'.format((wordlist.count(p)/len(wordlist)), wordlist.count(p), len(wordlist)) for p in wordlist]
  83.     return dict(zip(wordlist,wordfreq))
  84.  
  85. def sortFreqDict(freqdict):
  86.     aux = [(freqdict[key], key) for key in freqdict]
  87.     aux.sort()
  88.     aux.reverse()
  89.     return aux
  90.  
  91. def removeStopwords(wordlist, stopwords):
  92.     return [w for w in wordlist if w not in stopwords]
  93.  
  94. def findNextWordFreq(text, word):
  95.     nextWords = defaultdict(lambda: 0)
  96.     search = f"{word}\s+([\w|'|-]+)\s+"
  97.     #print(search)
  98.     for m in re.finditer(search, text):
  99.         #print(m.group(0))
  100.         #print(m.group(1))
  101.         nextWords[m.group(1)] += 1
  102.     return nextWords
  103.  
  104. text = ""
  105. for line in fileinput.input(args.files):
  106.     text += line
  107. t = stripNonAlphaNum(text.lower())
  108.  
  109. if args.word is not None:
  110.     nextWords = findNextWordFreq(t, args.word)
  111.     print(nextWords.items())
  112.     total = 0
  113.     for w in nextWords: total += nextWords[w]
  114.     for w in nextWords: print([w, nextWords[w], '{:.4%}'.format(nextWords[w]/total)])
  115. elif args.bigram is not None:
  116.     nextWords = findNextWordFreq(t, args.bigram)
  117.     for w in nextWords: print(f"{args.bigram} {w}")
  118. else:
  119.     fullwordlist = splitWords(t)
  120.     strippedWordlist = removeStopwords(fullwordlist, stopwords)
  121.     dictionary = wordListToFreqDict(strippedWordlist)
  122.     sorteddict = sortFreqDict(dictionary)
  123.     #print("Total words: %i" % len(strippedWordlist))
  124.  
  125.     for s in sorteddict: print(str(s))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement