如何使用 Python 中的功能和庫(kù)創(chuàng)建 n-gram的過(guò)程
在計(jì)算語(yǔ)言學(xué)中,n-gram 對(duì)于語(yǔ)言處理、上下文和語(yǔ)義分析非常重要。它們是從令牌字符串中相鄰的連續(xù)單詞序列。
常見(jiàn)的 n-gram 包括 unigram、bigram 和 trigram,它們是有效的,當(dāng) n>3 時(shí)可能會(huì)遇到數(shù)據(jù)稀疏的問(wèn)題。
本文將討論如何使用 Python 中的功能和庫(kù)創(chuàng)建 n-gram。
使用 for 循環(huán)在 Python 中從文本創(chuàng)建 n-gram
我們可以有效地創(chuàng)建一個(gè) ngrams 函數(shù),該函數(shù)接受文本和 n 值,并返回一個(gè)包含 n-gram 的列表。
為了創(chuàng)建這個(gè)函數(shù),我們可以分割文本并創(chuàng)建一個(gè)空列表(output)來(lái)存儲(chǔ) n-gram。我們使用 for 循環(huán)遍歷 splitInput 列表以遍歷所有元素。
然后將單詞(令牌)添加到 output 列表中。
def ngrams(input, num): splitInput = input.split(' ') output = [] for i in range(len(splitInput) - num + 1): output.append(splitInput[i:i + num]) return output text = "Welcome to the abode, and more importantly, our in-house exceptional cooking service which is close to the Burj Khalifa" print(ngrams(text, 3))
代碼輸出:
[['Welcome', 'to', 'the'], ['to', 'the', 'abode,'], ['the', 'abode,', 'and'], ['abode,', 'and', 'more'], ['and', 'more', 'importantly,'], ['more', 'importantly,', 'our'], ['importantly,', 'our', 'in-house'], ['our', 'in-house', 'exceptional'], ['in-house', 'exceptional', 'cooking'], ['exceptional', 'cooking', 'service'], ['cooking', 'service', 'which'], ['service', 'which', 'is'], ['which', 'is', 'close'], ['is', 'close', 'to'], ['close', 'to', 'the'], ['to', 'the', 'Burj'], ['the', 'Burj', 'Khalifa']]
使用 NLTK 在 Python 中創(chuàng)建 n-gram
NLTK 是一個(gè)自然語(yǔ)言工具包,提供了一個(gè)易于使用的接口,用于文本處理和分詞等重要資源。要安裝 nltk,我們可以使用以下 pip 命令。
pip install nltk
為了展示潛在問(wèn)題,讓我們使用 word_tokenize()
方法。它可以幫助我們使用 NLTK 推薦的單詞分詞器創(chuàng)建一個(gè)令牌化的文本副本,然后再編寫更詳細(xì)的代碼。
import nltk text = "well the money has finally come" tokens = nltk.word_tokenize(text)
代碼輸出:
Traceback (most recent call last):
File "c:\Users\akinl\Documents\Python\SFTP\n-gram-two.py", line 4, in <module>
tokens = nltk.word_tokenize(text)
File "C:\Python310\lib\site-packages\nltk\tokenize\__init__.py", line 129, in word_tokenize
sentences = [text] if preserve_line else sent_tokenize(text, language)
File "C:\Python310\lib\site-packages\nltk\tokenize\__init__.py", line 106, in sent_tokenize
tokenizer = load(f"tokenizers/punkt/{language}.pickle")
File "C:\Python310\lib\site-packages\nltk\data.py", line 750, in load
opened_resource = _open(resource_url)
File "C:\Python310\lib\site-packages\nltk\data.py", line 876, in _open
return find(path_, path + [""]).open()
File "C:\Python310\lib\site-packages\nltk\data.py", line 583, in find
raise LookupError(resource_not_found)
LookupError:
**********************************************************************
Resource [93mpunkt[0m not found.
Please use the NLTK Downloader to obtain the resource:[31m>>> import nltk
>>> nltk.download('punkt')
[0m
For more information see: https://www.nltk.org/data.htmlAttempted to load [93mtokenizers/punkt/english.pickle[0m
Searched in:
- 'C:\\Users\\akinl/nltk_data'
- 'C:\\Python310\\nltk_data'
- 'C:\\Python310\\share\\nltk_data'
- 'C:\\Python310\\lib\\nltk_data'
- 'C:\\Users\\akinl\\AppData\\Roaming\\nltk_data'
- 'C:\\nltk_data'
- 'D:\\nltk_data'
- 'E:\\nltk_data'
- ''
**********************************************************************
上述錯(cuò)誤消息和問(wèn)題的原因是 NLTK 庫(kù)對(duì)于某些方法需要某些數(shù)據(jù),而我們尚未下載這些數(shù)據(jù),特別是如果這是您首次使用的話。因此,我們需要使用 NLTK 下載器來(lái)下載兩個(gè)數(shù)據(jù)模塊,punkt 和 averaged_perceptron_tagger。
當(dāng)我們使用 words()
等方法時(shí),可以使用這些數(shù)據(jù),例如創(chuàng)建一個(gè) Python 文件并運(yùn)行以下代碼以解決該問(wèn)題。
import nltk nltk.download('punkt') nltk.download('averaged_perceptron_tagger')
或者通過(guò)命令行界面運(yùn)行以下命令:
python -m nltk.downloader punkt python -m nltk.downloader averaged_perceptron_tagger
示例代碼:
import nltk text = "well the money has finally come" tokens = nltk.word_tokenize(text) textBigGrams = nltk.bigrams(tokens) textTriGrams = nltk.trigrams(tokens) print(list(textBigGrams), list(textTriGrams))
代碼輸出:
[('well', 'the'), ('the', 'money'), ('money', 'has'), ('has', 'finally'), ('finally', 'come')] [('well', 'the', 'money'), ('the', 'money', 'has'), ('money', 'has', 'finally'), ('has', 'finally', 'come')]
示例代碼:
import nltk text = "well the money has finally come" tokens = nltk.word_tokenize(text) textBigGrams = nltk.bigrams(tokens) textTriGrams = nltk.trigrams(tokens) print("The Bigrams of the Text are") print(*map(' '.join, textBigGrams), sep=', ') print("The Trigrams of the Text are") print(*map(' '.join, textTriGrams), sep=', ')
代碼輸出:
The Bigrams of the Text are
well the, the money, money has, has finally, finally comeThe Trigrams of the Text are
well the money, the money has, money has finally, has finally come
到此這篇關(guān)于在 Python 中從文本創(chuàng)建 N-Grams的文章就介紹到這了,更多相關(guān)Python 文本創(chuàng)建 N-Grams內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
深度學(xué)習(xí)Tensorflow2.8實(shí)現(xiàn)GRU文本生成任務(wù)詳解
這篇文章主要為大家介紹了深度學(xué)習(xí)Tensorflow?2.8?實(shí)現(xiàn)?GRU?文本生成任務(wù)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01django從后臺(tái)返回html代碼的實(shí)例
這篇文章主要介紹了django從后臺(tái)返回html代碼的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03python基于socket函數(shù)實(shí)現(xiàn)端口掃描
這篇文章主要為大家詳細(xì)介紹了python基于socket函數(shù)實(shí)現(xiàn)端口掃描,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05django ajax發(fā)送post請(qǐng)求的兩種方法
這篇文章主要介紹了django ajax發(fā)送post請(qǐng)求的兩種方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01pytorch: tensor類型的構(gòu)建與相互轉(zhuǎn)換實(shí)例
今天小編就為大家分享一篇pytorch: tensor類型的構(gòu)建與相互轉(zhuǎn)換實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07python實(shí)現(xiàn)添加圖片到word文檔中
這篇文章主要介紹了python實(shí)現(xiàn)添加圖片到word文檔中方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09酷! 程序員用Python帶你玩轉(zhuǎn)沖頂大會(huì)
程序員用Python玩轉(zhuǎn)王思聰?shù)摹稕_頂大會(huì)》,感興趣的小伙伴們可以參考一下2018-01-01