NLTK corpus: Print the first 15 random combine labeled male and female names from names corpus
NLTK corpus: Exercise-12 with Solution
Write a Python NLTK program to print the first 15 random combine labeled male and labeled female names from names corpus.
Python Code :
from nltk.corpus import names import random male_names = names.words('male.txt') female_names = names.words('female.txt') labeled_male_names = [(str(name), 'male') for name in male_names] labeled_female_names = [(str(name), 'female') for name in female_names] # combine labeled male and labeled female names labeled_all_names = labeled_male_names + labeled_female_names # shuffle the labeled names array random.shuffle(labeled_all_names) print("First 15 random labeled combined names:") print (labeled_all_names[:15])
First 15 random labeled combined names: [('Erinna', 'female'), ('Ricca', 'female'), ('Lyndell', 'female'), ('Claretta', 'female'), ('Esmeralda', 'female'), ('Quinn', 'male'), ('Patrizia', 'female'), ('Stanley', 'male'), ('Candice', 'female'), ('Blair', 'male'), ('Love', 'female'), ('Rudy', 'male'), ('Rinaldo', 'male'), ('Winny', 'male'), ('Aggie', 'female')]
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Python NLTK program to find the number of male and female names in the names corpus. Print the first 10 male and female names.
Next: Write a Python NLTK program to extract the last letter of all the labeled names and create a new array with the last letter of each name and the associated label.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
Python: Tips of the Day
Given each iterable to construct a tuple by adding an index:
>>> a = ['Hello', 'world', '!'] >>> list(enumerate(a)) [(0, 'Hello'), (1, 'world'), (2, '!')]
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook