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.



Share this Tutorial / Exercise on : Facebook and Twitter

Python: Tips of the Day

Get the Key Whose Value Is Maximal in a Dictionary:

>>> model_scores = {'model_a': 100, 'model_z': 198, 'model_t': 150}
>>> # workaround
>>> keys, values = list(model_scores.keys()), list(model_scores.values())
>>> keys[values.index(max(values))]
'model_z'
>>> # one-line
>>> max(model_scores, key=model_scores.get)
'model_z'