【NLP】竞赛必备的NLP库
NLP必備的庫
本周我們給大家整理了機器學習和競賽相關的NLP庫,方便大家進行使用,建議收藏本文。
jieba
jieba是Python中的優秀的中文分詞第三方庫,通過幾行代碼就可以完成中文句子的分詞。jieba的分詞精度和性能非常優異,經常用來進行中文分詞的實驗對比。此外jieba還可以很方便的自定義詞典,使用起來非常靈活。
import jiebaseg_list = jieba.cut("我來到北京清華大學", cut_all=True) print("Full Mode: " + "/ ".join(seg_list)) # 全模式 # 【全模式】: 我/ 來到/ 北京/ 清華/ 清華大學/ 華大/ 大學 seg_list = jieba.cut("我來到北京清華大學", cut_all=False) print("Default Mode: " + "/ ".join(seg_list)) # 精確模式 # 【精確模式】: 我/ 來到/ 北京/ 清華大學seg_list = jieba.cut("他來到了網易杭研大廈") # 默認是精確模式 print(", ".join(seg_list)) # 【新詞識別】:他, 來到, 了, 網易, 杭研, 大廈jieba項目主頁:https://github.com/fxsjy/jieba
此外jieba分詞還有CPP版本,如果覺得性能不夠,可以嘗試CPP版本。
spaCy
spaCy是功能強化的NLP庫,可與深度學習框架一起運行。spaCy提供了大多數NLP任務的標準功能(標記化,PoS標記,解析,命名實體識別)。spaCy與現有的深度學習框架接口可以一起使用,并預裝了常見的語言模型。
import spacy# Load English tokenizer, tagger, parser, NER and word vectors nlp = spacy.load("en_core_web_sm")# Process whole documents text = ("When Sebastian Thrun started working on self-driving cars at ""Google in 2007, few people outside of the company took him ""seriously. “I can tell you very senior CEOs of major American ""car companies would shake my hand and turn away because I wasn’t ""worth talking to,” said Thrun, in an interview with Recode earlier ""this week.") doc = nlp(text)# Analyze syntax print("Noun phrases:", [chunk.text for chunk in doc.noun_chunks]) print("Verbs:", [token.lemma_ for token in doc if token.pos_ == "VERB"])# Find named entities, phrases and concepts for entity in doc.ents:print(entity.text, entity.label_)spaCy項目主頁:https://spacy.io/
Gensim
是一個高效的自然語言處理Python庫,主要用于抽取文檔的語義主題(semantic topics)。Gensim的輸入是原始的、無結構的數字文本(純文本),內置的算法包括Word2Vec,FastText和LSA。
from gensim.test.utils import common_texts, get_tmpfile from gensim.models import Word2Vecpath = get_tmpfile("word2vec.model") model = Word2Vec(common_texts, size=100, window=5, min_count=1, workers=4) model.save("word2vec.model")Gensim項目官網:https://radimrehurek.com/gensim/
NLTK
NLTK是一個免費的,開源的,社區驅動的項目,提供了50多種語料庫和詞匯資源(如WordNet),還提供了一套用于分類,標記化,詞干化,標記,解析和語義推理的文本處理庫。
import nltk >>> sentence = """At eight o'clock on Thursday morning ... Arthur didn't feel very good.""" >>> tokens = nltk.word_tokenize(sentence) >>> tokens ['At', 'eight', "o'clock", 'on', 'Thursday', 'morning', 'Arthur', 'did', "n't", 'feel', 'very', 'good', '.'] >>> tagged = nltk.pos_tag(tokens) >>> tagged[0:6] [('At', 'IN'), ('eight', 'CD'), ("o'clock", 'JJ'), ('on', 'IN'), ('Thursday', 'NNP'), ('morning', 'NN')]NLTK官網:http://www.nltk.org/
TextBlob
TextBlob是一個用python編寫的開源的文本處理庫,它可以用來執行很多自然語言處理的任務,比如,詞性標注、名詞性成分提取、情感分析、文本翻譯等。
from textblob import TextBlobtext = ''' The titular threat of The Blob has always struck me as the ultimate movie monster: an insatiably hungry, amoeba-like mass able to penetrate virtually any safeguard, capable of--as a doomed doctor chillingly describes it--"assimilating flesh on contact. Snide comparisons to gelatin be damned, it's a concept with the most devastating of potential consequences, not unlike the grey goo scenario proposed by technological theorists fearful of artificial intelligence run rampant. '''blob = TextBlob(text) blob.tags # [('The', 'DT'), ('titular', 'JJ'),# ('threat', 'NN'), ('of', 'IN'), ...]blob.noun_phrases # WordList(['titular threat', 'blob',# 'ultimate movie monster',# 'amoeba-like mass', ...])for sentence in blob.sentences:print(sentence.sentiment.polarity) # 0.060 # -0.341TextBlob官網:https://textblob.readthedocs.io/en/dev/
CoreNLP
Stanford CoreNLP是用處理自然語言的工具集合。它可以給出詞語的基本形式:詞性(它們是公司名、人名等,規范化日期,時間,和數字),根據短語和語法依賴來標記句子的結構,發現實體之間的關系、情感以及人們所說的話等。
CoreNLP提供了Java版本的服務器部署,也有python版本的調用,用途非常廣泛。在工業界和學術界都有廣泛的應用。
CoreNLP官網:https://stanfordnlp.github.io/CoreNLP/
AllenNLP
AllenNLP 是由世界著名的艾倫人工智能實驗室(Allen Institute for AI Lab)建立的 NLP 深度學習通用框架,不僅包含了最先進的參考模型,可以進行快速部署,而且支持多種任務和數據集。
AllenNLP官網:https://allennlp.org/
TorchText
TorchText是Pytorch下對NLP的支持庫,包含便利的數據處理實用程序,可在批量處理和準備之前將其輸入到深度學習框架中。TorchText可以很方便加載訓練數據、驗證和測試數據集,來進行標記化、vocab構造和創建迭代器,并構建迭代器。
TorchText官網:https://github.com/pytorch/text
Transformers
Transformers是現如今最流行的庫,它實現了從 BERT 和 GPT-2 到 BART 和 Reformer 的各種轉換。huggingface 的代碼可讀性強和文檔也是清晰易讀。在官方github的存儲庫中,甚至通過不同的任務來組織 python 腳本,例如語言建模、文本生成、問題回答、多項選擇等。
huggingface官網:https://huggingface.co/
OpenNMT
OpenNMT 是用于機器翻譯和序列學習任務的便捷而強大的工具。其包含的高度可配置的模型和培訓過程,讓它成為了一個非常簡單的框架。因其開源且簡單的特性,建議大家使用 OpenNMT 進行各種類型的序列學習任務。
OpenNMT官網:https://opennmt.net/
往期精彩回顧適合初學者入門人工智能的路線及資料下載機器學習及深度學習筆記等資料打印機器學習在線手冊深度學習筆記專輯《統計學習方法》的代碼復現專輯 AI基礎下載機器學習的數學基礎專輯獲取一折本站知識星球優惠券,復制鏈接直接打開:https://t.zsxq.com/662nyZF本站qq群704220115。加入微信群請掃碼進群(如果是博士或者準備讀博士請說明):總結
以上是生活随笔為你收集整理的【NLP】竞赛必备的NLP库的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 我所认识的数据产品经理(文末有彩蛋)
- 下一篇: 【深度学习】深入理解LSTM