Install dependencies to local, if not pre-installed:
pip install wikipedia-api emoji
import wikipediaapi
import random
import re
from collections import Counter
# Initialize Wikipedia API, needs a valid User-Agent
user_agent = 'MyWikipediaScript/1.0 (https://example.com; myemail@example.com)' # Replace with your details
wiki = wikipediaapi.Wikipedia('en', headers={'User-Agent': user_agent})
# Function to clean/extract words from text
def extract_words(text):
text = text.lower() # Convert to lowercase
text = re.sub(r'[^a-z\s]', '', text) # Remove non-alphabet characters
words = text.split() # Split text into words
return words
# Generate random article titles from a list of common ones
def get_random_titles():
common_words = [
'apple', 'dog', 'music', 'computer', 'ocean', 'earth', 'history', 'science',
'car', 'mountain', 'sun', 'moon', 'technology', 'book', 'river', 'forest'
]
random_titles = random.sample(common_words, 5) # Pick 5 random titles from the list
return random_titles
# Get 5 Wikipedia articles by random titles and scan for the most common word
def get_common_word_from_random_articles():
titles = get_random_titles()
articles = []
print("Selected Wikipedia articles:")
for title in titles:
page = wiki.page(title)
# If the article exists, get the text content
if page.exists():
print(f"- {title}: {page.fullurl}")
articles.append(page.text)
else:
print(f"- {title}: (Article does not exist)")
if not articles:
return None
# Combine text from all articles
combined_text = ' '.join(articles)
words = extract_words(combined_text)
# Count the most common word
word_counts = Counter(words)
# Skip common English words and find the most frequent
common_words = [word for word, _ in word_counts.most_common() if len(word) > 3]
if common_words:
most_common_word = common_words[0]
return most_common_word
else:
return None
# Convert word to emoji format with the specified emojis
def word_to_emoji(word):
letter_emoji_map = {
'a': 'π
°', 'b': 'π
±', 'c': 'π
²', 'd': 'π
³', 'e': 'π
΄', 'f': 'π
΅',
'g': 'π
Ά', 'h': 'π
·', 'i': 'π
Έ', 'j': 'π
Ή', 'k': 'π
Ί', 'l': 'π
»',
'm': 'π
Ό', 'n': 'π
½', 'o': 'π
Ύ', 'p': 'π
Ώ', 'q': 'π', 'r': 'π',
's': 'π', 't': 'π', 'u': 'π', 'v': 'π
', 'w': 'π', 'x': 'π',
'y': 'π', 'z': 'π'
}
return ''.join([letter_emoji_map.get(char, char) for char in word])
# Main function
def main():
most_common_word = get_common_word_from_random_articles()
if most_common_word:
print(f"\nMost common word: {most_common_word}")
emoji_word = word_to_emoji(most_common_word)
print(f"Word in emoji form: {emoji_word}")
else:
print("No common word found or articles do not exist")
if __name__ == "__main__":
main()
Selected Wikipedia articles:
- music: https://en.wikipedia.org/wiki/Music
- car: https://en.wikipedia.org/wiki/Car
- history: https://en.wikipedia.org/wiki/History
- river: https://en.wikipedia.org/wiki/River
- sun: https://en.wikipedia.org/wiki/Sun
Most common word: music
Word in emoji form: π
Όπππ
Έπ
²