Python callback in a class? -
i found code text summarization in github, , change program became tkinter program.i have problem when value in class using button widget , show result in text widget.how value of method summarize in code use tkinter button?i usualy use function or procedure nothing class , method.this code running in intrepreter.
import nltk nltk.tokenize import sent_tokenize nltk.tokenize import word_tokenize nltk.probability import freqdist nltk.corpus import stopwords class naivesummarizer: def summarize(self, input, num_sentences ): punt_list=['.',',','!','?'] summ_sentences = [] sentences = sent_tokenize(input) lowercase_sentences =[sentence.lower() sentence in sentences] #print lowercase_sentences s=list(input) ts=''.join([ o o in s if not o in punt_list ]).split() lowercase_words=[word.lower() word in ts] words = [word word in lowercase_words if word not in stopwords.words()] word_frequencies = freqdist(words) most_frequent_words = [pair[0] pair in word_frequencies.items()[:100]] # add sentences frequent words word in most_frequent_words: in range(0, len(lowercase_sentences)): if len(summ_sentences) < num_sentences: if (lowercase_sentences[i] not in summ_sentences , word in lowercase_sentences[i]): summ_sentences.append(sentences[i]) break # reorder selected sentences summ_sentences.sort( lambda s1, s2: input.find(s1) - input.find(s2) ) return " ".join(summ_sentences) if __name__ == "__main__": naivesum = naivesummarizer() text=''' see world in grain of sand, , heaven in wild flower, hold infinity in palm of hand, , eternity in hour. robin redbreast in cage puts heaven in rage. dove-house fill'd doves , pigeons shudders hell thro' regions. ''' text2 = ''' conclude aphorism of hippocrates, "qui gravi morbo correpti dolores non sentiunt, mens aegrotat" (those not perceive wasted serious illness sick in mind), , suggest in need of medicine not conquer malady, more, sharpen senses condition of inner self. fain give answer such deserve, fain reveal myself entirely, not know how set it. hardly know whether still same person whom precious letter addressed. ''' print(naivesum.summarize(text2,3)) print(naivesum.summarize(text,2))
you can not directly use summarize
function callback button; instead should wrap function calls summarize
, displays result in entry widget.
first, have add text variable widget can read , write text, this:
self.outputvar = stringvar() entry(self, textvariable=self.outputvar)
now can add callback
function button, in your other question, doing this:
def callback(self): text = "lorem ipsum" # actual input text, maybe widget num = 3 # proper value whatever result = self.summarize(text, num) # call summarize self.outputvar.set(result) # show results in widget
alternatively, use text
widget; here, inserting text handled differently though.
Comments
Post a Comment