Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

nlp - Find the word behind the index numbers in python

I have a question as I am new at the NLP. I have a dataframe consists of 2 columns. The first has a sentence lets say "Hello World, how are you?" and the second column has [6,7,8,9,10] which represents the word "World" (index positions) from the first sentence. Is there any function in python which can give me the opportunity to recognize and appear the word that in each row is specified by the index numbers?

Thank you

https://ibb.co/LRnWM7G

question from:https://stackoverflow.com/questions/65645766/find-the-word-behind-the-index-numbers-in-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I think it's what you need (updated according to comments)

dataframe = ["Hello World",[6,7,8,9,10]],
                    ["Hello World",[6,7,8,9]],
                    ["Hello World",[7,8,9,10]]
resultdataframe=[]

def textbyindexlist(df):
    text = df[0]
    letterindex=df[1]
    newtext=""
    for ind in letterindex:
        newtext = newtext + text[ind]
    return newtext
for i in dataframe:
    resultdataframe.append(textbyindexlist(i))
print (resultdataframe)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...