最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

如何使用 Python 中的功能和庫創(chuàng)建 n-gram

 更新時(shí)間:2023年09月29日 09:35:25   作者:跡憶客  
在計(jì)算語言學(xué)中,n-gram 對(duì)于語言處理、上下文和語義分析非常重要,它們是從令牌字符串中相鄰的連續(xù)單詞序列,本文將討論如何使用 Python 中的功能和庫創(chuàng)建 n-gram,感興趣的朋友一起看看吧

在計(jì)算語言學(xué)中,n-gram 對(duì)于語言處理、上下文和語義分析非常重要。它們是從令牌字符串中相鄰的連續(xù)單詞序列。

常見的 n-gram 包括 unigram、bigram 和 trigram,它們是有效的,當(dāng) n>3 時(shí)可能會(huì)遇到數(shù)據(jù)稀疏的問題。

本文將討論如何使用 Python 中的功能和庫創(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)來存儲(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è)自然語言工具包,提供了一個(gè)易于使用的接口,用于文本處理和分詞等重要資源。要安裝 nltk,我們可以使用以下 pip 命令。

pip install nltk

為了展示潛在問題,讓我們使用 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.html

  Attempted 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ò)誤消息和問題的原因是 NLTK 庫對(duì)于某些方法需要某些數(shù)據(jù),而我們尚未下載這些數(shù)據(jù),特別是如果這是您首次使用的話。因此,我們需要使用 NLTK 下載器來下載兩個(gè)數(shù)據(jù)模塊,punkt 和 averaged_perceptron_tagger。

當(dāng)我們使用 words() 等方法時(shí),可以使用這些數(shù)據(jù),例如創(chuàng)建一個(gè) Python 文件并運(yùn)行以下代碼以解決該問題。

import nltk
nltk.download('punkt')
nltk.download('averaged_perceptron_tagger')

或者通過命令行界面運(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 come

The 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)文章

  • 從基礎(chǔ)到進(jìn)階詳解Python處理Word文檔的完全指南

    從基礎(chǔ)到進(jìn)階詳解Python處理Word文檔的完全指南

    在Python中處理Word文檔是一項(xiàng)常見且實(shí)用的任務(wù),本文將介紹如何使用幾個(gè)主流的Python庫來創(chuàng)建、修改和處理Word文檔,涵蓋從基礎(chǔ)操作到高級(jí)功能的完整流程
    2026-01-01
  • Python四大金剛之元組詳解

    Python四大金剛之元組詳解

    這篇文章主要介紹了Python的元組,小編覺得這篇文章寫的還不錯(cuò),需要的朋友可以參考下,希望能夠給你帶來幫助
    2021-10-10
  • python利用elaphe制作二維條形碼實(shí)現(xiàn)代碼

    python利用elaphe制作二維條形碼實(shí)現(xiàn)代碼

    條形碼的應(yīng)用將會(huì)越來越廣泛,看到了一篇文章,寫的挺好的!用手機(jī)拍二維碼,查二維碼確實(shí)很爽!這將成為一種潮流
    2012-05-05
  • python中的字符串類型解讀

    python中的字符串類型解讀

    這篇文章主要介紹了python中的字符串類型,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-06-06
  • python神經(jīng)網(wǎng)絡(luò)特征金字塔FPN原理

    python神經(jīng)網(wǎng)絡(luò)特征金字塔FPN原理

    這篇文章主要為大家介紹了python神經(jīng)網(wǎng)絡(luò)特征金字塔FPN原理的解釋,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • python的metaclass關(guān)鍵字詳解

    python的metaclass關(guān)鍵字詳解

    metaclass其實(shí)就是最常用的元類,本文主要介紹了python的metaclass關(guān)鍵字的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2026-02-02
  • Python使用DPKT實(shí)現(xiàn)分析數(shù)據(jù)包

    Python使用DPKT實(shí)現(xiàn)分析數(shù)據(jù)包

    dpkt項(xiàng)目是一個(gè)Python模塊,主要用于對(duì)網(wǎng)絡(luò)數(shù)據(jù)包進(jìn)行解析和操作,z這篇文章主要為大家介紹了python如何利用DPKT實(shí)現(xiàn)分析數(shù)據(jù)包,有需要的可以參考下
    2023-10-10
  • Flask框架實(shí)現(xiàn)的前端RSA加密與后端Python解密功能詳解

    Flask框架實(shí)現(xiàn)的前端RSA加密與后端Python解密功能詳解

    這篇文章主要介紹了Flask框架實(shí)現(xiàn)的前端RSA加密與后端Python解密功能,結(jié)合實(shí)例形式詳細(xì)分析了flask框架前端使用jsencrypt.js加密與后端Python解密相關(guān)操作技巧,需要的朋友可以參考下
    2019-08-08
  • Python結(jié)合FFmpeg實(shí)現(xiàn)將MP4視頻與SRT字幕無損合并

    Python結(jié)合FFmpeg實(shí)現(xiàn)將MP4視頻與SRT字幕無損合并

    在日常視頻處理場(chǎng)景中,我們經(jīng)常需要將字幕(SRT)合并到視頻中,本文將介紹如何使用 Python 調(diào)用 FFmpeg將 MP4 視頻與 SRT 字幕無損合并為一個(gè)新視頻文件,感興趣的小伙伴可以了解下
    2026-01-01
  • python的類class定義及其初始化方式

    python的類class定義及其初始化方式

    這篇文章主要介紹了python的類class定義及其初始化方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-05-05

最新評(píng)論

洛扎县| 乐平市| 九江县| 黔江区| 肃北| 拉萨市| 海宁市| 定南县| 苍山县| 来安县| 大渡口区| 桃园市| 武城县| 九寨沟县| 五原县| 崇义县| 白玉县| 手游| 平邑县| 岳西县| 集贤县| 彭水| 时尚| 三原县| 大理市| 施甸县| 罗平县| 兴业县| 鄱阳县| 石楼县| 资讯 | 文水县| 莱芜市| 大厂| 马山县| 上杭县| 柏乡县| 黎平县| 烟台市| 慈溪市| 泸水县|