

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


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.
import nltk nltk.download('punkt') paragraph = "write paragaraph here to convert into tokens." sentences = nltk.sentโ _tokenize(paragraph) words = nltk.word_tokenโ ize(paragraph)
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]
from keras.preprocessinโ g.text import text_to_wโ ord_sequence text_to_word_sequenceโ (paragraph)
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
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 = ""
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]
By sree cheatography.com/sree017/ Published 26th September, 2020. Last updated 26th September, 2020. Sponsored by CrosswordCheats.com Learn to solve cryptic crosswords!
Stemming & Lemmatization (cont) No Stemming in spacy
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
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]
import spacy as spac sp = spac.load('en_cโ ore_web_sm') ch = sp(u'warning warned') for x in ch: print(ch.lemma_)
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: