Note that there are some explanatory texts on larger screens.

plurals
  1. POoperating on embedded tuples/strings, python
    primarykey
    data
    text
    <p>say I have a tagged text (word, tag) in tuple format. i want to convert it to a string in order to make some changes to the tags. my function below only sees the last sentence in the text, i guess there is some obvious and stupid mistake which i cant realize, so plz help to make it work on the entire text.</p> <pre><code>&gt;&gt;&gt; import nltk &gt;&gt;&gt; tpl = [[('This', 'V'), ('is', 'V'), ('one', 'NUM'), ('sentence', 'NN'), ('.', '.')], [('And', 'CNJ'), ('This', 'V'), ('is', 'V'), ('another', 'DET'), ('one', 'NUM')]] def translate(tuple2string): for sent in tpl: t = ' '.join([nltk.tag.tuple2str(item) for item in sent]) &gt;&gt;&gt; print t 'And/CNJ This/V is/V another/DET one/NUM' </code></pre> <p>P.S. for those who are interested, tuple2str function is described <a href="http://docs.huihoo.com/nltk/0.9.5/guides/tag.html" rel="nofollow">here</a></p> <p><strong>EDIT:</strong> now i should convert it back to a tuple, having the same format. How do i do it?</p> <pre><code>&gt;&gt;&gt; [nltk.tag.str2tuple(item) for item in t.split()] </code></pre> <p>the one above converts in into entire tuple, but i need embedded one (the same as in the input (<code>tpl</code>) )</p> <p><strong>EDIT2:</strong> well, probably it's worth to publish the entire code: </p> <pre><code>def translate(tpl): t0 = [' '.join([nltk.tag.tuple2str(item) for item in sent]) for sent in tpl] for t in t0: t = re.sub(r'/NUM', '/N', t) t = [nltk.tag.str2tuple(item) for item in t.split()] print t </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload