iterate through nltk dictionaries
<p>I'd like to know whether it's possible to iterate through some of the available nltk dictionaries, ie: Spanish dictionary. I'd like to find certain words matching some requirements.</p>

<p>Let's say I got this list <code>["tv", "tb", "tp", "dv", "db", "dp"]</code>, the algorithm would give me words like <code>["tapa", "tubo", "tuba", ...]</code>. As you can see, if you get rid of the vowels in those words they'll be in the initial list:</p>

<ul>
<li>tapa => tp</li>
<li>tubo => tb</li>
<li>tuba => tb</li>
</ul>

<p>Anyway, I just want to know whether it's possible to iterate through spanish words on nltk dictionaries and how, that's pretty much</p>

<p>The nltk has plenty of Spanish language resources, but I'm not aware of a dictionary. So I'll leave the choice of wordlist up to you, and go on from there.</p>

<p>In general, the nltk represents wordlists as corpus readers with the usual method <code>words()</code> for the individual words. So here's how you could find words matching your template in the English wordlist:</p>

<pre><code>templates = set(["tv", "tb", "tp", "dv", "db", "dp"])
for w in nltk.corpus.words.words("en"):
    &lt;remove vowels and check if it is in `templates`&gt;
</code></pre>

<p>I notice there's a Spanish stopwords list; here's how you would iterate over it:</p>

<pre><code>for w in nltk.corpus.stopwords.words("spanish"):
    ...
</code></pre>

<p>You could also create your own "wordlist" from a Spanish-language corpus. I used the scare quotes because the best data structure for this purpose is a set. In python, iterating over a <code>set</code> or <code>dict</code> will give you its keys:</p>

<pre><code>mywords = set(w.lower() for w in nltk.corpus.conll2002.words("esp.train"))
for w in mywords:
    ...
</code></pre>

python nltk