2018年5月27日日曜日

開発環境

入門 自然言語処理 (Steven Bird (著)、Ewan Klein (著)、Edward Loper (著)、萩原 正人 (翻訳)、中山 敬広 (翻訳)、水野 貴明 (翻訳)、オライリージャパン)の1章(言語処理とPython)、1.8(演習問題)23、24、25、26、27、28、29.を取り組んでみる。

コード(Emacs)

Python 3

#!/usr/bin/env python3
from nltk.book import *

print('23.')

for word in [w for w in set(text6) if w.isupper()]:
    print(word)

print('24.')
print('a.')
print([w for w in set(text6) if w.endswith('ize')])

print('b.')
print([w for w in set(text6) if w.find('z') != -1])

print('c.')
print([w for w in set(text6) if w.find('pt') != -1])

print('25.')
words = ['she', 'sells', 'sea', 'shells', 'by', 'the', 'sea', 'shore']

print('a.')
print([w for w in words if w.startswith('sh')])

print('b.')
print([w for w in words if len(w) > 4])

print('26.')
# テキスト中の単語の長さの和
s = sum([len(w) for w in text1])
print(s)
# 単語数
n = len(text1)
print(n)

# テキスト中の単語帳の平均
print(s / n)

print('27.')
# 26のを関数に


def vocab_size(text):
    return sum([len(w) for w in text]) / len(text)


print(vocab_size(text1))

print('28.')


def percent(word, text):
    return len([w for w in text if w == word]) / len(text) * 100


print(f'{percent("the", text1)}%')
print(f'{percent("a", text1)}%')
print(f'{percent("an", text1)}%')

print('29.')
# テキストに含まれる単語が増えているか(減っているか)を知ることができる
print(set(text3) < set(text1))
texts = [text1, text2, ['a', 'the']]
for t1 in texts:
    for t2 in texts:
        print(set(t1) < set(t2))

入出力結果(Terminal, Jupyter(IPython))

$ ./sample16.py
*** Introductory Examples for the NLTK Book ***
Loading text1, ..., text9 and sent1, ..., sent9
Type the name of the text or sentence to view it.
Type: 'texts()' or 'sents()' to list the materials.
text1: Moby Dick by Herman Melville 1851
text2: Sense and Sensibility by Jane Austen 1811
text3: The Book of Genesis
text4: Inaugural Address Corpus
text5: Chat Corpus
text6: Monty Python and the Holy Grail
text7: Wall Street Journal
text8: Personals Corpus
text9: The Man Who Was Thursday by G . K . Chesterton 1908
23.
NI
PIGLET
CART
BRIDE
W
CRONE
CROWD
VOICE
MONKS
WOMAN
NARRATOR
CARTOON
MINSTREL
KNIGHTS
SENTRY
FATHER
A
OLD
OFFICER
MAYNARD
RANDOM
SIR
MIDDLE
LOVELY
PRINCESS
CUSTOMER
PRINCE
MIDGET
C
BLACK
ALL
THE
U
OF
ARTHUR
PATSY
GUARDS
HISTORIAN
GUESTS
VILLAGER
GUARD
DINGO
SHRUBBER
OTHER
HEADS
CRAPPER
GUEST
HERBERT
ANIMATOR
CRASH
SOLDIER
SUN
MASTER
ZOOT
SECOND
LAUNCELOT
CAMERAMAN
GIRLS
AMAZING
BEDEVERE
KING
DEAD
KNIGHT
B
Y
O
ENCHANTER
GALAHAD
CHARACTERS
MAN
BORS
ROGER
INSPECTOR
N
ARMY
PARTY
WITCH
LUCKY
LEFT
WIFE
PRISONER
CONCORDE
BROTHER
DIRECTOR
STUNNER
TIM
GOD
SCENE
GREEN
RIGHT
ROBIN
DENNIS
BRIDGEKEEPER
I
PERSON
FRENCH
S
VILLAGERS
HEAD
WINSTON
CHARACTER
24.
a.
[]
b.
['frozen', 'amazes', 'zoo', 'zoosh', 'zoop', 'Fetchez', 'zhiv', 'zone']
c.
['temptress', 'Thpppt', 'excepting', 'temptation', 'Chapter', 'Thpppppt', 'ptoo', 'Thppt', 'empty', 'aptly', 'Thppppt']
25.
a.
['she', 'shells', 'shore']
b.
['sells', 'shells', 'shore']
26.
999044
260819
3.830411128023649
27.
3.830411128023649
28.
5.260736372733581%
1.7517895552087845%
0.22314325260046236%
29.
False
False
False
False
False
False
False
True
True
False
$

0 コメント:

コメントを投稿

関連コンテンツ