在python中循环替换函数以获得不同的随机输出
嘿,伙计们,如果我不能很好地表达我的问题,我会提前道歉,但我现在需要帮助 基本上,我想浏览文本列表,并用随机选择的单词替换某些元素。我可以从列表中随机抽取单词,但是一旦我把它们分配到一个特定的单词,它们都是一样的 也就是说,我想改变这一点:在python中循环替换函数以获得不同的随机输出,python,Python,嘿,伙计们,如果我不能很好地表达我的问题,我会提前道歉,但我现在需要帮助 基本上,我想浏览文本列表,并用随机选择的单词替换某些元素。我可以从列表中随机抽取单词,但是一旦我把它们分配到一个特定的单词,它们都是一样的 也就是说,我想改变这一点: 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
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())
在程序中,我想用get_noon()函数中的不同单词替换所有NN,但它似乎只将一个单词拉入缓冲区,并将其用于所有单词
有人知道我哪里出错了吗?我怀疑这与:
okay = [w.replace('NN', get_noun()) for w in readS]
但我不知道如何重新循环它,以便为每个“NN”生成不同的结果
如果你能帮我,我真的会很高兴的
干杯
埃利奥特
编辑:
以下是我从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')
如果您感兴趣,我已经创建了一个代码,它完全符合您的要求(
python3
)
哇!非常感谢。不幸的是,我认为我运行的是最新版本的Python,而不是2.7。不管怎样,我在编译代码时出错了。回溯(最后一次调用):文件“novgentest2.py”,第16行,在中以open(infle,'r')作为f:TypeError:强制使用Unicode:需要字符串或缓冲区,文件foundHrmmmm我仍然收到相同的错误。我将编辑我的主要帖子,向您展示我是如何设计代码的。你能告诉我哪里出错了吗?再次感谢你的帮助!!!我想这是因为我试图打开文件两次!嘿,伙计,我在用nltk,但我不知道字典。正如您可能知道的,我昨天才开始使用lib,现在仍在努力学习python。非常感谢你帮我把这个打印出来!
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')
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.