Pythonで英語の形容詞と名詞のランダムな組み合わせを生成してみた

Pythonで英語の形容詞と名詞のランダムな組み合わせを生成してみた

準備

  1. 形容詞と名詞のリストをZIP形式でダウンロード
  2. 解凍してenglish-adjectives.txtとenglish-nouns.txtを確認する
  3. フォルダ名をEnglishListに変更

これで必要なファイルがDLできました。

プログラム

以下のプログラムをEnglishListフォルダと同じ階層に入れて実行します。

import random

# import adjectives list
with open("EnglishList/english-adjectives.txt") as f:
    adjectives = f.readlines()
# import nouns list
with open("EnglishList/english-nouns.txt") as f:
    nouns = f.readlines()

adj_index = random.randrange(len(adjectives))
noun_index = random.randrange(len(nouns))
theme = adjectives[adj_index]+' '+nouns[noun_index]
theme = theme.replace("\n", '')
print(theme)

実行結果

earnest dead
上記のようにランダムな形容詞と名詞の組み合わせが出力されます。

コメントする

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

Exit mobile version