Docsity

NLP Cheat Sheet, Schemes and Mind Maps of Mathematics

Toggle sidebar
bg1
NLP Cheat Sheet
by sree017 via cheatography.com/126402/cs/24446/
Tokeni๎˜zation
Tokenization breaks the
raw text into words,
sentences called tokens.
These tokens help in
understanding the
context or developing
the model for the NLP.
... If the text is split
into words using some
separation technique it
is called word
tokenization and same
separation done for
sentences is called
sentence tokenization.
# NLTK
import nltk
nltk.d๎˜own๎˜loa๎˜d('๎˜punkt')
paragraph = "๎˜write
paragaraph here to
convert into tokens."
sentences = nltk.s๎˜ent๎˜โ€
_to๎˜ken๎˜ize๎˜(pa๎˜rag๎˜raph)
words = nltk.w๎˜ord๎˜_to๎˜ken๎˜โ€
ize๎˜(pa๎˜rag๎˜raph)
# Spacy
from spacy.l๎˜ang.en
import English
nlp = English()
sbd = nlp.cr๎˜eat๎˜e_p๎˜ipe๎˜โ€
('s๎˜ent๎˜enc๎˜izer')
nlp.ad๎˜d_p๎˜ipe๎˜(sbd)
doc = nlp(pa๎˜rag๎˜raph)
[sent for sent in
doc.sents]
nlp = English()
doc = nlp(pa๎˜rag๎˜raph)
Tokeni๎˜zation (cont)
[word for word in doc]
# Keras
from keras.p๎˜re๎˜pro๎˜ces๎˜sin๎˜โ€
g.text import text_t๎˜o_w๎˜โ€
ord๎˜_se๎˜quence
text_t๎˜o_w๎˜ord๎˜_se๎˜que๎˜nce๎˜โ€
(pa๎˜rag๎˜raph)
# genis
from gensim.su๎˜mma๎˜riz๎˜ati๎˜โ€
on.t๎˜ex๎˜tcl๎˜eaner import
split_๎˜sen๎˜tences
split_๎˜sen๎˜ten๎˜ces๎˜(pa๎˜rag๎˜โ€
raph)
from gensim.utils import
tokenize
list(t๎˜oke๎˜niz๎˜e(p๎˜ara๎˜โ€
graph))
Bag Of Words & TF-IDF
Bag of Words model is
used to preprocess the
text by converting it
into a bag of words,
which keeps a count of
the total occurrences of
most frequently used
words
# counters = List of
stences after pre
processing like tokeni๎˜โ€
zation, stemmi๎˜ng/๎˜lem๎˜mat๎˜โ€
iza๎˜tion, stopwords
from sklear๎˜n.f๎˜eat๎˜ure๎˜_ex๎˜โ€
tra๎˜cti๎˜on.text import
CountV๎˜ect๎˜orizer
cv = CountV๎˜ect๎˜ori๎˜zer๎˜(ma๎˜โ€
x_f๎˜eatures = 1500)
Bag Of Words & TF-IDF (cont)
X = cv.fit๎˜_tr๎˜ans๎˜for๎˜m(c๎˜โ€
oun๎˜ter๎˜s).t๎˜oa๎˜rray()
Term Freque๎˜ncy๎˜-In๎˜verse
Document Frequency (TF-
IDF):
๎˜ ๎˜ ๎˜ ๎˜ ๎˜ Term freque๎˜ncy๎˜โ€“in๎˜โ€
verse document
frequency, is a
numerical statistic that
is intended to reflect
how important a word is
to a document in a
collection or corpus.
๎˜ ๎˜ T.F = No of rep of
words in setence/No of
words in sentence
๎˜ ๎˜ IDF = No of sentences
/ No of sentences
containing words
from sklear๎˜n.f๎˜eat๎˜ure๎˜_ex๎˜โ€
tra๎˜cti๎˜on.text import
TfidfV๎˜ect๎˜orizer
cv = TfidfV๎˜ect๎˜ori๎˜zer()
X = cv.fit๎˜_tr๎˜ans๎˜for๎˜m(c๎˜โ€
oun๎˜ter๎˜s).t๎˜oa๎˜rray()
N-gram Language Model:
An N-gram is a sequence
of N tokens (or words).
A 1-gram (or unigram) is
a one-word sequen๎˜ce.the
unigrams would simply
be: โ€œIโ€, โ€œloveโ€,
โ€œreadingโ€, โ€œblogsโ€,
โ€œaboutโ€, โ€œdataโ€,
โ€œscienceโ€, โ€œonโ€, โ€œAnaly๎˜โ€
ticsโ€, โ€œVidhyaโ€.
Bag Of Words & TF-IDF (cont)
A 2-gram (or bigram) is
a two-word sequence of
words, like โ€œI loveโ€,
โ€œlove readingโ€, or
โ€œAnalytics Vidhyaโ€.
And a 3-gram (or
trigram) is a three-word
sequence of words like
โ€œI love readingโ€, โ€œabout
data scienceโ€ or โ€œon
Analytics Vidhyaโ€.
Stemming & Lemmat๎˜ization
From Stemming we will
process of getting the
root form of a word. We
would create the stem
words by removing the
prefix of suffix of a
word. So, stemming a
word may not result in
actual words.
paragraph = "๎˜"
# NLTK
from nltk.stem import
Porter๎˜Stemmer
from nltk import sent_t๎˜โ€
okenize
from nltk import word_t๎˜โ€
okenize
stem = Porter๎˜Ste๎˜mmer()
sentence = sent_t๎˜oke๎˜niz๎˜โ€
e(p๎˜ara๎˜gra๎˜ph)[1]
words = word_t๎˜oke๎˜niz๎˜e(s๎˜โ€
ent๎˜ence)
[stem.s๎˜te๎˜m(word) for
word in words]
# Spacy
By sree017
cheatography.com/sree017/
Published 26th September, 2020.
Last updated 26th September, 2020.
Page 1 of 3.
Sponsored by CrosswordCheats.com
Learn to solve cryptic crosswords!
http://crosswordcheats.com
pf3

Partial preview of the text

Download NLP Cheat Sheet and more Schemes and Mind Maps Mathematics in PDF only on Docsity!

by sree017 via cheatography.com/126402/cs/24446/

Tokenization Tokenization breaks the raw text into words, sentences called tokens. These tokens help in understanding the context or developing the model for the NLP. ... If the text is split into words using some separation technique it is called word tokenization and same separation done for sentences is called sentence tokenization.

NLTK

import nltk nltk.download('punkt') paragraph = "write paragaraph here to convert into tokens." sentences = nltk.sentโ€ _tokenize(paragraph) words = nltk.word_tokenโ€ ize(paragraph)

Spacy

from spacy.lang.en import English nlp = English() sbd = nlp.create_pipeโ€ ('sentencizer') nlp.add_pipe(sbd) doc = nlp(paragraph) [sent for sent in doc.sents] nlp = English() doc = nlp(paragraph) Tokenization (cont) [word for word in doc]

Keras

from keras.preprocessinโ€ g.text import text_to_wโ€ ord_sequence text_to_word_sequenceโ€ (paragraph)

genis

from gensim.summarizatiโ€ on.textcleaner import split_sentences split_sentences(paragโ€ raph) from gensim.utils import tokenize list(tokenize(paraโ€ graph)) Bag Of Words & TF-IDF Bag of Words model is used to preprocess the text by converting it into a bag of words, which keeps a count of the total occurrences of most frequently used words

counters = List of

stences after pre processing like tokeniโ€ zation, stemming/lemmatโ€ ization, stopwords from sklearn.feature_exโ€ traction.text import CountVectorizer cv = CountVectorizer(maโ€ x_features = 1500) Bag Of Words & TF-IDF (cont) X = cv.fit_transform(cโ€ ounters).toarray() Term Frequency-Inverse Document Frequency (TF- IDF): Term frequencyโ€“inโ€ verse document frequency, is a numerical statistic that is intended to reflect how important a word is to a document in a collection or corpus. T.F = No of rep of words in setence/No of words in sentence IDF = No of sentences / No of sentences containing words from sklearn.feature_exโ€ traction.text import TfidfVectorizer cv = TfidfVectorizer() X = cv.fit_transform(cโ€ ounters).toarray() N-gram Language Model: An N-gram is a sequence of N tokens (or words). A 1-gram (or unigram) is a one-word sequence.the unigrams would simply be: โ€œIโ€, โ€œloveโ€, โ€œreadingโ€, โ€œblogsโ€, โ€œaboutโ€, โ€œdataโ€, โ€œscienceโ€, โ€œonโ€, โ€œAnalyโ€ ticsโ€, โ€œVidhyaโ€. Bag Of Words & TF-IDF (cont) A 2-gram (or bigram) is a two-word sequence of words, like โ€œI loveโ€, โ€œlove readingโ€, or โ€œAnalytics Vidhyaโ€. And a 3-gram (or trigram) is a three-word sequence of words like โ€œI love readingโ€, โ€œabout data scienceโ€ or โ€œon Analytics Vidhyaโ€. Stemming & Lemmatization From Stemming we will process of getting the root form of a word. We would create the stem words by removing the prefix of suffix of a word. So, stemming a word may not result in actual words. paragraph = ""

