1

I have a text file containing a very long text extracted from a book. I want to iterate over each sentence of the text and then, iterate again over every word of each sentence.

The idea is that if the word falls within category 1, it will be replaced by 'determiner', and if it falls within category 2, it will be replaced by 'non-determiner'. The for loop will then print each sentence (one sentence per line) with all the words replaced accordingly.

This is what I have tried so far:

import my_books

for sentence in my_books.sents("book1"):
        for word in my_books.words("book1"):
            if word == "the":
                print("determiner")
            else:
                print("non-determiner")

However, my output is a list with one word per line:

determiner
non-determiner
non-determiner
non-determiner
...

This is what I want instead:

determiner non-determiner non-determiner non-determiner
non-determiner non-determiner determiner non-determiner non-determiner
non-determiner non-determiner determiner ...

What can I do to get the output as I want it? I think the problem has to do with the for loop, since there's a print statement after each word is classified into a category.

Me All
  • 269
  • 1
  • 5
  • 17
  • This is not a question about looping over words in a text file; It's a question about how to avoid the newline output from the `print` function. See duplicate question: https://stackoverflow.com/q/493386/1193893 – aldo Oct 17 '18 at 01:00
  • @aldo I'm sorry! I thought my problem had to do with the loop, so that's why I used that in my title. – Me All Oct 17 '18 at 01:06

1 Answers1

0

It's a quick fix. In the print statement you can set the "end" character, which is defaulted to "\n" (new line) to whatever you want. By setting this to " ", and adding a print statement which just goes to a new line, you'll get what you want:

import my_books

for sentence in my_books.sents("book1"):
        for word in my_books.words("book1"):
            if word == "the":
                print("determiner", end=" ")
            else:
                print("non-determiner", end=" ")
        print("") # Here end="\n", and so it will go to a new line
Recessive
  • 1,780
  • 2
  • 14
  • 37