I\'m trying to write a book cipher decoder, and the following is what i got so far.
code = open("code.txt", "r").read()
my_book = open(&q
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.
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:
I 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"
def GetBookContent(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
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:
27978 130 17479 2974 130 23081 24481 130 726 202 61 64760 278 106853 1204 38417 256 8204 97 6394 130 147 16 17084
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
//Wishes William.
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.