﻿ NLTK corpus: Print the first 15 random combine labeled male and female names from names corpus - 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.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

﻿