简体   繁体   中英

Looping a replace function in python for different random outputs

hey there guys I appologise in advance if I can't quite articulate my problem effectively, but I'm at a point where I need help.

Basically I want to go through a list of text and replace certain elements with a random selection of words. I'm able to randomly pull the words from a list, but once I then assign them to a particular word they're all the same.

IE I want to change this:

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

to this:

  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 

my code so far is this:

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())

and the output I get is this:

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  

In the program I want to replace all of the NN with different words from the get_noun() function, but it seems to only pull one into the buffer and use that for all of them.

Anyone know where I'm going wrong? I suspect it's something to do with:

 okay = [w.replace('NN', get_noun()) for w in readS]

but I don't know how to re-loop it to yield a different result for each 'NN'.

If you could help me with this I would literally be so happy!!!!

cheers.

ELlliot

EDIT:

here is the code I copied from 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.

It's ALIVE. Thanks so much to you guys for all your help. I got this one to work, and being the script kiddie I am I'm going to keep it like this hahaha. I'll try my best to understand everything you guys showed me, but I'm content with having it just run as such. I hope that's not poor etiquette! All legends!

I don't know if you know about dictionaries, but given you appear to be using nltk or something like it, I'm going to assume yes. Here's a version that maintains a dictionary called Words[code] , where code is something like 'NN'. Each entry is a list of words, so you can randomly choose one.

You can read in multiple files, per code, etc. I'm writing the files with some dummy data - you should probably delete that before you try to use it.

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')

In case you are interested I have created a code which does exactly what you want ( 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM