pos機專業(yè)詞,「火爐煉AI」機器學(xué)習(xí)036

 新聞資訊  |   2023-05-17 11:20  |  投稿人:pos機之家

網(wǎng)上有很多關(guān)于pos機專業(yè)詞,「火爐煉AI」機器學(xué)習(xí)036的知識,也有很多人為大家解答關(guān)于pos機專業(yè)詞的問題,今天pos機之家(m.afbey.com)為大家整理了關(guān)于這方面的知識,讓我們一起來看下吧!

本文目錄一覽:

1、pos機專業(yè)詞

pos機專業(yè)詞

【火爐煉AI】機器學(xué)習(xí)036-NLP詞形還原

【火爐煉AI】機器學(xué)習(xí)036-NLP詞形還原

(本文所使用的Python庫和版本號: Python 3.6, Numpy 1.14, scikit-learn 0.19, matplotlib 2.2, NLTK 3.3)

詞形還原也是將單詞轉(zhuǎn)換為原來的相貌,和上一篇文章中介紹的詞干提取不一樣,詞形還原要難的多,它是一個更加結(jié)構(gòu)化的方法,在上一篇中的詞干提取例子中,可以看到將wolves提取為wolv等,這些肯定不是我們所期望的。那么此處我們使用NLP詞形還原的方式來將英語單詞還原。

1. NLP詞形還原

詞形還原是基于字典的映射,而且,NLTK還要求手動注明詞形,否則可能會還原不準(zhǔn)確,所以在做自然語言處理的過程中,先對文本進(jìn)行分詞,然后標(biāo)注詞性,最后再進(jìn)行詞形還原。

# 待還原的單詞words = [\'table\', \'probably\', \'wolves\', \'playing\', \'is\', \'dog\', \'the\', \'beaches\', \'grounded\', \'dreamt\', \'envision\']# 由于詞形還原需要先標(biāo)注詞性,故而此處我們用名詞和動詞兩種詞性進(jìn)行測試lemmatizers = [\'NOUN LEMMATIZER\', \'VERB LEMMATIZER\'] # 兩種詞性lemmatizer = wordNetLemmatizer()formatted_row = \'{:>24}\' * (len(lemmatizers) + 1) # 使其打印格式一致print(formatted_row.format(\'WORD\',*lemmatizers)) # 打印表頭for word in words: # # 每個單詞逐一變換 lemmatized=[lemmatizer.lemmatize(word, pos=\'n\'), lemmatizer.lemmatize(word, pos=\'v\')] # 注意里面pos表示詞性,分別表示名稱和動詞 print(formatted_row.format(word,*lemmatized)) # 對提取后的stem進(jìn)行拆包復(fù)制代碼

---------------------------輸---------出--------------------------------

WORD NOUN LEMMATIZER VERB LEMMATIZER table table table probably probably probably wolves wolf wolves playing playing play is is be dog dog dog the the the beaches beach beach grounded grounded ground dreamt dreamt dream envision envision envision復(fù)制代碼

------------------------------完-------------------------------------

可以看出,雖然WordNetLemmatizer很有用,但是需要事先判斷詞性,并且把詞性作為一個參數(shù)傳入到這個函數(shù)中,那么有沒有可能自動判斷詞性,不需要我們認(rèn)為判斷了?這是肯定的,NLTK有一個函數(shù)pos_tag可以自動判斷出某個單詞的詞性,所以基于此,我們可以編寫一個函數(shù),來自動處理一整個句子,輸出器詞性還原之后的形式。如下代碼:

# 定義一個函數(shù)來對一個句子中的所有單詞進(jìn)行詞形還原def lemmatize_all(sentence): wnl = WordNetLemmatizer() for word, tag in pos_tag(word_tokenize(sentence)): if tag.startswith(\'NN\'): yield wnl.lemmatize(word, pos=\'n\') elif tag.startswith(\'VB\'): yield wnl.lemmatize(word, pos=\'v\') elif tag.startswith(\'JJ\'): yield wnl.lemmatize(word, pos=\'a\') elif tag.startswith(\'R\'): yield wnl.lemmatize(word, pos=\'r\') else: yield word復(fù)制代碼text =\'dog runs, cats drunk wines, chicken eat apples, foxes jumped two meters\'print(\'/\'.join(lemmatize_all(text)))復(fù)制代碼

