[ Powershell ] Data Harvesting all dictionary words for each alphabet from Web


PROBLEM : 

I am working on a personal project where in I require common/popular dictionary words for lookups and performance is a concern.

INITIAL THOUGHTS :

Figure out a way in which I can use In-built dictionary on Microsoft Word, but couldn’t find much on google search ( May be I didn’t tried enough :P) – Not gonna work!

What if I use a REST API of any online dictionary, but API calls would be slow and would end up in performance issuesNah not good enough!

How about Web Scraping/Harvesting all Dictionary words from a popular online dictionary and storing them locally which will make it easily accessible and won’t be a setback on performanceThis may work 🙂

Alright! let’s find an online dictionary and after some google search I landed on this website which has 3000 core vocabulary words starting with all 26 alphabets.

1

Moreover they had implemented a pagination logic such that if you click next on a page,

2

It will open a new page with more words from that alphabet and your URL appends a page number, like below

3

again on clicking next, page number keeps incrementing

4

Again and goes on.

Perfect! I believe I can implement a Powershell Function to traverse the Website’s pages using the pagination logic for each alphabet and then extract the words. Which will give me around 3000 core vocabulary words and would be good enough 🙂 So let’s start!

HOW IT WORKS :

URL syntax for this website is like –

http://learnersdictionary.com/3000-words/alpha/a/pagenumber

http://learnersdictionary.com/3000-words/alpha/a/2
http://learnersdictionary.com/3000-words/alpha/b/5
http://learnersdictionary.com/3000-words/alpha/c/3

 

Making a loop that increments the page number in every iteration, in order to generate the  next URL for an alphabet, but these pages has to be finite in number and we still have to figure out when to break/stop this loop.

OK! Go to last page of any alphabet and you may observe that the Pagination link to “Next page” is not present on the last page for all Alphabets. Eureka!  This means I can access the HTML DOM(Document Object Model) structure of every web page and if there is no link for the “Next page” that means you are on last page. Simple isn’t it? 🙂

6

Having figured out how to stop the iterations, now let’s gather the data simply by an –

Invoke-WebRequest "http://learnersdictionary.com/3000-words/alpha/a/2" 

in an iterative manner, to get parsedHTML as  a intermediate result, filter only the HTML tags in which data is sitting, like in the below screenshot.

5

 

Do some Data wrangling (conversion of data from raw to more consumable format)  and generate result as a output, keep iterating until you are on the last page of an alphabet.

Wrap this logic in a Powershell Function – “Get-DictionaryWord” and Run the below script to harvest dictionary words for all 26 Alphabets. Generating around 3000 words

count

I’m saving dictionary words in JSON files for each starting alphabet which can be access easily and quickly.

ezgif-com-gif-maker-1

SCRIPT  :

 

I hosted these dictionary JSON Files on my Github repo here and I will be using these  in some more awesome powershell functions in my coming blog posts, so stay tuned ! 😉

 

signature

Advertisements

One thought on “[ Powershell ] Data Harvesting all dictionary words for each alphabet from Web

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s