2016年8月29日月曜日

開発環境

Think Python (Allen B. Downey (著)、 O'Reilly Media)のChapter 13.(Case Study: Data Structure Selection)のExercises 13-8.(No. 3056)を取り組んでみる。

Exercises 13-8.(No. 3056)

コード(Emacs)

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import random


def readFile(filename, prefix_length=2, header_end='*END*'):
    suffixes = {}
    prefix = tuple()
    with open(filename) as f:
        for line in f:
            if line.startswith(header_end):
                break
        for line in f:
            line = line.strip()
            for word in line.split():
                if len(prefix) < prefix_length:
                    prefix += (word,)
                else:
                    suffixes[prefix] = suffixes.get(prefix, []) + [word]
                    prefix = prefix[1:] + (word,)
    return suffixes


def random_text(suffixes, n=100):
    prefix = random.choice(list(suffixes.keys()))
    print(' '.join(prefix), end=' ')
    for _ in range(n):
        suffix = suffixes.get(prefix)
        if suffix is None:
            random_text(suffix, n=n - i)
            break
        word = random.choice(suffix)
        print(word, end=' ')
        prefix = prefix[1:] + (word,)

if __name__ == '__main__':
    filename = 'emma.txt'
    suffixes = readFile(filename)
    for i, k in enumerate(suffixes):
        print(k, suffixes[k])
        if i == 10:
            break
    print()

    for n in (2, 5, 10):
        suffixes = readFile(filename, prefix_length=n)
        random_text(suffixes, n=50)
        print('\n')

    print('2 files')
    filename1 = 'History_of_a_Six_Weeks_Tour_by_Shelley_and_Shelley.txt'
    for n in (2, 5, 10):
        suffixes = readFile(filename, prefix_length=n)
        suffixes1 = readFile(filename1, header_end='***')
        suffixes.update(suffixes1)
        random_text(suffixes, n=50)
        print('\n')

入出力結果(Terminal, IPython)

$ ./sample8.py
('too!--I', 'only') ['wish']
('been', 'enough."') ['"No,"']
('years', 'ago,') ['to', 'an', 'when']
('stoop', 'in') ['marrying', 'charity']
('on', 'business') ['which', 'with', 'an', 'but', 'three']
('its', 'ample') ['gardens']
('Mrs.', 'Elton!--`Jane') ['Fairfax']
('Only', 'one') ['page']
('in', 'our') ['second', 'neighbours.--My', 'lives;', 'personal', 'favour;', 'seclusion?--', 'estimation', 'dispositions,"', 'destiny;']
('speculations.', 'But') ['stranger']
('not', 'positively') ['answer']

laugh. Her attention was chained; but when tea is over." "But you need not hurry any body else. They are known to nobody else, and he did not know what a sad business." "Poor Mr. and Mrs. Dixon) excessively to come on purpose to assure you of it." When Harriet had acknowledged 

considers the variety of hands, and of bad hands too, that are to be deciphered, it increases the wonder." "The clerks grow expert from habit.--They must begin with some quickness of sight and hand, and exercise improves them. If you want any farther explanation," continued he, smiling, "they are paid for it. That is the 

in with the second-rate and third-rate of Highbury, who were calling on them for ever, and therefore she seldom went near them. But now she made the sudden resolution of not passing their door without going in--observing, as she proposed it to Harriet, that, as well as she could calculate, they were just now quite safe from any letter from 

2 files
. Indeed I must acknowledge to be some danger. We arrived at our inn by seven columns, whose branching capitals support the Project Gutenberg Web pages for current donation methods and addresses. Donations are accepted in a whirlwind high in the very spirit fails, Driven like a steam-boat, with a white expanse 

perhaps anywhere, than where he had placed himself. His tall, firm, upright figure, among the bulky forms and stooping shoulders of the elderly men, was such as Emma felt must draw every body's eyes; and, excepting her own partner, there was not one among the whole row of young men who could be compared with 

reason away, which she would rather feed and assist than not, she added soon afterwards--as if quite another subject, "It is so cold, so very cold--and looks and feels so very much like snow, that if it were to any other place or with any other party, I should really try not to go out to-day--and dissuade my father from 

$

0 コメント:

コメントを投稿