pythonprogrammer
pythonprogrammer

Reputation: 35

Recreating a sentence from its positions

sentence= raw_input("Enter a sentence: ")
sentence = sentence.lower()
sentence = sentence.split()
uniquewords = []
for word in sentence:
    if word not in uniquewords:
        uniquewords.append(word)
position = [word for word in range(len(uniquewords))]

I have the position of the uniquewords, the uniquewords and the sentence. I need now to recreate the original sentence with the positions of each word for example: I LIKE PYTHON PYTHON PYTHON LIKE HI

This would be recreated to 1 2 3 3 3 2 4. The uniquewords mean the word "PYTHON" can be repeated as many times as it likes without affecting the position of "HI" and making it in position 7 can someone please help me with recreating the original sentence with the position of each word.

Upvotes: 3

Views: 2758

Answers (5)

jtitusj
jtitusj

Reputation: 3084

Here's a simple solution:

sentence = raw_input("Enter a sentence: ")
sentence = sentence.lower().split()
uniquewords = []
for word in sentence:
    if word not in uniquewords:
        uniquewords.append(word)

positions = [uniquewords.index(word) for word in sentence]
recreated = " ".join([uniquewords[i] for i in positions])

print positions
print recreated

take note that in this implementation, position starts at 0 and not 1.

Upvotes: 0

Luc DUZAN
Luc DUZAN

Reputation: 1319

Firstly I don't know if it's because you program is unfinished or wrong but but position result in [0, 1, 2, 3] and not [0, 1, 2, 2, 2, 1, 3].

Here is a completed version of your program that work and construct back the sentence from position. I have rename position in digitSentence:

sentence= raw_input("Enter a sentence: ")
sentence = sentence.lower()
sentence = sentence.split()
uniquewords = []
for word in sentence:
    if word not in uniquewords:
        uniquewords.append(word)
wordToNum = {uniquewords[word]: word for word in range(len(uniquewords))}

digitSentence = map(wordToNum.get, sentence)

print digitSentence

print ' '.join(map(uniquewords.__getitem__, digitSentence))

Upvotes: 1

loretoparisi
loretoparisi

Reputation: 16309

You first of all need a good string tokenizer, to split the phrase in tokens in the right way. A good string tokenizes comes with nltk.

macbookproloreto:~ admin$ python
Python 2.7.10 (default, Jul 14 2015, 19:46:27) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import nltk
>>> sentence="I LIKE PYTHON PYTHON PYTHON LIKE HI" 
>>> tokens = nltk.word_tokenize(sentence)
>>> tokens
['I', 'LIKE', 'PYTHON', 'PYTHON', 'PYTHON', 'LIKE', 'HI']
>>>

As soon as you have tokens you have the positions as array indexes, so you can do your iterations.

At this point we create a set of unique words:

>>> unique=set(tokens)
>>> unique
set(['I', 'PYTHON', 'HI', 'LIKE'])

We then turn it into a list

>>> list(unique)
['I', 'PYTHON', 'HI', 'LIKE']

We know match position in the original tokens array i.e. the original sentence, so:

>>> indices = [tokens.index(t) for t in tokens]
>>> indices
[0, 1, 2, 2, 2, 1, 6]

That is your original sentence:

>>> original = " ".join([tokens[t] for t in indices])
>>> original
'I LIKE PYTHON PYTHON PYTHON LIKE HI'

Upvotes: 1

PShen Cool
PShen Cool

Reputation: 11

Is position ouput?

sentence= raw_input("Enter a sentence: ")
sentence = sentence.lower()
sentence = sentence.split()
count = 1
uniquewords = {}
for word in sentence:
    if word not in uniquewords:
        uniquewords[word] = count
        count += 1
position = [uniquewords[word] for word in sentence]

Upvotes: 0

Laur Ivan
Laur Ivan

Reputation: 4177

You just iterate through the position array and use the index of uniquewords. Something like:

reconstructed = []

for i in position:
  reconstructed.append(uniquewords[i])

print " ".join(reconstructed)

should do the trick...

Upvotes: 0

Related Questions