madlib用のPython 3アプレットを作成しています。これまでは、テキストファイルを読み込んでランダムなストーリーを引っ張り、そのストーリーをそれを検索するためのリストに分割するようにしました。それはすべて素晴らしいです。私は、単語が必要な場所で、(名詞)、(副詞)、(名前)などを使用するように書式を設定しました。しかし、私はそれらの文字列を置き換えることにいくつかの問題があります。次に例を示します。カッコ内のキーワードをユーザー入力に置き換えます。
>>> for i in range(0,len(poss)):
... if '(' in poss[i]: poss[i] = input('{0}: '.format(poss[i].replace('(','').replace(')','')))
...
noun: monster
name.: Strange
name: Strange
adjective: yellow
noun!: eatery
place.
: Times Square
>>> poss = ' '.join(poss)
>>> print(poss)
Back to the Future
Marty was an innocent, young monster and made friends with the local scientist,
Doc Strange Doc Strange was a bit off, but he was one yellow genius. One day,
he told Marty that he had an invented a eatery Of course, Marty had to see it
in action. Late that night, they met at Times Square
これは、各オブジェクト内の「(」文字を検索して、単語と、オブジェクト全体を置き換えることが関与している可能性句読点を保存しません。また、することができます。 。?
:どのように私は効果的に括弧内に含まれるだけの部分文字列を置き換えることができます句読点/改行文字が余っとinput()
が呼び出されたときに表示されていることがわかり
参考のため、ここではファイルから引き出されたオリジナルテキストです
Back to the Future
Marty was an innocent, young (noun) and made friends with the local scientist,
Doc (name). Doc (name) was a bit off, but he was one (adjective) genius. One day,
he told Marty that he had an invented a (noun)! Of course, Marty had to see it
in action. Late that night, they met at (place).
と私の意図した結果は次のとおりです。
Back to the Future
Marty was an innocent, young monster and made friends with the local scientist,
Doc Strange. Doc Strange was a bit off, but he was one yellow genius. One day,
he told Marty that he had an invented a eatery! Of course, Marty had to see it
in action. Late that night, they met at Times Square.
あなたは2つの異なる目的のために 'noun'を使用していますので、最も単純な解決法はおそらく機能しません。 – usr2564301
@RadLexus問題をどのように解決したかについての私の答えを表示します。そこにはかなりまっすぐに見える;)。 – CasperTheFriendlyCode