After scraped online, I got the children's books in text format from Gutenberg.com. Now I would like to analyze the words. But I failed to do the tokenization since the content turned out to be list of lists.
The content is sth like below:
raw[0]
['ALICE’S ADVENTURES IN WONDERLAND', 'Lewis Carroll', 'THE MILLENNIUM FULCRUM EDITION 3.0', 'CHAPTER I. Down the Rabbit-Hole', 'Alice was beginning to get very tired of sitting by her sister on the', 'bank, and of having nothing to do: once or twice she had peeped into the', 'book her sister was reading, but it had no pictures or conversations in', 'it, ‘and what is the use of a book,’ thought Alice ‘without pictures or', 'conversations?’', 'So she was considering in her own mind (as well as she could, for the', 'hot day made her feel very sleepy and stupid), whether the pleasure', 'of making a daisy-chain would be worth the trouble of getting up and', 'picking the daisies, when suddenly a White Rabbit with pink eyes ran', 'close by her.', '‘There might be some sense in your knocking,’ the Footman went on', ...]
import nltk
import pickle
with open('tokens.data', 'rb') as filehandle:
# read the data as binary data stream
raw = pickle.load(filehandle)
raw[0]
len(raw) -> 407 Which means we got 407 children's book.
type(raw) -> List Each list stands for one book.
from nltk.tokenize import sent_tokenize, word_tokenize
tokenized_sents = [word_tokenize(i) for i in raw[0]]
for i in tokenized_sents:
print (i)
['ALICE', '’', 'S', 'ADVENTURES', 'IN', 'WONDERLAND']
['Lewis', 'Carroll']
['THE', 'MILLENNIUM', 'FULCRUM', 'EDITION', '3.0']
......
['remembering', 'her', 'own', 'child-life', ',', 'and', 'the', 'happy',
'summer', 'days', '.']
['THE', 'END']
The thing is that I only could do like raw[0], raw[1], ...... Then how to apply lambda in this ?