自然語言處理2.3——詞典資源


詞典或者詞典資源是一個詞和/或者短語及其相關信息的集合,例如:詞性和詞意定義等相關信息。詞典資源隸屬於文本,並且通過在文本的基礎上創建和豐富。例如定義了一個文本my_text,然后通過vocab=sorted(set(my_text))建立my_text的詞匯表,再利用word_Freq=FreqDist(my_text)計數文本中每個詞的頻率。vocab和word_Freq都是簡單的詞匯資源。

【詞項】包括詞目(詞條)及其他附加信息。例如:詞性和詞意

1、詞匯列表語料庫

1.1NLTK中包括一些僅僅包含詞匯列表的語料庫。我們可以使用它來檢查文本預料中不常見的或者拼寫錯誤的詞匯。

例子:過濾文本:本程序能計算文本的詞匯表,然后刪除所有在現在的詞匯列表中出現的元素,值留下罕見的或者拼寫錯誤的詞匯。

def unusual_words(text):
	text_vocab=set(w.lower() for w in text in w.isalpha())
	english_vocab=set(w.lower() for w in nltk.corpus.words.words())  
	unusual=text_vocab.difference(english_vocab)
	return sorted(unusual)

>>>unusual_words(nltk.corpus.gutenberg.words('austen-sense.txt'))

 輸出結果:['abbeyland', 'abhorred', 'abilities', 'abounded', 'abridgement', 'abused', 'abuses', 'accents', 'accepting', 'accommodations', 'accompanied', 'accounted', 'accounts', ..... 大約有1600個。

1.2還有一個停用詞語料庫,所謂停用詞就是指高頻詞匯,如the,a,and等等。有時候在進一步處理之前需要將他們過濾出去。

>>>from nltk.corpus import stopwords
>>>stopwords=stopwords.words('english')

['i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', 'your', 'yours', 'yourself', 'yourselves', 'he', 'him', 'his', 'himself', 'she', 'her', 'hers', 'herself', 'it', 'its', 'itself', 'they', 'them', 'their', 'theirs', 'themselves', 'what', 'which', 'who', 'whom', 'this', 'that', 'these', 'those', 'am', 'is', 'are', 'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'having', 'do', 'does', 'did', 'doing', 'a', 'an', 'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until', 'while', 'of', 'at', 'by', 'for', 'with', 'about', 'against', 'between', 'into', 'through', 'during', 'before', 'after', 'above', 'below', 'to', 'from', 'up', 'down', 'in', 'out', 'on', 'off', 'over', 'under', 'again', 'further', 'then', 'once', 'here', 'there', 'when', 'where', 'why', 'how', 'all', 'any', 'both', 'each', 'few', 'more', 'most', 'other', 'some', 'such', 'no', 'nor', 'not', 'only', 'own', 'same', 'so', 'than', 'too', 'very', 's', 't', 'can', 'will', 'just', 'don', 'should', 'now', 'd', 'll', 'm', 'o', 're', 've', 'y', 'ain', 'aren', 'couldn', 'didn', 'doesn', 'hadn', 'hasn', 'haven', 'isn', 'ma', 'mightn', 'mustn', 'needn', 'shan', 'shouldn', 'wasn', 'weren', 'won', 'wouldn']
可以定義一個函數來計算文本中不包含在停用詞列表中的詞所占的比例:

from nltk.corpus import stopwords
def content_fraction(text):
	spwords=stopwords.words('english')
	content=[w for w in text if w.lower() not in spwords]
	return len(content)/len(text)
>>>print(content_fraction(nltk.corpus.reuters.words()))
0.735240435097661

可以看出停用詞占了將近1/3的詞。

詞謎問題:如下圖:

詞匯列表對於解決上面所示的字謎問題很有用。運行程序去遍歷每一個詞,檢查每一個詞是否符合條件。檢查必須出現的字母和長度限制很簡單,但是指定某些字母出現的兩次(V),這樣的檢查很棘手。利用FreqDist比較法可以檢查候選詞中的每個字母出現的頻率關系。

def puzzle(text):
	puzzle_letter=nltk.FreqDist(text)
	obligatory='r'
	wordlist=nltk.corpus.words.words()
	res=[w for w in wordlist if len(w)>=6 and obligatory in w and nltk.FreqDist(w)<=puzzle_letter]
	print(res)

puzzle('egivrvonl')

 結果為:['glover', 'gorlin', 'govern', 'grovel', 'ignore', 'involver', 'lienor', 'linger', 'longer', 'lovering', 'noiler', 'overling', 'region', 'renvoi', 'revolving', 'ringle', 'roving', 'violer', 'virole']
1.3 還有一個詞匯列表時名字語料庫,包括8000個按性別分類的名字。男性和女性的名字存儲在單獨的文件中。

>>>names=nltk.corpus.names
>>>names.fileids()
['female.txt','male.txt']
####尋找男女生都使用的名字:
>>>male_names=names.words('male.txt')
>>>female_names=names.words('female.txt')
>>>[w for w in male_names if w in female_name]
['Abbey', 'Abbie', 'Abby', 'Addie', 'Adrian', 'Adrien', 'Ajay', 'Alex', 'Alexis', 'Alfie', 'Ali', 'Alix', 'Allie', 'Allyn', 
'Andie', 'Andrea', 'Andy', 'Angel', 'Angie', 'Ariel', 'Ashley', 'Aubrey', 'Augustine', ...

 我們來看看名字最后一位在男女性之間有什么差別

>>>from nltk.corpus import names
>>>cfd=nltk.ConditionalFreqDist((fileid,name[-1]) for fileid in names.fileids() for name in names.words(fileid))
>>>cfd.plot

 

可以看出大多數以a,e,i結尾的名字為女性,以k,o,r,s,t結尾的為男性。

2.發音的詞典

2.1NLTK中包括了美國英語的CMU發音詞典,他是為語音合成器而設計的。

>>>entries=nltk.corpus.cumdict.entries()
>>>print(len(entries))
133737
>>>for entry in entries[39943:39948]:
        print(entry)
('explorer', ['IH0', 'K', 'S', 'P', 'L', 'AO1', 'R', 'ER0'])
('explorers', ['IH0', 'K', 'S', 'P', 'L', 'AO1', 'R', 'ER0', 'Z'])
('explores', ['IH0', 'K', 'S', 'P', 'L', 'AO1', 'R', 'Z'])
('exploring', ['IH0', 'K', 'S', 'P', 'L', 'AO1', 'R', 'IH0', 'NG'])
('explosion', ['IH0', 'K', 'S', 'P', 'L', 'OW1', 'ZH', 'AH0', 'N'])

 例子:尋找發音包含三個音素的條目,並且第一個發音為'P',第三個發音為'T',打印滿足條件的詞和該詞的第二個音素

>>>entries=nltk.corpus.cmudict.entries()
>>>for word,pron in entries:
            if len(pron)==3:
		ph1,ph2,ph3=pron
		if ph1=='P' and ph3=='T':
			print(word,ph2)

 結果:pait EY1 pat AE1 pate EY1 patt AE1 peart ER1 peat IY1 peet IY1 peete IY1 pert ER1 pet EH1 pete IY1 pett EH1 piet IY1 piette IY1 pit IH1 pitt IH1
pot AA1 pote OW1 pott AA1 pout AW1 puett UW1 purt ER1 put UH1 putt AH1

音素包含數字表示主重音(1)、次重音(2)和無重音(0)。定義一個函數來提取重音數字,然后尋找具有特定重音模式的詞匯。

>>>entries=nltk.corpus.cmudict.entries()
>>>def stress(pron):
    return [char for phone in pron for char in phone if char.isdigit()]
####尋找音素為01020的詞匯
>>>res=[w for w,pron in entries if stress(pron)==['0','1','0','2','0']]
>>>print(res)
['abbreviated', 'abbreviated', 'abbreviating', 'accelerated', 'accelerating', 'accelerator', 'accelerators', 'accentuated', 'accentuating', 'accommodated', 'accommodating', 'accommodative', 'accumulated', 'accumulating', 'accumulative', 'accumulator', 'accumulators'...

 3.比較詞表

NLTK中包含了所謂的斯瓦迪士核心詞列表,包括幾種語言的約200個常見詞的列表。

>>>from nltk.corpus import swadesh
>>>swadesh.fileids()
['be', 'bg', 'bs', 'ca', 'cs', 'cu', 'de', 'en', 'es', 'fr', 'hr', 'it', 'la', 'mk', 'nl', 'pl', 'pt', 'ro', 'ru', 'sk', 'sl', 'sr', 'sw', 'uk']

可以使用entries()方法來制定一個語言鏈表來訪問多語言的同源詞。而且,可以把它轉換成一個簡單的詞典、

>>>fr2en=swadesh.entries(['fr','en'])  ###法語和英語
>>>translate=dict(fr2en)
>>>translate['chien']  ###進行翻譯
'dog'
>>>translate['jeter']
'throw'

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM