Welcome to OGeek Q&A 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 Reply

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...