def undo_inverted_index(inverted_index): """ The purpose of the function is to 'undo' and inverted index. It inputs an inverted index and returns the original string. """ #create empty lists to store uninverted index word_index = [] words_unindexed = [] #loop through index and return key-value pairs for k,v in inverted_index.items(): for index in v: word_index.append([k,index]) #sort by the index word_index = sorted(word_index, key = lambda x : x[1]) #join only the values and flatten for pair in word_index: words_unindexed.append(pair[0]) words_unindexed = ' '.join(words_unindexed) return(words_unindexed) #create 'original_abstract' feature ai_search['original_abstract'] = list(map(undo_inverted_index, ai_search['abstract_inverted_index']))