Skip to the content.

Wikipedia Search using Python

Wikipedia Search using Python

import wikipedia

def get_wikipedia_summary(topic):
    summary = wikipedia.summary(topic, sentences=3)
    return summary

def add_emoji_to_text(text):
    emoji_dict = {
        "happy": "😊",
        "sad": "😢",
        "love": "❤️",
        "angry": "😠",
        "excited": "😃",
        "surprised": "😮",
        "victory": "🏆",
        "fire": "🔥",
        "warning": "⚠️",
        "science": "🔬",
        "racing": "🏎️",
        "british": "🇬🇧",
        "won": "🥇",
        "born": "👶"
    }
    
    words = text.split()
    
    new_words = []
    
    for word in words:
        lower_word = word.lower().strip(",.!")
        if lower_word in emoji_dict:
            new_word = word + " " + emoji_dict[lower_word]
        else:
            new_word = word
        new_words.append(new_word)
    
    modified_text = " ".join(new_words)
    
    return modified_text

topic = input("Enter a Wikipedia topic: ")

summary = get_wikipedia_summary(topic)

print("\nOriginal Summary:")
print(summary)

emoji_text = add_emoji_to_text(summary)

print("\nModified Summary with Emojis:")
print(emoji_text)
Original Summary:
Sir Lewis Carl Davidson Hamilton   (born 7 January 1985) is a British racing driver competing in Formula One, driving for Mercedes. Hamilton has won a joint-record seven Formula One World Drivers' Championship titles (tied with Michael Schumacher), and holds the records for most wins (105), pole positions (104), and podium finishes (201), among others.
Born and raised in Stevenage, Hertfordshire, Hamilton joined the McLaren Young Driver Programme in 1998.

Modified Summary with Emojis:
Sir Lewis Carl Davidson Hamilton (born 7 January 1985) is a British 🇬🇧 racing 🏎️ driver competing in Formula One, driving for Mercedes. Hamilton has won 🥇 a joint-record seven Formula One World Drivers' Championship titles (tied with Michael Schumacher), and holds the records for most wins (105), pole positions (104), and podium finishes (201), among others. Born 👶 and raised in Stevenage, Hertfordshire, Hamilton joined the McLaren Young Driver Programme in 1998.