2014-12-02 56 views
3

我注意到NLTK sent_tokenizer在某些日期時會出錯。有什麼辦法來調整,以便它能夠正確標記化如下:NLTK Sentence Tokenizer錯誤

valid any day after january 1. not valid on federal holidays, including february 14, 
or with other in-house events, specials, or happy hour. 

目前運行sent_tokenize的結果:

['valid any day after january 1. not valid on federal holidays, including february 14, 
or with other in-house events, specials, or happy hour.'] 

但它不應導致:

['valid any day after january 1.', 'not valid on federal holidays, including february 14, 
    or with other in-house events, specials, or happy hour.'] 

爲'1月1日'之後的時期是合法的句子終止字符。

回答

3

首先,sent_tokenize函數使用用於標記格式良好的英語句子的punkt標記器。因此,通過包括正確的大小寫必須解決您的問題:

>>> from nltk import sent_tokenize 
>>> s = 'valid any day after january 1. not valid on federal holidays, including february 14, or with other in-house events, specials, or happy hour.' 
>>> sent_tokenize(s) 
['valid any day after january 1. not valid on federal holidays, including february 14, or with other in-house events, specials, or happy hour.'] 
>>>> 
>>> s2 = 'Valid any day after january 1. Not valid on federal holidays, including february 14, or with other in-house events, specials, or happy hour.' 
>>> sent_tokenize(s2) 
['Valid any day after january 1.', 'Not valid on federal holidays, including february 14, or with other in-house events, specials, or happy hour.'] 

現在,讓我們深入挖掘,該PUNKT分詞器是Kiss and Strunk (2005)的算法,見https://github.com/nltk/nltk/blob/develop/nltk/tokenize/punkt.py的實施。

此標記生成器把一個文本句子的列表,通過使用 監督的算法來構建該啓動句子縮寫詞, 搭配,和詞的典範。其必須在 上訓練目標語言的大量明文,然後才能使用 。

所以在sent_tokenize的情況下,我敢肯定這是一個結構良好的英語語料庫因此事實句號後市值句子邊界的一個強烈的信號列車。並且fullstop本身可能不是,因爲我們有像i.e. , e.g.

而且在某些情況下,語料庫可能有類似01. put pasta in pot \n02. fill the pot with water的東西。在訓練數據中存在這樣的句子/文檔時,算法很可能認爲跟隨未捕獲的單詞的整個句子不是句子邊界。

因此,要解決這個問題,我建議如下:

  1. 手動細分句子的10-20%,並重新訓練標記生成器
  2. 特定語料庫將您的語料庫爲形成良好正字使用前sent_tokenize

參見:training data format for nltk punkt

+0

偉大的答案。我會嘗試將標記器移動到文本轉換爲小寫之前。 – user2694306 2014-12-02 09:16:04

+0

您不應該不必要地小寫您的語料庫。它可能會降低模型訓練的稀疏性並提高IR中的檢索率,但會導致噪聲模型,並且幾乎總是會導致預處理不好,因爲標記化,標記和解析模型是基於格式良好的數據構建的。 – alvas 2014-12-02 09:24:42

+0

例如'睡覺狗是一個很好的遊戲',在這種情況下'睡覺狗'是一個命名實體,如果你在POS標記之前將它降低了,我很肯定POS標記會說它是一個「睡眠(形容詞/形容詞動詞)狗(名詞)「而不是」睡眠(NNP)狗「(NNP)' – alvas 2014-12-02 09:26:31