2

I have a grammar tag producing nltk block which is,

    sent_text = nltk.sent_tokenize(text) # this gives us a list of sentences
    # now loop over each sentence and tokenize it separately
    for sentence in sent_text:
          tokenized_text = nltk.word_tokenize(sentence)
          tagged = nltk.pos_tag(tokenized_text) 
          for word, tag in tagged:
               print(tag)

This gives me the following output,

    DT
    JJ
    NN
    NN
    VBD
    IN
    DT
    JJ
    NN

However, I want the output to single lined like

    DT JJ NN NN VBD IN DT JJ NN      

How do I do this?

Tilak Madichetti
  • 4,110
  • 5
  • 34
  • 52
Nikhil Raghavendra
  • 1,570
  • 5
  • 18
  • 25

5 Answers5

2

If you want not just print, but store the result in a string, you can use str.join() and a single list comprehension:

tags = [tag 
        for sentence in sent_text 
        for _, tag in nltk.pos_tag(nltk.word_tokenize(sentence))]
result = " ".join(tags)
print(result)

Note that the _ is a common variable name for throwaway variables.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
1

Do this

print (tag, end=" ")

That should leave a space and not go to next line.

heemayl
  • 39,294
  • 7
  • 70
  • 76
Tilak Madichetti
  • 4,110
  • 5
  • 34
  • 52
1

I dont think ultimately you want to print the whole string and keep using print(tag, end = ' ') so assign it to new variable is explained below. Initialize a variable tag_str on top and use it after print statement like this.

tag_str += ' '

tag_str += tag
Marlon Abeykoon
  • 11,927
  • 4
  • 54
  • 75
1

If you aren't using Python 3, you could write directly to sys.stdout, which would enable you to skip the newline character.

mcchucklezz
  • 376
  • 1
  • 16
1

If you are using python 2.x use print(tag,), the , puts output in the same line. so you can use print (tag + ' ',)

If you are using python 3 use print(tag, end="") or print(tag, end=" ") depending on whether you want whitespace or not.

Aravind Voggu
  • 1,491
  • 12
  • 17