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 Maddy
  • 3,505
  • 2
  • 32
  • 49
Nikhil Raghavendra
  • 1,510
  • 5
  • 15
  • 24

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
  • 441,113
  • 110
  • 1,021
  • 1,148
1

Do this

print (tag, end=" ")

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

heemayl
  • 35,775
  • 6
  • 62
  • 69
Tilak Maddy
  • 3,505
  • 2
  • 32
  • 49
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
  • 10,709
  • 4
  • 53
  • 72
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
  • 368
  • 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,343
  • 11
  • 16