2016-09-19 40 views
0

我是NLTK的新手,我正在用NLTK 3 Cookbook進行Python 3文本處理:第4章。我已經完成了「使用WordNet進行標記」,並且在默認語言English中工作正常。我已經下載了Language Bahasa(zsm)以omw並且想要使用其他數據集嘗試使用Bahasa。使用相同的方法,我現在如何將語言默認從英語更改爲zsm?我使用如何將NLTK默認的wordnet語言更改爲zsm?

代碼:

class WordNetTagger(SequentialBackoffTagger): 

    def __init__(self, *args, **kwargs): 
     SequentialBackoffTagger.__init__(self, *args, **kwargs) 

     self.wordnet_tag_map = { 
      'n': 'NN', 
      's': 'JJ', 
      'a': 'JJ', 
      'r': 'RB', 
      'v': 'VB' 
     } 

    def choose_tag(self, tokens, index, history): 
     word = tokens[index] 
     fd = FreqDist() 

     for synset in wordnet.synsets(word): 
      fd[synset.pos()] += 1 

     if not fd: return None 
     return self.wordnet_tag_map.get(fd.max()) 

在此先感謝。

回答

0

正如您似乎已經想通,您不會更改默認語言;只要不需要默認設置,就可以明確指定所需的語言。如果您覺得這很麻煩,您可以將wordnet對象包裝在您自己的自定義類中,該類提供其自己的默認值。

class MyWordNet: 
    def __init__(self, wn): 
     self._wordnet = wn 

    def synsets(self, word, pos=None, lang="zsm"): 
     return self._wordnet.synsets(word, pos=pos, lang=lang) 

    # and similarly for any other methods you need 

然後你初始化一個包裝對象,向其傳遞NLTK的wordnet讀者對象,以後你用這個代替原來的:

wn = MyWordNet(wordnet) 
... 

for synset it wn.synsets(word): 
    ... 
+0

謝謝@alexis的幫助。它工作,我正在尋找,但結果與我的不同。大多數單詞使用您的建議標記爲「無」。就像我的明確指定的那樣,它就像逃避了很多單詞。 Urmm,有趣。任何想法如何發生。 –

+0

那麼......我的答案顯示瞭如何用包裝類重寫默認參數,但不知道如何用wordnet去實現它!哎呀...現在檢查並修復,再看看。 (主要是匹配原始'synsets()'方法的簽名)。 – alexis

+0

再次感謝@alexis的反饋。我試過了,仍然返回相同的結果。但是,我們會將其留給其他人以便稍後使用他們的數據集來測試這兩種方法。至於你現在的建議回答了我的主要問題:-) ps:我正在使用[鏈接](http://www.nltk.org/howto/wordnet.html) –

0

一些嘗試後,我只是想出:

def choose_tag(self, tokens, index, history): 
    word = tokens[index] 
    fd = FreqDist() 

    for synset in wordnet.synsets(word, lang='zsm'): 
     fd[synset.pos()] += 1 

    if not fd: return None 
    return self.wordnet_tag_map.get(fd.max()) 

主要是wordnet.synsets(文字,LANG = 'ZSM')的作品,現在對我來說。我仍然願意接受任何其他建議或更正。謝謝。

相關問題