# Program to reorder words in text

**URL:** <https://boards.straightdope.com/t/program-to-reorder-words-in-text/728025>\
**Category:** Cafe Society\
**Created:** [August 16, 2015, 9:44pm UTC](https://boards.straightdope.com/t/program-to-reorder-words-in-text/728025 "2015-08-16T21:44:09Z")\
**Posts on this page:** 11\
**Page:** 2

<div class="post-metadata">

**Author:** ![drad\_dog](https://avatars.discourse-cdn.com/v4/letter/d/aeb1de/32.png) [@drad\_dog](https://boards.straightdope.com/u/drad_dog)\
**Post date:** [August 18, 2015, 1:07am UTC](https://boards.straightdope.com/t/program-to-reorder-words-in-text/728025/21 "2015-08-18T01:07:37Z")

</div>

> [@N9IWP](#):
>
> There were some non backwards compatible changes from Python 2 to 3  
> The big one is you can no longer do  
> print blah  
> print is now a function so you have to do  
> print (blah)
> 
> Also, Unicode is now default. Since you are not manipulating text at the byte level this shouldn’t be a problem.
> 
> If you type “python” (after cd ing to the install directory) what error do you get? You should get “\>\>\>” as a prompt.
> 
> Brian  
> (far from a Python expert)

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.

---

<div class="post-metadata">

**Author:** ![N9IWP](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/n9iwp/32/3154_2.png) [@N9IWP](https://boards.straightdope.com/u/N9IWP)\
**Post date:** [August 18, 2015, 1:35am UTC](https://boards.straightdope.com/t/program-to-reorder-words-in-text/728025/22 "2015-08-18T01:35:28Z")

</div>

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

```auto

blah = 42
print (blah)

```

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

```auto

python program.py

```

I think

Brian

---

<div class="post-metadata">

**Author:** ![drad\_dog](https://avatars.discourse-cdn.com/v4/letter/d/aeb1de/32.png) [@drad\_dog](https://boards.straightdope.com/u/drad_dog)\
**Post date:** [August 18, 2015, 2:01am UTC](https://boards.straightdope.com/t/program-to-reorder-words-in-text/728025/23 "2015-08-18T02:01:42Z")

</div>

> [@N9IWP](#):
>
> At the \>\>\> prompt you can start typing your program
> 
> ```auto
> 
> blah = 42
> print (blah)
> 
> ```
> 
> You probably would not do a long program that way, instead use
> 
> ```auto
> 
> 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.

---

<div class="post-metadata">

**Author:** ![N9IWP](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/n9iwp/32/3154_2.png) [@N9IWP](https://boards.straightdope.com/u/N9IWP)\
**Post date:** [August 18, 2015, 2:41am UTC](https://boards.straightdope.com/t/program-to-reorder-words-in-text/728025/24 "2015-08-18T02:41:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![N9IWP](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/n9iwp/32/3154_2.png) [@N9IWP](https://boards.straightdope.com/u/N9IWP)\
**Post date:** [August 18, 2015, 2:54am UTC](https://boards.straightdope.com/t/program-to-reorder-words-in-text/728025/25 "2015-08-18T02:54:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![drad\_dog](https://avatars.discourse-cdn.com/v4/letter/d/aeb1de/32.png) [@drad\_dog](https://boards.straightdope.com/u/drad_dog)\
**Post date:** [August 18, 2015, 4:07am UTC](https://boards.straightdope.com/t/program-to-reorder-words-in-text/728025/26 "2015-08-18T04:07:27Z")

</div>

> [@N9IWP](#):
>
> 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

---

<div class="post-metadata">

**Author:** ![N9IWP](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/n9iwp/32/3154_2.png) [@N9IWP](https://boards.straightdope.com/u/N9IWP)\
**Post date:** [August 18, 2015, 10:55am UTC](https://boards.straightdope.com/t/program-to-reorder-words-in-text/728025/27 "2015-08-18T10:55:14Z")

</div>

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

```auto

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

```auto

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

```

it might be

```auto

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

```auto

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

```

near the top and change the print to

```auto

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

```

I think - again I am not an expert

Brian

---

<div class="post-metadata">

**Author:** ![StarvingButStrong](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/starvingbutstrong/32/335_2.png) [@StarvingButStrong](https://boards.straightdope.com/u/StarvingButStrong)\
**Post date:** [August 18, 2015, 1:37pm UTC](https://boards.straightdope.com/t/program-to-reorder-words-in-text/728025/28 "2015-08-18T13:37:17Z")

</div>

> [@Voyager](#):
>
> You can do it in five minutes in Perl - have a hash where the key is the word and the value is the number of times it has been seen. There are some Modules to read Word files which would probably be useful for this. You could get fancy and put plurals in with non-plurals if you wanted to.

It sounds easy when you say it. 😃 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?

---

<div class="post-metadata">

**Author:** ![obfusciatrist](https://avatars.discourse-cdn.com/v4/letter/o/4af34b/32.png) [@obfusciatrist](https://boards.straightdope.com/u/obfusciatrist)\
**Post date:** [August 18, 2015, 8:16pm UTC](https://boards.straightdope.com/t/program-to-reorder-words-in-text/728025/29 "2015-08-18T20:16:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Trinopus](https://avatars.discourse-cdn.com/v4/letter/t/2bfe46/32.png) [@Trinopus](https://boards.straightdope.com/u/Trinopus)\
**Post date:** [August 18, 2015, 8:50pm UTC](https://boards.straightdope.com/t/program-to-reorder-words-in-text/728025/30 "2015-08-18T20:50:42Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![drad\_dog](https://avatars.discourse-cdn.com/v4/letter/d/aeb1de/32.png) [@drad\_dog](https://boards.straightdope.com/u/drad_dog)\
**Post date:** [August 18, 2015, 10:25pm UTC](https://boards.straightdope.com/t/program-to-reorder-words-in-text/728025/31 "2015-08-18T22:25:47Z")

</div>

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.

[Previous page](https://boards.straightdope.com/t/program-to-reorder-words-in-text/728025.md?page=1)
