訓練分詞器並在句子中過濾停用詞
介紹
在 NLP 中,將文字分割成句子是一個非常關鍵的預處理任務。分詞是將文字語料庫分解成單個句子的過程。在 NLTK 中,預設的分詞器可以很好地完成文字分詞任務,但是當文字包含非標準的標點符號、符號等時,它就會失效。在這種情況下,我們需要訓練一個分詞器。
在本文中,讓我們探討分詞器的訓練,並瞭解過濾詞或停用詞的使用。
NLP 中的句子分詞
NLTK 中的預設分詞器可用於以下給出的文字示例。
Ram - 上週日去哪裡了?
Mohan - 我去看了泰姬陵。
Ram - 泰姬陵在哪裡?
Mohan - 它位於阿格拉。它被認為是世界七大奇蹟之一。
import nltk nltk.download('punkt') from nltk.tokenize import sent_tokenize textual_data = """ Ram : Where have gone last Sunday? Mohan : I went to see the Taj Mahal. Ram: Where is the Taj Mahal located Mohan: It is located in Agra.It is considered to be one of the wornders of the world. """ sentences = sent_tokenize(textual_data) print(sentences[0]) print("\n",sentences)
輸出
Ram : Where have gone last Sunday? ['\nRam : Where have gone last Sunday?', 'Mohan : I went to see the Taj Mahal.', 'Ram: Where is the Taj Mahal located \n\n\nMohan: It is located in Agra.It is considered to be one of the wonders of the world.']
最後一句的輸出看起來不正確,因為分詞器未能對文字進行分詞,因為文字沒有遵循正常的段落結構。
這是一種可以訓練分詞器的情況。
data.txt 連結:https://drive.google.com/file/d/1bs2eBbSxTSeaAuDlpoDqGB89Ej9HAqPz/view?usp=sharing。
訓練分詞器
在本例中,我們將使用 Punkt 句子分詞器。
import nltk nltk.download('webtext') from nltk.tokenize import PunktSentenceTokenizer from nltk.corpus import webtext data = webtext.raw('/content/data.txt') tokenizer_sentence = PunktSentenceTokenizer(data) sentences = tokenizer_sentence.tokenize(data) print(sentences[0]) print("\n",sentences)
輸出
Ram : Where have gone last Sunday? ['Ram : Where have gone last Sunday?', 'Mohan : I went to see the Taj Mahal.', 'Ram: Where is the Taj Mahal located?', 'Mohan: It is located in Agra.It is considered to be one of the wonders of the world.']
在句子中過濾停用詞
在文字語料庫中,那些不為特定句子增添意義的詞稱為停用詞。在預處理過程中,它們通常會被從原始文字中刪除,因為它們對 NLP 任務並不重要。NLTK 庫是對應於不同語言的停用詞集合。
讓我們透過程式碼示例瞭解過濾停用詞的過程。
示例句子:“每一代都會誕生一位新的演員,並受到許多粉絲的崇拜”
import nltk nltk.download('stopwords') from nltk.corpus import stopwords as sw from nltk.tokenize import word_tokenize sentence = "A new actor is born every generation and is worshipped by many fans" " stopwords_en = set(sw.words('english')) word_list = word_tokenize(sentence) filtered_words = [w for w in word_list if w not in stopwords_en] print ("Words present in the sentence initially : ",word_list) print ("\nWords after stopword removal process : ",filtered_words)
輸出
Words presents in the sentence initially : ['A', 'new', 'actor', 'is', 'born', 'every', 'generation', 'and', 'is', 'worshipped', 'by', 'many', 'fans'] Words after stopword removal process : ['A', 'new', 'actor', 'born', 'every', 'generation', 'worshipped', 'many', 'fans']
不同型別的分詞
TFIDF 分詞
TF-IDF 代表詞頻-逆文件頻率。它是一種演算法,利用詞語出現的頻率來確定和權衡詞語在特定文件中的重要性。它有兩個術語 TF(詞頻)和 IDF(逆文件頻率)。
TF 表示詞語在特定文件中出現的頻率,計算公式如下:
TF = t 在 d 中的頻率 / d 中的總詞數 = tf(t,d)
IDF 衡量的是在語料庫中的多少個文件 D 中出現了特定術語 t,表示為:
IDF = 語料庫中文件的總數 N / 包含術語 t 的文件數 = idf(t,D)
TF-IDF 是 TF 和 IDF 項的乘積
TF-IDF=tf(t,d) *idf(t,D)
使用 Scikit-learn 庫的 TFIDF 向量化器的示例
from sklearn.feature_extraction.text import TfidfVectorizer vectorizer = TfidfVectorizer() corpus = ["Hello how are you","You have called me","How are you"] data = vectorizer.fit_transform(corpus) tokens = vectorizer.get_feature_names_out() print(tokens) print(data.shape)
輸出
['are' 'called' 'have' 'hello' 'how' 'me' 'you'] (3, 7)
頻率計數
此方法用於計算語料庫中文件或文字中每個詞的頻率。
例如,在給定的文字中
狐狸在叢林裡走著。然後它看到一隻老虎朝它走來。狐狸看到老虎後嚇壞了。”
詞頻可以發現為
coming: 1
it.The: 1
jungle: 1
seeing: 1
terrified: 1
then: 1
tiger: 2
towards: 1
walking: 1
import nltk from nltk.corpus import webtext from nltk.tokenize import word_tokenize from nltk.probability import FreqDist import nltk nltk.download('punkt') text_data = "The fox was walking in the jungle. It then saw a tiger coming towards it.The fox was terrified on seeing the tiger." words = word_tokenize(text_data) print(words) word_freq = nltk.FreqDist(words) words_filtered = dict([(i, j) for i, j in word_freq.items() if len(i) > 3]) for k in sorted(words_filtered): print("%s: %s" % (k, words_filtered[k]))
輸出
['The', 'fox', 'was', 'walking', 'in', 'the', 'jungle', '.', 'It', 'then', 'saw', 'a', 'tiger', 'coming', 'towards', 'it.The', 'fox', 'was', 'terrified', 'on', 'seeing', 'the', 'tiger', '.'] coming: 1 it.The: 1 jungle: 1 seeing: 1 terrified: 1 then: 1 tiger: 2 towards: 1 walking: 1
基於規則的分詞
基於規則的分詞器使用一些預定義規則將文字分解成標記。這些規則可以是正則表示式過濾器或語法約束。
例如,可以使用規則透過空格或逗號分割文字。
此外,一些分詞器適用於推文,它們具有特殊的規則來分割單詞並保留特殊字元,如表情符號等。
下面是一個基於正則表示式規則的分詞器的程式碼示例。
from nltk.tokenize import regexp_tokenize data_text = "Jack and Jill went up the hill." print(regexp_tokenize(data_text, "[\w']+"))
輸出
['Jack', 'and', 'Jill', 'went', 'up', 'the', 'hill']
停用詞過濾器
停用詞是在 NLP 和文字處理的上下文中不會為句子的含義增加任何特殊意義的常用詞,並且通常會被大多數分詞器忽略。
from nltk.corpus import stopwords from nltk.tokenize import word_tokenize import nltk nltk.download('stopwords') text_data = "Hello how are you.You have called me. How are you" data = word_tokenize(text_data) filtered_words = [w for w in data if w not in stopwords.words('english')] print(filtered_words)
輸出
['Hello', '.', 'You', 'called', '.', 'How']
結論
句子分詞和停用詞去除是兩種非常常見且重要的 NLP 文字預處理步驟。對於簡單的語料庫結構,可以使用預設的句子分詞器,但是對於不遵循常規段落結構的文字,甚至可以訓練分詞器。停用詞不會對句子的含義做出貢獻,因此在文字預處理期間會被過濾掉。