Program to reorder words in text

OK I managed to get it to recognize python.

I got the three arrows prompt. Any suggestions on whats next?

I unzipped some exercises from Google but having trouble. So far “google” is not defined.
Also Hello is not defined.

At the >>> prompt you can start typing your program



blah = 42
print (blah)


You probably would not do a long program that way, instead use


python program.py

I think

Brian

import random, string

Read in the file and shuffle the words around; keeps punctuation

file = open(“file.txt”, “r”)
text = file.read()
words = text.split()

random.shuffle(words)

Make all words lowercase unless they follow punctuation.

sentence_terminators = [‘.’,‘!’,‘?’]
prev_word = “”
out =
for word in words:
first_char = “”

if prev_word == "" or prev_word[len(prev_word)-1] in sentence_terminators:
	first_char = word[0].upper()
else:
	first_char = word[0].lower()

out.append(first_char + word[1:])
prev_word = word

Output the string with a space between every word, and end the sentence

with a random piece of punctuation.

print (string.join(out) + random.choice(sentence_terminators))

The above is Jragons program copied.

So I can paste this after the arrows, with a space after the arrow, as in Jragons post, or no spaces after the arrows?

How would I make my source and destination folders? Lets say they are folders named “source” and "result"on my C drive no subdirectory.

I recommend cut and pasting the code in a text editor and saving it as say shuffle.py.
Be sure to preserve tabs as whitespace matters in Python
Then in dos type python shuffle.py

For the file.txt I would use the full path C:\source\inout.txt
The code doesn’t write an output file

Brian

I recommend cut and pasting the code in a text editor and saving it as say shuffle.py.
Be sure to preserve tabs as whitespace matters in Python
Then in dos type python shuffle.py

For the file.txt I would use the full path C:\source\inout.txt
The code doesn’t write an output file

Brian

Save it inside Python, how?
It will shuffle whatever is in the file named file.txt? Where is that file kept again?
What is inout.txt?
What do you mean it doesn’t write an output file?
Thx

No, save the code in a text file outside of python (e.g. shuffle.py)
Then on the dos command line type


python shuffle.py

I meant “input.txt” (that is, the name of the file you want to shuffle) If you put that in the c:\source directory you’d change the code to


file = open("c:\source\input.txt", "r")

it might be


file = open("c:\\source\\input.txt", "r")

The code in question uses a print function, so it just goes to the screen. To write to a file instead of the print you would add


outfile = open("c:\result\output.txt", "w")

near the top and change the print to



outfile.write(string.join(out) + random.choice(sentence_terminators))
outfile.close()


I think - again I am not an expert

Brian

It sounds easy when you say it. :smiley: I haven’t done any coding since Turbo Pascal, whenever the hell that was current.

Still. I’m newly retired. Why not use some of the new time to learn a language?

While getting an answer to the actual question, what I’m wondering is what use a random block of words is for use in writing?

Honest question, not saying there isn’t one, just curious what it is.

Good question! I was wondering too!

I have used a travesty generator to come up with fantasy names for characters. I used an original file with 100 Chinese names, and the program ground out a whole list of pseudo-Chinese names, words that followed most of the alphabetical patterns of the real names. A fun exercise!

I write songs. I’m trying to come up with lyrics for a lot of my music so I can get on with it.

I like to find accidental sense in word combinations (a la william burroughs) and I figure that my own notes and texts, poetry, essays will be the best source. It’ll have all the cool things I thought of, and my vocabulary and I can tweak all kinds of things out of it I never would have thought of.

Or I can use other texts that have a lot of, for instance, one syllable words, to make something happen.

It’s more profound than your intentional “deep” thoughts, way more than you’d think before you tried it.

There was a site for it once years ago, but it fell off.