user3782154
user3782154

Reputation: 37

How to make term document in python

I have 16000 record from imdb dataset like this

Movie_Name         Synops 
Alien Predator     ['great','17th', 'abigail', 'by', 'century', 'is']
Shark Exorcist     ['demonic', 'devil', 'great', 'hell', 'holy', 'nun']
Jurassic Shark     ['abandoned', 'an', 'and', 'beautiful', 'abigail',]

i don't know how to make term document for each word in Synops column like this

"great": Alien Predator,Shark Exorcist
"17th"  :Alien Predator
"abigail":Alien Predator,Jurassic Shark
.....

Upvotes: -2

Views: 79

Answers (2)

Kevin
Kevin

Reputation: 76254

data = {
    "Alien Predator": ['great','17th', 'abigail', 'by', 'century', 'is'],
    "Shark Exorcist": ['demonic', 'devil', 'great', 'hell', 'holy', 'nun'],
    "Jurassic Shark": ['abandoned', 'an', 'and', 'beautiful', 'abigail',]
}

result = {}
for movie_name, keywords in data.items():
    for keyword in keywords:
        result.setdefault(keyword, []).append(movie_name)
print(result)

Result (newlines added for clarity):

{
'great': ['Alien Predator', 'Shark Exorcist'], 
'17th': ['Alien Predator'], 
'abigail': ['Alien Predator', 'Jurassic Shark'], 
'by': ['Alien Predator'], 
'century': ['Alien Predator'], 
'is': ['Alien Predator'], 
'demonic': ['Shark Exorcist'], 
'devil': ['Shark Exorcist'], 
'hell': ['Shark Exorcist'], 
'holy': ['Shark Exorcist'], 
'nun': ['Shark Exorcist'], 
'abandoned': ['Jurassic Shark'], 
'an': ['Jurassic Shark'],
'and': ['Jurassic Shark'], 
'beautiful': ['Jurassic Shark']
}

Upvotes: 0

Vineeth Sai
Vineeth Sai

Reputation: 3455

Put them in a dictionary first or JSON. Once you have that.

dataset = {
"Alien Predator":['great','17th', 'abigail', 'by', 'century', 'is'],
"Shark Exorcist":['demonic', 'devil', 'great', 'hell', 'holy', 'nun'],
"Jurassic Shark":['abandoned', 'an', 'and', 'beautiful', 'abigail',],
}

You can easily query for values from here on.

search_word = "great"
d = [movie for movie, synops in dataset.items() if search_word in synops]

gives back ['Alien Predator', 'Shark Exorcist']

You can add them to a dictionary to make a complete result.

final_dict = {}
final_dict[search] = d

That should give you.

>>> final_dict
{'great': ['Alien Predator', 'Shark Exorcist']}

Now you can implement the same using some for loop and a list of required keywords and complete the task yourself.

Upvotes: 0

Related Questions