NLP 中的自定義語料庫
簡介
語料庫被稱為機器可讀的文字文件集合。語料庫具有特定的結構。可以在語料庫上執行各種 NLP 操作。語料庫閱讀器是可以讀取這些文字檔案的實用程式。自定義語料庫是使用 NLTK 資料包生成的。建立自定義語料庫遵循特殊的約定。Corpora 是 corpus 的複數形式。
在本文中,讓我們簡要了解一下語料庫以及如何建立自定義語料庫。
自定義語料庫
語料庫可以採用以下任何格式。
來自以電子方式存在的原始文字。
來自轉錄成文字的語音/語音資料
來自可以從文件中提取文字的光學字元識別 (OCR) 工具。
WordNet、TreeBank 等是一些流行的 NLP 語料庫。
建立自定義語料庫
我們需要 NLTK 資料庫來建立自定義語料庫。我們將為資料定義一個自定義路徑,即 nltk_data。nltk.data.path 包含 nltk 識別的某些路徑。對於構建自定義語料庫,我們的路徑應存在於此路徑列表中。
'/root/nltk_data', '/usr/nltk_data', '/usr/share/nltk_data', '/usr/lib/nltk_data', '/usr/share/nltk_data', '/usr/local/share/nltk_data', '/usr/lib/nltk_data', '/usr/local/lib/nltk_data'
接下來,我們在 ~/nltk_data/corpus 資料夾中建立一個名為 custom_corpus.txt 的檔案,然後使用 NLKT 載入。NLTK 中有兩種型別的載入程式格式 - raw 和 yaml。
## custom corpus NLP !mkdir ~/nltk_data/corpus !touch ~/nltk_data/corpus/custom_corpus.txt !echo hello > ~/nltk_data/corpus/custom_corpus.txt !touch ~/nltk_data/corpus/custom_corpus.yaml !echo data:134 > ~/nltk_data/corpus/custom_corpus.yaml import os, os.path import nltk.data data_path = os.path.expanduser('~/nltk_data') # check whether path exists , otherwise create if not os.path.exists(data_path): os.mkdir(data_path) print ("Is the required path present : ", os.path.exists(data_path)) print ("Path exists within NLTK : ", data_path in nltk.data.path) import nltk.data ## load text raw file nltk.data.load("corpus/custom_corpus.txt", format = "raw") ## load yaml file nltk.data.load("corpus/custom_corpus.yaml", format = "yaml")
輸出
Is the required path present : True Path exists within NLTK : True {'data:134': None}
結論
NLTK 提供了建立自定義語料庫以用於實際應用的工具。NLTK 載入器可以讀取自定義語料庫,並且可以輕鬆應用內建函式。自定義語料庫是使用定義的結構和約定建立的,遵循一組規則。有許多語料庫閱讀器可用於讀取自定義語料庫。
廣告