Need Help

A

abosalim

#Gui
import re, collections
from Tkinter import *
from nltk_lite import tokenize

def words(text): return re.findall('[a-z]+', text.lower())
def train(features):
model = collections.defaultdict(lambda: 1)
for f in features:
model[f] += 1
return model
NWORDS = train(words(file('big1.txt').read()))
alphabet = 'abcdefghijklmnopqrstuvwxyz'
def edits1(word):
s = [(word[:i], word[i:]) for i in range(len(word) + 1)]
deletes = [a + b[1:] for a, b in s if b]
transposes = [a + b[1] + b[0] + b[2:] for a, b in s if len(b)>1]
replaces = [a + c + b[1:] for a, b in s for c in alphabet if b]
inserts = [a + c + b for a, b in s for c in alphabet]
return set(deletes + transposes + replaces + inserts)
def known_edits2(word):
return set(e2 for e1 in edits1(word) for e2 in edits1(e1) if e2 in
NWORDS)
def known(words):
return set(w for w in words if w in NWORDS)
def correct(word):
candidates = known([word]) or known(edits1(word)) or known_edits2
(word) or [word]
return max(candidates, key=lambda w: NWORDS[w])
def textcorrect(str):
s=[]
for i in tokenize.whitespace(str):
s.append(correct(i))
result= " ".join(s)
return result

################################GUI
Part#####################################
class Ex3:
def __init__(self, master):
self.frame = Frame(master)
self.frame.pack()
self.field = Entry(self.frame)
self.field.pack(side=TOP)
self.contents = StringVar()
self.field.config(text=self.contents)
self.stop = Button(self.frame, text="Exit", fg="red",
command=self.frame.quit)
self.stop.pack(side=RIGHT)
self.enter = Button(self.frame, text="Correct", fg="black",
command=self.new)
self.enter.pack(side=LEFT)

def new(self):
self.string = self.contents.get()
#pass contents of textfield to words()method above
self.res= words(self.string)
self.frame2 = Frame()
self.frame2.pack()
self.words = Label(self.frame2, text=self.res, fg="blue", font=
("Arial", 16))
self.words.pack()
root = Tk()
Ex3 = Ex3(root)
root.mainloop()



It print text without correction.I think it didn't enter the method or
any method above the gui.
Please if you have any information,inform me
 
P

Piet van Oostrum

abosalim said:
a> #Gui
a> import re, collections
a> from Tkinter import *
a> from nltk_lite import tokenize
a> def words(text): return re.findall('[a-z]+', text.lower())
a> def train(features):
a> model = collections.defaultdict(lambda: 1)
a> for f in features:
a> model[f] += 1
a> return model

The return statement should be shifted left 4 spaces. Now it is inside
the loop instead of after the loop.

By the way, it is better to add python code as attachment instead of
inline text because some news software might fold the lines like in your
posting, making it difficult to reconstruct the code.

....
a> def new(self):
a> self.string = self.contents.get()
a> #pass contents of textfield to words()method above
a> self.res= words(self.string)
a> self.frame2 = Frame()
a> self.frame2.pack()
a> self.words = Label(self.frame2, text=self.res, fg="blue", font=
a> ("Arial", 16))
a> self.words.pack()
a> It print text without correction.I think it didn't enter the method or
a> any method above the gui.
a> Please if you have any information,inform me

You don't call correct() or textcorrect() anywhere in your GUI code.
I think instead of self.res= words(self.string) you should call one of
these.
 
S

Steven D'Aprano

By the way, it is better to add python code as attachment instead of
inline text because some news software might fold the lines like in your
posting, making it difficult to reconstruct the code.

Except that some news software might not show the attachment at all.
 
A

abosalim

Except that some news software might not show the attachment at all.

I modified the method,but it can't identified it.(self.res=textcorrect
(self.string)
NameError: global name 'textcorrect' is not defined)
This is what i done:


import re, collections
from Tkinter import *
from nltk_lite import tokenize
class Ex3:
def __init__(self, master):
self.frame = Frame(master)
self.frame.pack()
self.field = Entry(self.frame)
self.field.pack(side=TOP)
self.contents = StringVar()
self.field.config(text=self.contents)
self.stop = Button(self.frame, text="Exit", fg="red",
command=self.frame.quit)
self.stop.pack(side=RIGHT)
self.enter = Button(self.frame, text="Correct", fg="black",
command=self.new)
self.enter.pack(side=LEFT)



def words(text): return re.findall('[a-z]+', text.lower())
def train(features):
model = collections.defaultdict(lambda: 1)
for f in features:
model[f] += 1
return model
NWORDS = train(words(file('big1.txt').read()))
alphabet = 'abcdefghijklmnopqrstuvwxyz'
def edits1(word):
s = [(word[:i], word[i:]) for i in range(len(word) + 1)]
deletes = [a + b[1:] for a, b in s if b]
transposes = [a + b[1] + b[0] + b[2:] for a, b in s if len(b)
replaces = [a + c + b[1:] for a, b in s for c in alphabet if
b]
inserts = [a + c + b for a, b in s for c in alphabet]
return set(deletes + transposes + replaces + inserts)
def known_edits2(word):
return set(e2 for e1 in edits1(word) for e2 in edits1(e1) if
e2 in NWORDS)
def known(words):
return set(w for w in words if w in NWORDS)
def correct(word):
candidates = known([word]) or known(edits1(word)) or
known_edits2(word) or [word]
return max(candidates, key=lambda w: NWORDS[w])
def textcorrect(str):
s=[]
for i in tokenize.whitespace(str):
s.append(correct(i))
result= " ".join(s)
return result
def new(self):
self.string = self.contents.get()
#pass contents of textfield to textcorrect()method above
self.res=textcorrect(self.string)
self.frame2 = Frame()
self.frame2.pack()
self.words = Label(self.frame2, text=self.res, fg="blue", font=
("Arial", 16))
self.words.pack()

root = Tk()
Ex3 = Ex3(root)
root.mainloop()
 
P

Piet van Oostrum

abosalim said:
a> I modified the method,but it can't identified it.(self.res=textcorrect
a> (self.string)
a> NameError: global name 'textcorrect' is not defined)
a> This is what i done:
[...]
a> class Ex3: [...]
a> def textcorrect(str):
a> s=[]
a> for i in tokenize.whitespace(str):
a> s.append(correct(i))
a> result= " ".join(s)
a> return result
a> def new(self):
a> self.string = self.contents.get()
a> #pass contents of textfield to textcorrect()method above
a> self.res=textcorrect(self.string)
a> self.frame2 = Frame()
a> self.frame2.pack()
a> self.words = Label(self.frame2, text=self.res, fg="blue", font=
a> ("Arial", 16))
a> self.words.pack()

So now you have put textcorrect inside the class Ex3. But that means
that it is an instance method now. So you need to call
self.textcorrect(self.string)
otherwise it is not known. But then you must also give it a self
parameter. The same applies to the other methods, so in textcorrect you
have to call self.correct(i) and give correct a self parameter. etc.

Or you make them all static methods and use Ex3.textcorrect().

I don't think putting all the methods in a class is a good idea, as you
only need one instance of it and most methods are static-like anyway.
Putting it in a class is a Java-ism. A module for this is more Pythonic.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top