Arnold/book Cipher With Python
Solution 1:
You already know how to read in the book file, break it into lines, and break each of those into words.
If paragraphs are defined as being separated by "\n\n"
, you can split
the contents of the book file on that, and break each paragraph into lines. Or, after you break the book into lines, any empty line signals a change of paragraph.
Solution 2:
This may be quite a late answer; but better now than never I guess?
I completed a book cipher implementation, that I would like to say; does exactly what you are asking after.
- It takes a book file (in my example test run, at the bottom "Shakespeare.txt")
- and a message (words)
- finds the index of each words typed in, and gets the same words from that -> but in the book.
- It prints out the book's-Words-Indexes.
Give it a look? Hope it helps!
I worked as crazy on this one. Took me, literally Years to complete this!
Have a great day!
I believe a answer with working code, or at least a try in that direction That's why I'm providing this code too; Really hope it helps both you & The future viewers!
MAJOR EDIT:
Edit 1: Providing code here, easier for the future viewers; and for you hopefully:
Main Program:
Originally from my GitHub: https://github.com/loneicewolf/Book-Cipher-PythonI shortened it and removed stuff (that wasn't needed in this case) to make it more 'elegant' (& hopefully it became that too)
# Replace "document1.txt" with whatever your book / document's name is.
BOOK="document1.txt"# This contains your "Word Word Word Word ...." I believed from the very start that you meant, they are not the same - (obviously)# Read book into "boktxt"defGetBookContent(BOOK):
ReadBook = open(BOOK, "r")
txtContent_splitted = ReadBook.read();
ReadBook.close()
Words=txtContent_splitted
return(txtContent_splitted.split())
boktxt = GetBookContent(BOOK)
words=input("input text: ").split()
print("\nyou entered these words:\n",words)
i=0
words_len=len(words)
for word in boktxt:
while i < words_len:
print(boktxt.index(words[i]))
i=i+1
x=0
klist=input("input key-sequence sep. With spaces: ").split()
for keys in klist:
print(boktxt[int(klist[x])])
x=x+1
TEST ADDED:
EDIT: I think I could provide a sample run with a book, to show it in action, at least.. Sorry for not providing this sooner:
I executed the python script: and I used Shakespeare.txt
as my 'book' file.
input text: King of dragon, lord of gold, queen of time has a secret, which 3 or three, can hold if two of them are dead
(I added a digit in it too, so it would prove that it works with digits too, if somebody in the future wonders)
and it outputs your book code:
27978130174792974 130230812448113072620261647602781068531204 384172568204 976394 1301471617084
For example:
27978
means the 27978'th
word
in Shakespeare.txt
To decrypt it, you feed in the book code and it outputs the plain text! (the words you originally typed in)
input key-sequence sep. With spaces: 27978 130 17479 2974 130 23081 24481 130 726 202 61 64760 278 106853 1204 38417 256 8204 97 6394 130 147 16 17084
-> it outputs ->
King of dragon, lord of gold, queen of time has a secret, which 3 or three, can hold if two of them are dead
//WishesWilliam.
Post a Comment for "Arnold/book Cipher With Python"