2016-08-22 6 views
0

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. 
+0

あなたは2つの異なる目的のために 'noun'を使用していますので、最も単純な解決法はおそらく機能しません。 – usr2564301

+0

@RadLexus問題をどのように解決したかについての私の答えを表示します。そこにはかなりまっすぐに見える;)。 – CasperTheFriendlyCode

答えて

0

さて、一度だけ、同一の項目についてお尋ねしたいという私の問題を解決するために、私は少し書き直さなければならなかった。私はまだ@Lexによって提供されている正規表現を使用しているので、彼のおかげです。しかし、私はもはや彼のre.sub()メソッドを使用していません。

import re 
l1 = re.findall('\(([^()]*)\)',story) 
l2 = [] 
for i in l1: 
if i not in l2: 
    l2.append(i) 
for i in l2: 
substr = '({})'.format(i) 
word = input('{}: '.format(i)) 
story = story.replace(substr,word) 


print(story) 
findall()

機能は「括弧の間」の正規表現に一致するすべてのインスタンスを検索し、最初のリストを構築します。次に、そのリストを繰り返して、それぞれの固有のトークンを新しいリストに入れて、重複を排除します。私は新しいトークンのリストを繰り返し、それぞれのマッチをプロンプト入力に置き換えます。これは正確に私がアプレットのために欲しかったことです。

2

私は完全なコードを参照する必要があると思いますが、私は別の提案を持っているあなたのアプローチで問題に対処するために。

ここでは正規表現を実際に使用することができます。これにより、1行(ほぼ)の置換を行うことができます。

In [1]: import re 

In [2]: story = '''        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).''' 

In [3]: def replace(match): 
    ...:  return input('{}: '.format(match.group())) 
    ...: 

In [4]: print(re.sub('\((noun|name|adjective|place)\)', replace, story)) 
(noun): monster 
(name): Strange 
(name): Strange 
(adjective): yellow 
(noun): eatery 
(place): Times Square 
           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. 

re.sub()我々は試合から抽出されたプロンプトでinput()を呼び出すために使用され、代替として呼び出し可能に受け入れます。

編集:括弧内の任意の語句に一致するように、あなただけ例えば、パターンを変更することができます。

print(re.sub('\(([^()]*)\)', replace, story)) 
+0

これは良いです!私はそれを使用しますが、私が書いたmadlibの中には、(line 1からの名詞などの)抽象的なプロンプトを使用しています。これをどのように克服したのか私の解決策を見てください。 – CasperTheFriendlyCode

+1

@CasperTheFriendlyCode括弧内の任意の句に簡単にマッチさせることができます。編集を参照してください。 –

+0

これにはどのPythonを使用していますか?私はPython 3.5.2を使用してインタプリタの代わりに.pyファイルとして実行しようとしています。それは元のストーリーを印刷しているだけです。 – CasperTheFriendlyCode

0
text = """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).""" 


match = ['monster', 'Strange', 'Strange', 'yellow', 'eatery', 'Times Square'] 

splitted = text.split() 

for i, item in enumerate(splitted): 
    if '(' in item: 
     matched = match.pop(0) 
     if not item.endswith(')'): 
      matched = '{}{}'.format(matched, item[:-1]) 
     splitted[i] = matched 

print ' '.join(splitted) 
関連する問題