diff --git a/rikeripsum/rikeripsum.py b/rikeripsum/rikeripsum.py index e1232c4..637ceed 100644 --- a/rikeripsum/rikeripsum.py +++ b/rikeripsum/rikeripsum.py @@ -13,59 +13,59 @@ def get_data(path): return os.path.join(_ROOT, 'data', path) -def generate_paragraph(sentence_count=None): +def generate_paragraph(sentence_count=None): """Generates a 'paragraph' consisting of sentence_count 'sentences'. If sentence_count is not provided, a random number between two and ten will be chosen. - + """ if not sentence_count: sentence_count = random.choice(range(2, 10)) paragraph = '' - for i in range(sentence_count): + for _ in range(sentence_count): paragraph += ' ' + generate_sentence() return paragraph.strip() -def generate_sentence(word_count=None): +def generate_sentence(word_count=None): """Returns a 'sentence'. A sentence is actually one line of dialog by William Riker, and may in fact consiste of multiple sentences. - If a word_count is provided, the generator will attempt to return - a sentence with that number of words. Or come as close as possible. + If a word_count is provided, the generator will attempt to return + a sentence with that number of words. Or come as close as possible. Note that higher numbers of word will become increasingly unique to - the distribution and may result in a less 'random' sentence. + the distribution and may result in a less 'random' sentence. """ global lines - if not lines: + if not lines: lines = load_pickle() - if not word_count: + if not word_count: return random.choice(lines)['text'] - potential_matches = [line for line in lines if + potential_matches = [line for line in lines if line['word_count'] == word_count] - if potential_matches: + if potential_matches: return random.choice(potential_matches)['text'] - else: + else: if word_count == 1: raise ImpossibleSentenceError('Couldn\'t generate a sentence with \ the requested number of words.') - # recursive callback, trying one less words each time. + # recursive callback, trying one less words each time. return generate_sentence(word_count - 1) -def load_pickle(): - """Loads up sentence data. All methods in this class which use +def load_pickle(): + """Loads up sentence data. All methods in this class which use phrase data should call this if global lines == None. """ - f = open(get_data('riker.pickle')) + f = open(get_data('riker.pickle'), 'rb') return pickle.load(f) class ImpossibleSentenceError(Exception): - """Called when the engine is unable to fufill the request due to lack - of potential data. This would usually be raised if a number of sentences + """Called when the engine is unable to fufill the request due to lack + of potential data. This would usually be raised if a number of sentences was requested which the engine did not have data to fulfill. """ @@ -80,8 +80,8 @@ def main(): help='minimum number of words in the sentence') args = parser.parse_args() - print generate_sentence(args.count) + print(generate_sentence(args.count)) -if __name__ == '__main__': +if __name__ == '__main__': main() diff --git a/rikeripsum/script-stripper-all.py b/rikeripsum/script-stripper-all.py index a4bddf0..4bb2954 100644 --- a/rikeripsum/script-stripper-all.py +++ b/rikeripsum/script-stripper-all.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -import re import os, os.path import pickle @@ -13,54 +12,53 @@ def get_data(path): def main(): lines = {} - for season_num in range(1, 7): #check every script for all seasons + for season_num in range(1, 7): # check every script for all seasons season_dir = 'scripts/season%s' % num_words[season_num] - for script_file in os.listdir(season_dir): + for script_file in os.listdir(season_dir): record = False dbuffer = "" charname = "" f = open('scripts/season%s/%s' % (num_words[season_num], script_file)) - for data in f.readlines(): #look for pattern indicating charector name + for data in f.readlines(): # look for pattern indicating charector name if "
" in data and "
" in data:
splited = data.split("
")[-1].split("
")[0]
splited = splited.split("'S")[0].split(" ")
- if len(splited) > 5: #long list of words probably isnt a name
+ if len(splited) > 5: # long list of words probably isnt a name
continue
for name in splited:
- if name.isupper(): # charector names are uppercase
+ if name.isupper(): # charector names are uppercase
charname = name
record = True
dbuffer = ""
break
- elif record: #if name was just found, extract dialog until
' + character + r'
[ ]+(.*?)