欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

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

本文將討論如何使用 Python 中的功能和庫創(chuàng)建 n-gram。

使用 for 循環(huán)在 Python 中從文本創(chuàng)建 n-gram

我們可以有效地創(chuàng)建一個 ngrams 函數(shù),該函數(shù)接受文本和 n 值,并返回一個包含 n-gram 的列表。

為了創(chuàng)建這個函數(shù),我們可以分割文本并創(chuàng)建一個空列表(output)來存儲 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 是一個自然語言工具包,提供了一個易于使用的接口,用于文本處理和分詞等重要資源。要安裝 nltk,我們可以使用以下 pip 命令。

pip install nltk

為了展示潛在問題,讓我們使用 word_tokenize() 方法。它可以幫助我們使用 NLTK 推薦的單詞分詞器創(chuàng)建一個令牌化的文本副本,然后再編寫更詳細的代碼。

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'
    - ''
**********************************************************************

上述錯誤消息和問題的原因是 NLTK 庫對于某些方法需要某些數(shù)據,而我們尚未下載這些數(shù)據,特別是如果這是您首次使用的話。因此,我們需要使用 NLTK 下載器來下載兩個數(shù)據模塊,punkt 和 averaged_perceptron_tagger。

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

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

或者通過命令行界面運行以下命令:

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

到此這篇關于在 Python 中從文本創(chuàng)建 N-Grams的文章就介紹到這了,更多相關Python文本創(chuàng)建 N-Grams內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • python工具dtreeviz決策樹可視化和模型可解釋性

    python工具dtreeviz決策樹可視化和模型可解釋性

    這篇文章主要介紹了python工具dtreeviz決策樹可視化和模型可解釋性,決策樹是梯度提升機和隨機森林的基本構建塊,在學習這些模型的工作原理和模型可解釋性時,可視化決策樹是一個非常有幫助,下文相關資料,需要的小伙伴可任意參考一下
    2022-03-03
  • Python編程基礎之類和對象

    Python編程基礎之類和對象

    這篇文章主要為大家詳細介紹了Python的類和對象,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-01-01
  • Python中的hypot()方法使用簡介

    Python中的hypot()方法使用簡介

    這篇文章主要介紹了Python中的hypot()方法使用簡介,是Python入門所需掌握的基礎知識,需要的朋友可以參考下
    2015-05-05
  • Django REST framework 限流功能的使用

    Django REST framework 限流功能的使用

    DRF常用功能的案例基本用法都有講解,關于限流(Throttling)這個功能其實在真實的業(yè)務場景中能真正用到的其實不算多。今天說這個話題其實一方面是討論功能,另一方面也是希望換個角度去審視我們的開發(fā)過程,希望大家可以在使用DRF功能的同時,也了解一下功能背后的實現(xiàn)
    2021-06-06
  • Python 實現(xiàn)Numpy中找出array中最大值所對應的行和列

    Python 實現(xiàn)Numpy中找出array中最大值所對應的行和列

    今天小編就為大家分享一篇Python 實現(xiàn)Numpy中找出array中最大值所對應的行和列,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • Python異常處理操作實例詳解

    Python異常處理操作實例詳解

    這篇文章主要介紹了Python異常處理操作,結合實例形式分析了Python常見的異常處理類型、相關操作技巧與注意事項,需要的朋友可以參考下
    2018-08-08
  • Python生成可執(zhí)行文件之PyInstaller庫的使用方式

    Python生成可執(zhí)行文件之PyInstaller庫的使用方式

    PyInstaller是一個十分有用的第三方庫,通過對源文件打包,Python程序可以在沒有安裝Python的環(huán)境中運行,也可以作為一個獨立文件方便傳遞和管理,下面這篇文章主要給大家介紹了關于Python生成可執(zhí)行文件之PyInstaller庫的使用方式,需要的朋友可以參考下
    2022-04-04
  • 淺析Python基礎-流程控制

    淺析Python基礎-流程控制

    Python編程語言的作用非常強大,而且其應用方便的特點也對開發(fā)人員起到了非常大的作用。在這里我們就可以先從Python流程控制關鍵字的相關概念開始了解,從而初步掌握這一語言的特點
    2016-03-03
  • python中把元組轉換為namedtuple方法

    python中把元組轉換為namedtuple方法

    在本篇文章里小編給大家整理的是一篇關于python中把元組轉換為namedtuple方法,有興趣的朋友們可以參考下。
    2020-12-12
  • python中使用pymssql庫操作MSSQL數(shù)據庫

    python中使用pymssql庫操作MSSQL數(shù)據庫

    這篇文章主要給大家介紹了關于python中使用pymssql庫操作MSSQL數(shù)據庫的相關資料,最近在學習python,發(fā)現(xiàn)好像沒有對pymssql的詳細說明,于是乎把官方文檔學習一遍,重要部分做個歸檔,方便以后查閱,需要的朋友可以參考下
    2023-08-08

最新評論