-
Notifications
You must be signed in to change notification settings - Fork 0
/
raw_book.py
42 lines (33 loc) · 986 Bytes
/
raw_book.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
"""
File: raw_book.py
Author: MTG
Topic: request
This module implements get a book from Project Gutenber and plot the frequency of a word that apper in the book.
"""
import requests
import matplotlib.pyplot as plt
import nltk
def get_raw_book(url):
"""Request a book from url
Args:
url ([type]): website
Returns:
txt: text file of the book
"""
response = requests.get(url)
raw = response.text
return raw
def get_plot(book, word):
"""Plot frequency of one word
Args:
book ([type]): [description]
"""
tokens = nltk.word_tokenize(book)
tokens = [token.lower() for token in tokens]
text = nltk.Text(tokens)
plt.figure(figsize=(8, 8))
text.dispersion_plot([word])
if __name__ == "__main__":
url_book = 'https://gist.githubusercontent.com/phillipj/4944029/raw/75ba2243dd5ec2875f629bf5d79f6c1e4b5a8b46/alice_in_wonderland.txt'
book = get_raw_book(url_book)
get_plot(book, 'forty-two')