Is it possible to mix literal words and tags in NLTK regex
<p>I'm experimenting with NLTK to help me parse some text. As an example I have:</p>

<pre><code>1 Robins Drive owned by Gregg S. Smith was sold to TeStER, LLC of 494 Bridge Avenue, Suite 101-308, Sheltville AZ 02997 for $27,000.00.
</code></pre>

<p>using:</p>

<pre><code>words =pos_tag(word_tokenize(sentence))
</code></pre>

<p>I get:</p>

<pre><code>[('1', 'CD'), ('Robins', 'NNP'), ('Drive', 'NNP'), ('owned', 'VBN'), ('by', 'IN'), ('Gregg', 'NNP'), ('S.', 'NNP'), ('Smith', 'NNP'), ('was', 'VBD'), ('sold', 'VBN'), ('to', 'TO'), ('TeStER', 'NNP'), (',', ','), ('LLC', 'NNP'), ('of', 'IN'), ('494', 'CD'), ('Bridge', 'NNP'), ('Avenue', 'NNP'), (',', ','), ('Suite', 'NNP'), ('101-308', 'CD'), (',', ','), ('Sheltville', 'NNP'), ('AZ', 'NNP'), ('02997', 'CD'), ('for', 'IN'), ('$', '$'), ('27,000.00', 'CD'), ('.', '.')]
</code></pre>

<p>Assuming I want to extract the role of 'owner' (Gregg S. Smith), Is there a way to mix and match literals and tags perhaps of a format something like:</p>

<pre><code>'owned by{&lt;NP&gt;+}'
</code></pre>

<p>There was a previous discussion of this at <a href="http://stackoverflow.com/questions/12755638/mixing-words-and-pos-tags-in-nltk-parser-grammars">Mixing words and PoS tags in NLTK parser grammars</a>, but I'm not sure I understood the provided answer. Is this possible, and if so could you provide a code example.</p>

<p>if you combine each word and tag and then use <code>RegEx</code> to look for certain sequences of PoS tags you can get the results you are looking for.</p>

<p>for example, using the <code>words</code> variable you have defined</p>

<pre><code>joined = ' '.join([w+"&lt;"+t+"&gt;" for w,t in words])
</code></pre>

<p>would produce</p>

<pre><code>'1&lt;CD&gt; Robins&lt;NNP&gt; Drive&lt;NNP&gt; owned&lt;VBN&gt; by&lt;IN&gt; Gregg&lt;NNP&gt; S.&lt;NNP&gt; Smith&lt;NNP&gt; was&lt;VBD&gt; sold&lt;VBN&gt; to&lt;TO&gt; TeStER&lt;NNP&gt; ,&lt;,&gt; LLC&lt;NNP&gt; of&lt;IN&gt; 494&lt;CD&gt; Bridge&lt;NNP&gt; Avenue&lt;NNP&gt; ,&lt;,&gt; Suite&lt;NNP&gt; 101-308&lt;CD&gt; ,&lt;,&gt; Sheltville&lt;NNP&gt; AZ&lt;NNP&gt; 02997&lt;CD&gt; for&lt;IN&gt; $&lt;$&gt; 27,000.00&lt;CD&gt; .&lt;.&gt;'
</code></pre>

<p>Then you have to create a regular expression to find the sequence you are looking for depending on word/tag context.</p>

<p>For example, using the python RegEx module <code>re</code></p>

<pre><code>&gt;&gt;&gt; import re
&gt;&gt;&gt; m = re.match(r'.*owned&lt;VBN&gt; by&lt;IN&gt;.*?&lt;NNP&gt;', joined)
&gt;&gt;&gt; m.group(0)
'1&lt;CD&gt; Robins&lt;NNP&gt; Drive&lt;NNP&gt; owned&lt;VBN&gt; by&lt;IN&gt; Gregg&lt;NNP&gt;'
</code></pre>

python nltk