Reputation: 45
I have a python code that takes a user inputted sentence and compresses ach word into their position in the sentence in a txt file
For example if the sentence was "hello world hello world" the compression would be " 1 2 1 2".
I need a way for the compressed file to be decompress and the user given back their original sentence.
Many thanks
Upvotes: 1
Views: 255
Reputation: 11
If you enter just the position of the word into the sentence the program has not enough data to decompress the message... but if you create a txt file (that here I call "example.txt") that contains some numbered sentences, you can create a function that reads the input string and compares it with the sentences of example.txt. Now I continue the explanation of my idea with an example, 'cos it's easier to me to explain it in this way. Here's the example:
example.txt:
1. hello world
2. welcome on stack overflow
Now this is the input of the user:
hello world welcome on stack overflow
As I understand it, the compression is:
1 2 1 2 3 4
But with the change that I'm explaining you the compression will be:
1 2 1 2 3 4 1 2
1 and 2 are the number of the sentences that the program has recognized, so the uncompression function will check the finals numbers, then it will read the sentences in example.txt and will use those strings to translate the numbers into normal words. Obviously, the file example.txt will be editable by the user via the program. However, this is only an idea that needs to be developed, but I think that's a good start.
P.S.: I know, my english is not the best, sorry ;)
Upvotes: 1