Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

trying to generate a vector

I’m trying to generate vector from wikipediaText. But when i running my def generateVector i have an error message like: list indices must be integers or slices, not str on line word2idx[word] = idx. I’ll be very thankful if somebody help me with my task.
Here my code:

def getVocab(inputString):
    inputString = inputString.lower()
    inputString = inputString.replace("."," ")
    parsed = inputString.split()
    vocab = set(parsed)
    return vocab, parsed 

wikipediaText = 'Python is an interpreted, high-level, general-purpose programming language.   Created by Guido van Rossum and first released in 1991, Python`s design philosophy emphasizes code  readability with its notable use of significant whitespace.'

vocab, _= getVocab(wikipediaText)
print(vocab)


def generateVector(inputString):
    vocab, parsed = getVocab(inputString)
    word2idx = []
    for idx, word in enumerate(vocab):
        word2idx[word] = idx
    
    vector = []
    for word in len(range(parsed)):
        vector.append(word2idx[word])

    return vector

print(generateVector(wikipediaText))

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You define word2idx as a list (word2idx = []) and then trying to address it as a dictionary. Than you have another mistake in the next for cycle. The corrected code below works, but I’m not sure if the result is what you expected

def getVocab(inputString):
    inputString = inputString.lower()
    inputString = inputString.replace("."," ")
    parsed = inputString.split()
    vocab = set(parsed)
    return vocab, parsed 

wikipediaText = 'Python is an interpreted, high-level, general-purpose programming language.   Created by Guido van Rossum and first released in 1991, Python`s design philosophy emphasizes code  readability with its notable use of significant whitespace.'

vocab, _= getVocab(wikipediaText)
print(vocab)


def generateVector(inputString):
    vocab, parsed = getVocab(inputString)
    word2idx = {}
    for idx, word in enumerate(vocab):
        word2idx[word] = idx
    
    vector = []
    for word in parsed:
        vector.append(word2idx[word])

    return vector

print(generateVector(wikipediaText))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading