Gathering texts from the web is often the first step towards further analysis. Once a website has been downloaded, its contents can be explored, counted and even used to generate new text. This tutorial shows how to go from a website to generated sentences in a few steps, mostly on the command-line. This blog serves as an example.
All the necessary tools can be installed with pip (or pip3 where applicable):
$ pip install trafilatura somajo markovify
Gathering the texts
The tool for web text extraction I am working on, Trafilatura, can list the pages of a website from its sitemap without downloading them (see this post on sitemaps for more details). Tag and category pages are filtered out and the list is restricted to 10 posts in order to keep the download short:
# list the pages of the website using its sitemap
$ trafilatura --sitemap "https://adrien.barbaresi.eu/blog/sitemap.xml" --list > urls.txt
# keep 10 blog posts only
$ grep -v -e '/tag/' -e '/category/' urls.txt | grep '\.html$' | head -10 > posts.txt
The list then serves as input. The pages are downloaded and their main text is extracted:
$ trafilatura -i posts.txt > texts.txt
The download takes about a minute since trafilatura waits between two requests to the same website. In the end, the file contains roughly 10,000 words.
Frequent word sequences
The texts are then split into tokens with SoMaJo, one token per line (see also this post on tokenization). The tokenizer defaults to German, the -l en_PTB option selects English:
$ somajo-tokenizer -l en_PTB texts.txt > tokens.txt
Punctuation and empty lines are removed and the tokens are converted to lowercase:
$ < tokens.txt sed -e "s/[[:punct:]]//g" -e "/^$/d" | tr '[:upper:]' '[:lower:]' > tokens-filtered.txt
Finally, the tokens are put back on one line in order to print all sequences of three words. These trigrams are counted and the 10 most frequent ones are displayed:
$ < tokens-filtered.txt tr "\n" " " | awk '{for (i=1; i<NF-1; i++) print $i, $(i+1), $(i+2)}' | sort | uniq -c | sort -nrk1 | head -10
17 a series of
10 a list of
9 in order to
8 the command line
7 to look for
7 please refer to
6 on the command
6 here is how
5 which can be
5 to use the
As expected, function words come first. The topics of the blog only show through, for example with the command line. The list also says a lot about my writing habits. The results will vary as the blog grows.
Generating text
Markovify builds a Markov chain out of the texts, that is a simple model which predicts each word based on the previous ones. A few lines of Python are enough:
import markovify
# read the extracted texts
with open("texts.txt") as f:
text = f.read()
# build the model, well_formed=False keeps sentences with quotes or brackets
text_model = markovify.Text(text, well_formed=False)
# generate 5 sentences of 150 characters at most
for i in range(5):
print(text_model.make_short_sentence(150))
The length limit is useful since sentences combining several sources can get very long. The results differ with each run, here are a few of them:
Feeds are a good overview of a research lab.
Together with Python, it is a new kid on the command-line.
Here are examples of Python 3 is necessary.
The sentences sound like the blog without making much sense, which is all one can expect from such a simple model.
Going further
- The same steps work for any website with a sitemap, or with a feed using the
--feedoption of trafilatura. - For a neural approach to text generation, see for example textgenrnn.
- Please note that it is not considered fair to download a large number of pages from a website in a short period of time.