views:

102

answers:

1

i have this piece of code which only prints the line number of the incorrect words. i want it to print the linenumbers of the incorrect words from the txt file. Am i able to modify this code to do that?

# text1 is my incorrect words
# words is my text file where my incorrect word are in 

from collections import defaultdict
d = defaultdict(list)
for lineno, word in enumerate(text1):
    d[word].append(lineno)
print(d)

ive now done this but this prints the character its located like the place of the word rather then the line. this is the code

import sys
import string

text = []
infile = open(sys.argv[1], 'r').read()
for punct in string.punctuation:
    infile = infile.replace(punct, "")
    text = infile.split()

dict = open(sys.argv[2], 'r').read()
dictset = []
dictset = dict.split()

words = []
words = list(set(text) - set(dictset))
words = [text.lower() for text in words]
words.sort()

def allwords(line):
    return line.split()
def iswrong(word):
    return word in words
for i, line in enumerate(text):
    for word in allwords(line):
        if iswrong(word):
            print(word, i))

the output of that code is

millwal    342

this is printing where the character is located not which line its located

i want it to print the line number so what do i change in my code?????

+5  A: 

You could completely rewrite this code to do what you mention -- this code's structure has no relation whatsoever to what you require.

Since you need "line numbers from a text file", you'll need an object representing the text file (either as a list of lines in memory, or as an open file object). You say you have one called words (it's not clear if that's a filename or a Python variable identifier): having the text in a file called (say, as a variable) words and the (incorrect) words in a (collection of some kind) named text1 is a truly horrible choice of names, possibly the worst I've seen in many decades -- positively misleading. Use variable names that are a better match for the variables' meaning, unless you're trying to confuse yourself and everybody else.

Given a sensibly named variable for the input text, e.g. text = open('thefile.txt'), and a decent way to determine whether a word is incorrect, say a function def iswrong(word):..., the way to code what you require becomes clear:

for i, line in enumerate(text):
    for word in allwords(line):
        if iswrong(word):
            print word, i

The allwords function could be just:

def allwords(line):
    return line.split()

if you have no punctuation (words just separated by whitespace), or

import re

def allwords(line):
    return re.findall(r'\w+', line)

using regular expressions.

If e.g. badwords is a set of incorrect words,

def iswrong(word):
    return word in badwords

or viceversa if goodwords is the set of all correct words,

def iswrong(word):
    return word not in goodwords

The details of iswrong and allwords are secondary -- as is the choice of whether to keep them as functions or just embed their code inline in the main stream of control.

Alex Martelli
it prints the words and the line number but it prints the line number of the incorrect words not the line number where the incorrect word is located in the text file how do i do that?
jad
@jad, not sure what you mean by "it". The code I've shown prints the line number in the text file (and the word number within the line, `j`, but that's easy to omit if you don't want it).
Alex Martelli
i want it to print the line number of the list of incorrect words within the textfile ?
jad
ive updated my code at the top hope you can help me out
jad
Let me edit my answer to remove that `j` which seems to be confusing you so utterly.
Alex Martelli
mines still not printing the line number it prints the character number like where the word is in the line rather then what line the word is ???
jad
@jad, what's `text` in your code now? If it's an open text file, or list of lines, what you say is simply impossible: the code has no idea "where the word is in the line" (!) so it can't possibly print that information it doesn't have. Edit your answer to show a **complete** simplified example with small sample input and output (what does it print and what do you **wish** it printed instead) and I'll show you where your bugs are -- when you omit some code, as you've been doing throughout, it's impossible to show precisely the bugs you have in the code you're not showing;-).
Alex Martelli
i have updated my code, the output and what i want it to do plz help ASAP cheers
jad
@jad, despite my earnest entreaties you're **still** not showing us the complete code -- you're **still** hiding the parts where the bugs lie. I repeat: if `text` was an open text file or a list of lines the 2nd snippet you show would work as you mean it to; since it does, `text` is something else -- and you're carefully hiding the part of the code where you bind something wrong to `text`. How many more times do you need this trivially simple message repeated?
Alex Martelli
kk ill show u all my code
jad
that code prints the character located not the line number of the word in the txt file
jad
@jad, if that was all the code you'd get a NameError because `text` is **never** bound to any value whatsoever. So, you're lying (after wasting so much time on this crazy thread I'm not going to mince my words).
Alex Martelli
text is the words of the txt file that i open with sys.argv it is a list of all the words in the txt filei removed punctuation so the words r like this ["hello", etc...
jad
the code above works but it prints where the word is located on the line of the txtfile rather then the linenumber the word is in ive been programming for 2 months to excuse of i didtn explain right
jad
@jad, I've seen your other question (which finally does show something like complete code -- an utter mess, but presumably complete) and answered there, pointing out that `test` is first made into a list of words (completely losing any concept of lines) and then reused as the control variable in a list comprehension which leaves it bound to the last item in `words` -- so, as I said, it was _not_ what it should have been (an open text file or a list of lines)... nor can it be within your code because in another spot you're using it in a way that can **only** work if it's a list of words!!! Eek.
Alex Martelli
so what do u think i should do because uve confused me alot and i do not know much programming
jad