-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokenization.py
63 lines (42 loc) · 2.49 KB
/
tokenization.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# -*- coding: utf-8 -*-
"""Untitled16.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1w7qOuuGMSa-n9TVJkPg7PPUjpL2XBTl1
"""
import nltk
nltk.download('punkt')
from nltk.tokenize import sent_tokenize
text = "Hello, you are reading about Tokenization. There are many forms of Tokenizers."
sent_tokenize(text)
import nltk.data
newsent= "Tokenization is a way of separating a piece of text into smaller units called tokens.Here, tokens can be either words, characters, or subwords. Hence, tokenization can be broadly classified into 3 types – word, character, and subword (n-gram characters) tokenization.For example, consider the sentence:Never give up.The most common way of forming tokens is based on space. Assuming space as a delimiter, the tokenization of the sentence results in 3 tokens – Never-give-up. As each token is a word, it becomes an example of Word tokenization.Similarly, tokens can be either characters or subwords. For example, let us consider “smarter”:Character tokens: s-m-a-r-t-e-rSubword tokens: smart-er.But then is this necessary? Do we really need tokenization to do all of this?"
# Loading PunktSentenceTokenizer using English pickle file
tokenizer = nltk.data.load('tokenizers/punkt/PY3/english.pickle')
tokenizer.tokenize(newsent)
import nltk.data
spanish_tokenizer = nltk.data.load('tokenizers/punkt/PY3/spanish.pickle')
text1 = 'Hola amigo. Estoy bien.'
spanish_tokenizer.tokenize(text1)
from nltk.tokenize import word_tokenize
text = "Hello, you are reading about Tokenization. There are many forms of Tokenizers."
word_tokenize(text)
from nltk.tokenize import TreebankWordTokenizer
tokenizer = TreebankWordTokenizer()
tokenizer.tokenize(text)
from nltk.tokenize import WordPunctTokenizer
tokenizer = WordPunctTokenizer()
tokenizer.tokenize("Let's see how it's working.")
from nltk.tokenize import RegexpTokenizer
tokenizer = RegexpTokenizer("[\w']+")
text = "Hello, you are reading about Tokenization. There are many forms of Tokenizers."
tokenizer.tokenize(text)
from nltk.tokenize import regexp_tokenize
text = "Hello, you are reading about Tokenization. There are many forms of Tokenizers."
regexp_tokenize(text, "[\w']+")
from nltk.tokenize import TweetTokenizer
tknzr = TweetTokenizer(strip_handles=True, reduce_len=True)
s1 = '@remy: This is waaaaayyyy too much for you!!!!!!'
tknzr.tokenize(s1)
from nltk.tokenize import SExprTokenizer
SExprTokenizer().tokenize('(a b (c d)) e f (g)')