---------------------------輸---------出--------------------------------

dog/run/,/cat/drink/wine/,/chicken/eat/apple/,/fox/jump/two/meter

---------------------------------完-------------------------------------

可以看出,句子中的名詞復(fù)數(shù)都轉(zhuǎn)變?yōu)榱藛螖?shù),動詞過去式都轉(zhuǎn)變?yōu)榱爽F(xiàn)在式等。

關(guān)于pos_tag()函數(shù),返回的是單詞和詞性標(biāo)簽,這些詞性標(biāo)簽有:

# NLTK 詞性標(biāo)簽: CC 連詞 and, or,but, if, while,althoughCD 數(shù)詞 twenty-four, fourth, 1991,14:24DT 限定詞 the, a, some, most,every, noEX 存在量詞 there, there\'sFW 外來詞 dolce, ersatz, esprit, quo,maitreIN 介詞連詞 on, of,at, with,by,into, underJJ 形容詞 new,good, high, special, big, localJJR 比較級詞語 bleaker braver breezier briefer brighter briskerJJS 最高級詞語 calmest cheapest choicest classiest cleanest clearestLS 標(biāo)記 A A. B B. C C. D E F First G H I J KMD 情態(tài)動詞 can cannot could couldn\'tNN 名詞 year,home, costs, time, educationNNS 名詞復(fù)數(shù) undergraduates scotchesNNP 專有名詞 Alison,Africa,April,WashingtonNNPS 專有名詞復(fù)數(shù) Americans Americas Amharas AmityvillesPDT 前限定詞 all both half manyPOS 所有格標(biāo)記 \' \'sPRP 人稱代詞 hers herself him himself hisselfPRP$ 所有格 her his mine my our oursRB 副詞 occasionally unabatingly maddeninglyRBR 副詞比較級 further gloomier granderRBS 副詞最高級 best biggest bluntest earliestRP 虛詞 aboard about across along apartSYM 符號 % & \' \'\' \'\'. ) )TO 詞to toUH 感嘆詞 Goodbye Goody Gosh WowVB 動詞 ask assemble assessVBD 動詞過去式 dipped pleaded swipedVBG 動詞現(xiàn)在分詞 telegraphing stirring focusingVBN 動詞過去分詞 multihulled dilapidated aerosolizedVBP 動詞現(xiàn)在式非第三人稱時態(tài) predominate wrap resort sueVBZ 動詞現(xiàn)在式第三人稱時態(tài) bases reconstructs marksWDT Wh限定詞 who,which,when,what,where,howWP WH代詞 that what whateverWP$ WH代詞所有格 whoseWRB WH副詞復(fù)制代碼

########################小**********結(jié)###############################

1,NLP詞形還原可以使用WordNetLemmatizer函數(shù),但是這個函數(shù)在使用前需要先判斷某個單詞的詞性,然后將詞性作為參數(shù)輸入到這個函數(shù)中進(jìn)行轉(zhuǎn)換。

2,為了簡化這種人為判斷的過程,NLTK有自帶的詞性判斷函數(shù)pos_tag,這個函數(shù)可以自動輸出某個單詞的詞性,所以將pos_tag和WordNetLemmatizer函數(shù)聯(lián)合起來,可以自動對某一整段文本進(jìn)行分詞,詞形還原等操作。

#################################################################

注:本部分代碼已經(jīng)全部上傳到(我的github)上,歡迎下載。

參考資料:

1, Python機器學(xué)習(xí)經(jīng)典實例,Prateek Joshi著,陶俊杰,陳小莉譯

以上就是關(guān)于pos機專業(yè)詞,「火爐煉AI」機器學(xué)習(xí)036的知識,后面我們會繼續(xù)為大家整理關(guān)于pos機專業(yè)詞的知識,希望能夠幫助到大家!

轉(zhuǎn)發(fā)請帶上網(wǎng)址:http://m.afbey.com/news/45978.html

你可能會喜歡:

版權(quán)聲明:本文內(nèi)容由互聯(lián)網(wǎng)用戶自發(fā)貢獻(xiàn),該文觀點僅代表作者本人。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如發(fā)現(xiàn)本站有涉嫌抄襲侵權(quán)/違法違規(guī)的內(nèi)容, 請發(fā)送郵件至 babsan@163.com 舉報,一經(jīng)查實,本站將立刻刪除。