2014-04-24 14 views
-2

我在Github中發現了代碼文本摘要,我將把這個程序改爲Tkinter程序。我在使用Button小部件在類中獲得值時出現問題,並在Text小部件中顯示結果。如何獲取此代碼中使用的方法彙總值Tkinter按鈕?我通常只使用函數或過程沒有任何類和方法。此代碼已經運行在內存中。在一個類中的Python回調?

import nltk 
from nltk.tokenize import sent_tokenize 
from nltk.tokenize import word_tokenize 
from nltk.probability import FreqDist 
from nltk.corpus import stopwords 


class NaiveSummarizer: 


    def summarize(self, input, num_sentences): 

     punt_list=['.',',','!','?'] 
     summ_sentences = [] 

     sentences = sent_tokenize(input) 
     lowercase_sentences =[sentence.lower() 
      for sentence in sentences] 
     #print lowercase_sentences 

     s=list(input) 
     ts=''.join([ o for o in s if not o in punt_list ]).split() 
     lowercase_words=[word.lower() for word in ts] 
     words = [word for word in lowercase_words if word not in stopwords.words()] 
     word_frequencies = FreqDist(words) 

     most_frequent_words = [pair[0] for pair in 
      word_frequencies.items()[:100]] 


       # add sentences with the most frequent words 
     for word in most_frequent_words: 
      for i in range(0, len(lowercase_sentences)): 
           if len(summ_sentences) < num_sentences: 
             if (lowercase_sentences[i] not in summ_sentences and word in lowercase_sentences[i]): 
               summ_sentences.append(sentences[i]) 
               break 


     # reorder the selected sentences 
     summ_sentences.sort(lambda s1, s2: input.find(s1) - input.find(s2)) 
     return " ".join(summ_sentences) 


if __name__ == "__main__": 

    naivesum = NaiveSummarizer() 
    text=''' 
    To see a world in a grain of sand, 
    And a heaven in a wild flower, 
    Hold infinity in the palm of your hand, 
    And eternity in an hour. 

    A robin redbreast in a cage 
    Puts all heaven in a rage. 

    A dove-house fill'd with doves and pigeons 
    Shudders hell thro' all its regions. 
    ''' 
text2 = ''' 
     You conclude with the aphorism of Hippocrates, "Qui gravi morbo correpti dolores non sentiunt, us mens aegro­tat" (Those who do not perceive that they are wasted by seri­ous illness are sick in mind), and suggest that I am in need of medicine not only to conquer my malady, but even more, to sharpen my senses for the condition of my inner self. I would fain give you an answer such as you deserve, fain reveal myself to you entirely, but I do not know how to set about it. Hardly do I know whether I am still the same person to whom your precious letter is addressed. 
''' 
print(naivesum.summarize(text2,3)) 
print(naivesum.summarize(text,2)) 
+1

你爲什麼不從你的[其他問題](http://stackoverflow.com/q/23262238/1639625)添加代碼以及哪些不適用?不要指望我們爲您編寫整個GUI。 –

+0

我不是故意的。 我只要求提供相同問題的示例代碼。 ,我可以從應用於我的案例 的代碼中學習使用該類,並使用tkinter 或我的學習網站地址的類的價值,非常感謝 – user3567432

+0

但在另一個問題中,您已經添加了回調到一個按鈕。爲什麼不能使用'callback'函數調用'summarize'函數並將結果放入文本字​​段? –

回答

1

您不能直接使用summarize函數作爲按鈕的回調函數;相反,您應該將其包裝到另一個調用summarize的函數中,然後將結果顯示在Entry小部件中。

首先,你必須添加一個text variable給你小部件,以便您可以讀取和寫入文本,就像這樣:

self.outputvar = StringVar() 
Entry(self, textvariable=self.outputvar) 

現在你可以在your other question添加callback功能,您的按鈕一樣,做這個:

def callback(self): 
    text = "lorem ipsum" # get actual input text, maybe from another widget 
    num = 3    # get proper value for whatever this is for 
    result = self.summarize(text, num) # call summarize 
    self.outputvar.set(result)   # show results in your widget 

或者,你可以使用一個Text小部件;這裏,inserting text處理方式不同。

+0

謝謝@tobias_k – user3567432

相關問題