Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # encoding: utf-8
- from __future__ import unicode_literals, absolute_import, print_function
- import sys
- import re
- import unicodedata
- from collections import defaultdict
- from pprint import pprint
- def main():
- with open(sys.argv[1]) as f:
- text = f.read().decode('utf-8').lower()
- re_punctuation = re.compile('[^a-z0-9]')
- text = ''.join(c for c in unicodedata.normalize('NFD', text)
- if unicodedata.category(c) != 'Mn')
- text = text.replace("œ", "oe")
- text = re_punctuation.sub(' ', text).strip()
- words = re.split(r' +', text)
- df = defaultdict(list)
- for pos, word in enumerate(words):
- df[word].append(pos)
- df = sorted(df.items(), key = lambda (word,positions):(len(positions), sorted(positions)))
- pprint(df)
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment