[英]Looping a replace function in python for different random outputs
嘿,如果我不能很好地阐明我的问题,我会提前向我表示歉意,但是我现在需要帮助。
基本上,我想浏览文本列表,并用随机选择的单词替换某些元素。 我可以从列表中随机抽取单词,但是一旦将它们分配给特定单词,它们都是一样的。
IE浏览器,我想改变这个:
DT JJ NNP DT JJ NN I PRP VBD JJ NN IN DT JJ NN CC VBD VBN IN RB CD 8 CD JJ
NN IN I PRP VBP IN PRP JJ NN PRP VBP RP DT JJ NN CC I PRP VBD VBG RB RB VB
DT NN VBD VBG RB
IN DT JJ NN CC PRP VBD VBN IN NN CC WP I PRP VBP TO VB NN
对此:
DT JJ NNP DT JJ shopping I PRP VBD JJ bag IN DT JJ house CC VBD VBN IN RB CD 8 CD JJ fun
IN I PRP VBP IN PRP JJ hatred PRP VBP RP DT JJ bum CC I PRP VBD VBG RB RB CC VB DT
到目前为止,我的代码是这样的:
import random, re
def get_noun():
infile = open('nouns.txt', 'r') #opens the file, preps it to be read
nouns = infile.readlines() # reads each line of the file
infile.close() # closes the file
index = 0 # starts at the begining of the list
while index < len(nouns): # first part of the counter
nouns[index] = nouns[index].rstrip('\n') # i believe this goes through and strips each line of the /n thing, which is usually output at the end of each line
index += 1 # counts up until it hits the final length number of the list
noun = random.choice(nouns) # outputs a random line from the list.
return noun
print (get_noun() + get_noun())
def work_plz():
fun = open('struc1.txt', 'r')
readS = fun.readlines()
fun.close
index = 0
while index < len(readS): # first part of the counter
readS[index] = readS[index].rstrip('\n') # i believe this goes through and strips each line of the /n thing, which is usually output at the end of each line
index += 1
okay = [w.replace('NN', get_noun()) for w in readS]
return okay
print (work_plz() + work_plz())
我得到的输出是这样的:
DT JJ shopping P DT JJ shopping I PRP VBD JJ shopping IN DT JJ
shopping CC VBD VBN IN RB CD 8 CD JJ shopping IN I PRP VBP IN PRP JJ
shopping
在程序中,我想用get_noun()函数中的不同单词替换所有NN,但似乎只将其中之一拉入缓冲区并将其用于所有缓冲区。
有人知道我要去哪里错吗? 我怀疑这与以下内容有关:
okay = [w.replace('NN', get_noun()) for w in readS]
但我不知道如何重新循环以为每个“ NN”产生不同的结果。
如果您可以帮助我,我真的会很高兴!
干杯。
ELlliot
编辑:
这是我从thanasissdr复制的代码:
import random
nouns = 'file/path/nouns.txt'
infile = file/path/struc1.txt'
def get_noun(file):
''' This function takes as input the filepath of the file where the words you want to replace with are stored and it returns
a random word of this list. We assume that each word is stored in a new line.'''
def random_choice(lista):
return random.choice(lista)
with open(file, 'r') as f:
data = f.readlines()
return random.choice(data).rstrip()
with open(infile, 'r') as f:
big = [] ## We are going to store in this list all the words in the "infile" file.
data = f.readlines() ## Read the file.
for row in data:
c = row.rstrip() ## Remove all the '\n' characters.'
d = ','.join(c.split()) ## Separate all the words with comma.
d = d.split(',') ## Storing all the words as separate strings in a list.
## This is the part where we replace the words that meet our criteria.
for j in range(len(d)):
if d[j]== 'NN':
d[j] = get_noun(nouns)
big.extend(d) ## join all the rows (lists) in a big list.
print (' '.join(big)) ## returns the desired output.
它还活着。 非常感谢你们的所有帮助。 我让这个工作,作为脚本小子,我要保持这种哈哈哈。 我会尽力了解你们向我展示的所有内容,但是我对让它照这样运行感到满意。 我希望这不是可怜的礼节! 所有传说!
我不知道您是否对字典有所了解,但是鉴于您似乎正在使用nltk或类似的东西,我将假设是。 这是一个维护名为Words[code]
的字典的版本,其中的代码类似于'NN'。 每个条目都是一个单词列表,因此您可以随机选择一个。
您可以按代码读取多个文件,等等。我正在使用一些伪数据编写文件-在尝试使用它之前,您可能应该删除该文件。
import random
with open('nouns.txt', 'w') as outfile:
contents = """
fox dog
shopping bag # Not sure this is right. Shopping?
fun house
hatred # Or this
bum
"""
print(contents, file=outfile)
with open('struc1.txt', 'w') as outfile:
contents = """
DT JJ NNP DT JJ NN I PRP VBD JJ NN IN DT JJ NN CC VBD VBN IN RB CD 8 CD JJ
NN IN I PRP VBP IN PRP JJ NN PRP VBP RP DT JJ NN CC I PRP VBD VBG RB RB VB
DT NN VBD VBG RB
IN DT JJ NN CC PRP VBD VBN IN NN CC WP I PRP VBP TO VB NN
"""
print(contents, file=outfile)
Words = dict()
def get_words(path, code):
words = Words[code] = []
with open(path, 'r') as infile:
for line in infile:
words.extend(line.split('#', 1)[0].strip().split())
def random_word(code):
wordlist = Words.get(code)
if wordlist is None:
return code
return random.choice(wordlist)
def work_plz(path):
with open(path, 'r') as infile:
for line in infile:
line_out = []
for token in line.strip().split():
line_out.append(random_word(token))
print(' '.join(line_out))
get_words('nouns.txt', 'NN')
work_plz('struc1.txt')
如果您有兴趣,我创建了一个代码,该代码可以完全满足您的要求( python 3
)。
import random
nouns = '/path/to/file/containing/the/nouns.txt'
infile = '/path/to/initial/file.txt'
def get_noun(file):
''' This function takes as input the filepath of the file where the words you want to replace with are stored and it returns
a random word of this list. We assume that each word is stored in a new line.'''
def random_choice(lista):
return random.choice(lista)
with open(file, 'r') as f:
data = f.readlines()
return random.choice(data).rstrip()
with open(infile, 'r') as f:
big = [] ## We are going to store in this list all the words in the "infile" file (after our desired modifications).
data = f.readlines() ## Read the initial file.
for row in data:
c = row.rstrip() ## Remove all the '\n' characters.
d = ','.join(c.split()) ## Separate all the words with comma.
d = d.split(',') ## Storing all the words as separate strings in a list.
## This is the part where we replace the words that meet our criteria.
for j in range(len(d)):
if d[j] == 'NN':
d[j] = get_noun(nouns)
big.extend(d) ## Joins all the rows (lists) in the 'big' list.
print (' '.join(big)) ## Prints out the desired output.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.