0

I have the code below to write out a list of N-grams in Python.

from nltk.util import ngrams

def word_grams(words, min=1, max=6):
    s = []
    for n in range(min, max):
        for ngram in ngrams(words, n):
            s.append(' '.join(str(i) for i in ngram))
    return s
email = open("output.txt", "r")
for line in email.readlines():
    with open('file.txt', 'w') as f:
            for line in email:
                prnt = word_grams(email.split(' '))
                f.write("prnt")
email.close()
f.close()

when I print out the word_grams it prints out the files correctly but when it comes to writing the output into files.txt it doesn't work. The "file.txt" is empty.

So I guess the problem must be within these lines of codes:

for line in email.readlines():
    with open('file.txt', 'w') as f:
            for line in email:
                prnt = word_grams(email.split(' '))
                f.write("prnt")
email.close()
f.close()
7
  • How does it "not work?" Commented Nov 8, 2016 at 9:03
  • @juanpa.arrivillaga the code runs correctly w/o any errors and the file.txt is empty. Commented Nov 8, 2016 at 9:03
  • 1
    You are writing the string "prnt" to your file. What you really want is to write the variable prnt. Simply remove the quotation marks. Commented Nov 8, 2016 at 9:04
  • 1
    @adelrahimi add that to the question not as a comment. Commented Nov 8, 2016 at 9:04
  • 1
    After having a second look, your code does not make much sense. You are first iterating over lines in email, in the iteration, ou iterate over the lines again and inside that iteration split the source string into words. What are you trying to accomplish? Commented Nov 8, 2016 at 9:06

3 Answers 3

1

1) the final f.close() does something else than what you want (f inside the loop is another object)

2) You name the file "file.txt" but want the output in "files.txt". Are you sure that you are looking in a correct file?

3) You are overwriting the file for each line in the email. Perhaps the with statement for "file.txt" should be outside the loop.

4) You are writing "prnt" instead of prnt

Something like this?

def word_grams(words, min=1, max=6):
    s = []
    for n in range(min, max):
        for ngram in ngrams(words, n):
            s.append(' '.join(str(i) for i in ngram))
    return s

with open("output.txt", "r") as email:
    with open('file.txt', 'w') as f:
        for line in email.readlines():
            prnt = word_grams(line.split(' '))
            for ngram in prnt:
                f.write(ngram)
Sign up to request clarification or add additional context in comments.

Comments

1

I don't know what you are trying to accomplish exactly, but it seems that you would like to apply the function word_grams to every word in the file "output.txt" and save the output to a file called "file.txt", probably one item per line.

With these assumptions, I would recommend to rewrite your iteration in this manner:

words = []
# load words from input
with open("output.txt") as f:
    for line in f:
        words += line.strip().split(" ")
# generate and save output
grams = apply(word_grams, words)
with open("file.txt", "w") as f:
    f.write("\n".join(grams))

However, this code assumes that the function word_grams is working properly.

Comments

0

Your code in loop:

for line in email:

did not run!

Because after email.readlines()run,the variable email is empty. You can do some test like fallows:

email = open("output.txt", "r")
for line in email.readlines():
    print '1'
    for line in email:
        print '2'

if you have 3 lines in your output.txt,after you run this test,you will get:

1
1
1

in the output.

And you can do a test like this:

email = open("output.txt", "r")
email.readlines()

you will see a list with the lines in your output.txt.

but when you run email.readlines()again,you will get an empty list!

so,there should be the problem.your variable email is empty in your second loop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.