w3resource

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.

Sample Solution:

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])

Sample Output:

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.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/python-exercises/nltk/nltk-corpus-exercise-12.php