"""
Word Rearranger by Michael Huynh

Rearranges the letters inside words for the idea that wrods can siltl
be rbedalae if the fonrt and lsat lteters are kpet cnotsnat.

Version 1 (09/28/04)
Version 2 (09/29/04) - Fixed bug when punctuation counted as part of a word.
  For instance: [However,] would be arranged with the H and the , in fixed
  positions rather than the H, r, and , in fixed positions. Added a recursive
  solution. Thanks to Penguin for pointing out this bug!

Future considerations: Want to modify this program? How about adding a
degree of obfuscation to the program? This value can be adjusted to change
the *extent* to which letters can be moved around. For instance, the word:
[composition] can be extremely jumbled like: [cotiioosmpn], or it can be
mildly jumbled like: [copmsotioin]. This would be an interesting study to
determine the degree of word jumbling that can occur but still retain elements
of recognition.
"""

import random
import re, string

"--------------------------------------"
"Definitions: "

#Function to rearrange letters in a word
def shuffleWord(inWord):

    #Filter for 1, 2, or 3 characters
    if (len(inWord)<4):
        return inWord
    
    charList = list(inWord)

    #place list into new sequence
    firstChar = charList[0:1]
    lastChar = charList[len(charList)-1:]
    midCharList = charList[1:len(charList)-1]

    #-----------------------------
    #If first and last characters aren't letters or numbers, then
    #scramble where the letters start
    if(re.match('\W', lastChar[0])): #If the last character is not a word
        #send first+mid Chars into this function again for processing
        firstAndMiddleChars = midCharList
        firstAndMiddleChars.insert(0, firstChar[0])
        return (shuffleWord(firstAndMiddleChars)+lastChar[0]) # + means concat
    if(re.match('\W', firstChar[0])):
        midAndLastChars = midCharList
        midCharList.append(lastChar[0])
        return (firstChar[0]+shuffleWord(midAndLastChars))
    #-----------------------------
    
    random.shuffle(midCharList) #randomizes the middle characters
    #print firstChar, midCharList, lastChar

    #join together in one list
    wholeShuffledWord = midCharList
    wholeShuffledWord.insert(0, firstChar[0])
    wholeShuffledWord.append(lastChar[0])
    #print wholeShuffledWord

    #convert list into string and return
    shuffledWord = ''.join(wholeShuffledWord)
    #print shuffledWord
    return shuffledWord

"--------------------------------------"
"Main program: "

"!!!! For reading from a file !!!!"
"""
# Open a file, read the text
inputFileHandler = open('sometext.txt', 'r')

#Read into string
inputFile = inputFileHandler.read()
"""
"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"

"!!!! For manual input !!!!!"
inputFile = "Python is an easy to learn, powerful programming language. It has \
efficient high-level data ,structures and a simple but effective approach to \
object-oriented programming. Python's elegant syntax and dynamic typing, \
together with its interpreted nature, make it an ideal language for scripting \
and rapid application development in many areas on most platforms."
"!!!!!!!!!!!!!!!!!!!!!!!!!!!"

#Split string into and word list
listWords = inputFile.split()

#Scramble word
for eachWord in listWords:
    #Split word by characters
    print shuffleWord(eachWord), #comma supresses newline

""" Sample output:
Photyn is an esay to learn, puoerfwl pnmigrramog luganage. It has eiefifnct
hilgheve-l data ,sctuuetrrs and a spmile but efeivftce apoprcah to
ooerjtenb-eitcd pmoaingrrmg. Ph'tyons elgnaet synatx and damniyc tnyipg,
ttehoger wtih its ietnretrped nraute, make it an iaedl lggnuaae for sncirtpig
and riapd appioitclan dvlepnmeeot in many areas on msot plmtorafs.
"""