NLTK

from nltk.stem import PorterStemmer from nltk import sent_tโ€ okenize from nltk import word_tโ€ okenize stem = PorterStemmer() sentence = sent_tokenizโ€ e(paragraph)[1] words = word_tokenize(sโ€ entence) [stem.stem(word) for word in words]

Spacy

By sree cheatography.com/sree017/ Published 26th September, 2020. Last updated 26th September, 2020. Sponsored by CrosswordCheats.com Learn to solve cryptic crosswords!

by sree017 via cheatography.com/126402/cs/24446/

Stemming & Lemmatization (cont) No Stemming in spacy

Keras

No Stemming in Keras Lemmatization: As stemming, lemmatโ€ ization do the same but the only difference is that lemmatization ensures that root word belongs to the language

NLTK

from nltk.stem import WordNetLemmatizer lemma = WordNetLemmaโ€ tizer() sentence = sent_tokeโ€ nize(paragraph)[1] words = word_tokenizโ€ e(sentence) [lemma.lemmatize(word) for word in words]

Spcay

import spacy as spac sp = spac.load('en_cโ€ ore_web_sm') ch = sp(u'warning warned') for x in ch: print(ch.lemma_)

Keras

No lemmatization or stemming Word2Vec In BOW and TF-IDF approach semantic information not stored. TF-IDF gives importance to uncommon words. There is definitely chance of overfitting. In W2v each word is basically represented as a vector of 32 or more dimension instead of a single number. Here the semantic information and relation between words is also preserved. Steps:

  1. Tokenization of the sentences
  2. Create Histograms
  3. Take most frequent words
  4. Create a matrix with all the unique words. It also represents the occurence relation between the words from gensim.models import Word2Vec model = Word2Vec(senโ€ tences, min_count=1) words = model.wv.vocab vector = model.wv['frโ€ eedom'] similar = model.wv.mosโ€ t_similar['freedom'] Stop Words Stopwords are the most common words in any natural language. For the purpose of analyzing text data and building NLP models, these stopwords might not add much value to the meaning of the document.

    NLTK

    from nltk.corpus import stopwords from nltk.tokenize import word_tokenize stopwords = set(stopwโ€ ords.words('english')) word_tokens = word_tโ€ okenize(paragraph) [word for word in word_tokens if word not in stopwords]

    Spacy

    from spacy.lang.en import English from spacy.lang.en.sโ€ top_words import STOP_WORDS nlp = English() my_doc = nlp(paragraph)

    Create list of word

    tokens token_list = [token.text for token in my_doc]

    Create list of word

    tokens after removing stopwords filtered_sentence =[] Stop Words (cont) for word in token_list: lexeme = nlp.vocabโ€ [word] if lexeme.is_stop == False: filtered_sentenโ€ ce.append(word) # Gensim from gensim.parsing.preproโ€ cessing import remove_stโ€ opwords remove_stopwords(paragraph) Tokenization NLTK Spacy Keras Tensorlfow dfdfd Parts of Speech (POS) Tagging, Chunking & NER The pos(parts of speech) explain you how a word is used in a sentence. In the sentence, a word have different contexts and semantic meanings. The basic natural language processing(NLP) models like bag-of-words(bow) fails to identify these relation between the words. For that we use pos tagging to mark a word to its pos tag based on its context in the data. Pos is also used to extract rlationship between the words # NLTK By sree cheatography.com/sree017/ Published 26th September, 2020. Last updated 26th September, 2020. Sponsored by CrosswordCheats.com Learn to solve cryptic crosswords!