こんにちは、学習者の皆さん 今日は、PythonのWonderwordsモジュールというあまり知られていない新しいモジュールについて学びます!
ワンダーワードモジュール入門
Wonderwords` はランダムな単語や文章を生成するための様々な関数を含む Python ライブラリです。
このライブラリの特徴は以下の通りです。
- 様々なカテゴリのランダムな単語や文の生成
- あなた自身のカスタム正規表現を取得する
- ライブラリを実装するための素晴らしいコマンドラインインタフェースが付属しています。
- オープンソースです。
この記事もチェック:PythonのOshashモジュールを使ってハッシュ関数を実装する方法
ワンダーワードライブラリの実装
それでは早速、wonderwordsモジュールの実装に入りましょう。
1. ランダムワードの生成
まず、wonderwordsライブラリをインポートし、ランダムワードを得るために、RandomWordというサブモジュールをインポートする必要があります。
そして、次にランダムワードオブジェクトを作成します。
そのためのコードを以下に示します。
|
1
2
|
from wonderwords import RandomWord
R_word_obj = RandomWord()
|
ランダムワードを生成するには、生成されたランダムワードオブジェクトに word 関数を使用する必要があります。
以下のコードでは、ループを使って 5 個のランダムな単語を生成しています。
|
1
2
3
|
for i in range(5):
x = R_word_obj.word()
print("Word "+str(i+1)+" : ",x)
|
このコードの出力は、以下のように5つのランダムな単語を生成します。
Word 1 : irrigation
Word 2 : porcupine
Word 3 : lightning
Word 4 : award
Word 5 : small
|
特定のカテゴリーに属する単語を生成したり、特定の始点や終点を持つ単語を生成したり、あるいはその両方を生成することもできる。
このような種類の単語をすべて1つのコードブロックに生成してみましょう。
以下のコードでは、同じ R_word_obj を使って、異なるカテゴリのランダムな単語を表示しています。
その出力はコードのすぐ下に表示されています。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
print("Words starting with 'w' and end with 'er'")
for i in range(5):
x = R_word_obj.word(starts_with="w",ends_with="er")
print("Word "+str(i+1)+" : ",x)
print(")
for i in range(5):
x = R_word_obj.word(include_parts_of_speech=["adjectives"])
print("Word "+str(i+1)+" : ",x)
print(")
for i in range(5):
x = R_word_obj.word(include_parts_of_speech=["verbs"])
print("Word "+str(i+1)+" : ",x)
print(")
for i in range(5):
x = R_word_obj.word(word_min_length=10,word_max_length=20)
print("Word "+str(i+1)+" : ",x)
|
Words starting with 'w' and end with 'er'Word 1 : winterWord 2 : wrestlerWord 3 : waferWord 4 : wrestlerWord 5 : winterGenerate random AdjectivesWord 1 : beautifulWord 2 : orangeWord 3 : old-fashionedWord 4 : ruthlessWord 5 : lopsidedGenerate random VerbsWord 1 : enlistWord 2 : tickleWord 3 : studyWord 4 : delightWord 5 : whineGenerate random words having length between 10 and 20Word 1 : sensitivityWord 2 : precedenceWord 3 : recapitulationWord 4 : co-producerWord 5 : willingness |
また、random_words関数を使用して、パラメータとして単語の数を指定すれば、毎回forループを使用せずに、大量の単語を生成することができます。
そのためのコードを以下に示す。
|
1
2
3
4
5
|
l1 = R_word_obj.random_words(10,include_parts_of_speech=["verbs"])
print("Random Verbs: ",l1)
print(")
l2 = R_word_obj.random_words(30,include_parts_of_speech=["adjectives"])
print("Random Adjectives: ",l2)
|
Random Verbs: ['manipulate', 'dive', 'shave', 'talk', 'design', 'obtain', 'wreck', 'juggle', 'challenge', 'spill']Random Adjectives: ['enchanting', 'berserk', 'tight', 'utter', 'staking', 'calm', 'wakeful', 'nostalgic', 'juicy', 'bumpy', 'unbiased', 'shiny', 'small', 'verdant', 'wanting', 'telling', 'famous', 'orange', 'quack', 'absent', 'devilish', 'overconfident', 'boundless', 'faded', 'cloudy', 'goofy', 'encouraging', 'guarded', 'vigorous', 'null'] |
2. ランダム文の生成
ランダム文の生成には、Wonderwords ライブラリから RandomSentence サブモジュールをインポートする必要がある。
そして、ランダム文オブジェクトを作成して、ランダム文を生成します。
以下はそのコードです。
|
1
2
3
4
5
|
from wonderwords import RandomSentence
R_sent_obj = RandomSentence()
for i in range(5):
x = R_sent_obj.sentence()
print("Sentence "+str(i+1)+" : ",x)
|
上記のコードは5つの平易なランダム文を生成し、その出力は以下のようになる。
Sentence 1 : The onerous dogwood twists invoice.Sentence 2 : The erect chauvinist kills mail.Sentence 3 : The noxious meet ties terminology.Sentence 4 : The accurate trail suggests bustle.Sentence 5 : The racial theism accomplishes hostel. |
また、以下のコードで形容詞を含む文を生成することができます。
出力はコードと一緒に表示される。
|
1
2
3
4
|
print("Generate sentences with adjectives")
for i in range(5):
x = R_sent_obj.bare_bone_with_adjective()
print("Sentence "+str(i+1)+" : ",x)
|
Generate sentences with adjectivesSentence 1 : The ritzy sunroom mixes.Sentence 2 : The goofy back assembles.Sentence 3 : The abusive tiara offends.Sentence 4 : The wakeful mix mixes.Sentence 5 : The itchy submitter bids. |
この記事もチェック:Pythonでstringモジュールを使ってランダムな文字列を生成する方法
まとめ
今日、あなたはWonderworldというPythonで利用可能な全く新しいライブラリについて学びました。
もっと学びたい方はこちらへどうぞ。
お読みいただきありがとうございました。