loading

This is an exercise to create a Word Cloud for any Wikipedia article and control the rotation of the words in the word cloud.

To do this, we need to first figure out the role of each function we need - WikipediaData[], DeleteStopwords[], WordCloud[] and the option to rotate words by WordOrientation[].

Finally, we put everything together and let the users enter their desired article using FormFunction[] and deploy it using CloudDeploy[].

Step 1: Get the Words From a Wikipedia Article

We can do this by using the WikipediaData[] function. As an example, enter "United States of America" as the Wiki article title.

Code:

words = WikipediaData["United States of America"]

Step 2: Removing Common Words

We need the wordcloud to show words which are related to the article. Therefore, we need to delete words which are most common in the language such as the, is, at, which, on, etc. We can do this by using DeleteStopwords[] function.

Code:

DeleteStopwords[WikipediaData["United States of America"]]

Step 3: Constructing the Word Cloud

We construct the word cloud for the article by evaluating the WordCloud[] function.

Code:

WordCloud[DeleteStopwords[WikipediaData["United States of America"]]]

Step 4: Introducing Rotation for Words in the Word Cloud

Using the WordOrientation[] option in WordCloud, we can introduce rotation for the words in the Word Cloud. There are numerous ways to orient the words. You can check them out here - http://reference.wolfram.com/language/ref/WordOri....

Here, we will using "HorizontalVertical" which uses a value between 0 (0%) and 1 (100%) to determine what percentage of the words would be horizontally oriented.

Code:

WordCloud[DeleteStopwords[WikipediaData["United States of America"]], WordOrientation -> {"HorizontalVertical", 0.1}]

Step 5: Assigning What We Have to a Function Definition

As you can see, the code is getting longer and longer. To make the code cleaner, we will assign all this to a function we define. We need not do this but it will be easier to understand the code.

Till now, we used an example article, "United States of America" to evaluate the code. Since we need the user to input the article they want a word cloud of, we will use "subject" to denote the article title they want and "horiz" to denote the percentage of words they need to be horizontal. Please note the ":=" after our defined function "wikiFunc". This is used to delay the evaluation of our function. At this point, the user has not input the "subject" but we need to evaluate this function to assign the values. Therefore, we are telling the program that the values for "subject" and "horiz" will be filled in later but the rest of the code can be evaluated now.

Please note that since we have used delayed evaluation, there will not be an output for this line of code.

Code:

wikiFunc[subject_, horiz_] := WordCloud[DeleteStopwords[WikipediaData[subject]], WordOrientation -> {"HorizontalVertical", horiz}]

Step 6: The FormFunction[]

Now for the user to input the "subject" and "rot", we use the FormFunction[]. Let's break this line of code down.

The first part, "{{"subject","Article Title"}->"String",{"rot","Percent of Words that are horizontal"}->"Number"}" is the input fields for the Form. This tells the program that the user will input "subject" where the form asks with the text "Article Title" and it will be a String. The same goes with "horiz".

The second part, "wikiFunc[#subject,#rot]&,"PNG"" runs the function which you defined before with the variables "subject" and "rot". "PNG" means that the final output i.e the Word Cloud will be a picture in the PNG format.

Finally, we have the last part which sets the Appearance rules for your form. It has attributes such as Title, Description and a lot of other options which you can find here - http://reference.wolfram.com/language/ref/Appeara... Please note that we have put all this into our defined function, "formFunc" with delayed evaluation (using :=) so that the code for the final line will be easier to understand.

Again, since we used a delayed evaluation, there will not be any output for this line of code.

Code:

formFunc := FormFunction[{{"subject", "Article Title"} -> "String", {"horiz", "Percent of Words that are horizontal"} -> "Number"}, wikiFunc[#subject, #horiz] &, "PNG", AppearanceRules -> <|"Title" -> "WikiWord Cloud", "Description" -> "Generate a word cloud for any wikipedia article"|>]

Step 7: Deploying to the Cloud

Lastly, we will deploy the program to the cloud using CloudDeploy[] so that you can run it in any web browser.

We have put all the things we want in the FormFunction[] into formFunc.

The first part of CloudDeploy evaluates this. Next is the name of the cloud program we have deployed which will be there in the link you get after you evaluate this line of code. Finally, we set the Permissions to Public so that anyone who has clicked the link can evaluate the program.

Code:

CloudDeploy[formFunc, "WikiWordCloudform", Permissions -> "Public"]

Step 8: That's It! Now You Can Have a Word Cloud of Any Article in Wikipedia in a Picture!

Clicking on that final link will give you the Form in which the user will input the article they want a Wiki Word Cloud of!

To make you more comfortable in the Wolfram Development Platform, clicking on the link below will give you the code for this example. But don't just stop there! Tinker around and make your awesome changes to your Wiki WordCloud using different Appearance Rules. Click on the "New" icon on the right to create a new notebook and code away!

https://wolfr.am/7sWKG8pd

In case you're wondering how the program would've looked without using functions we defined, here it is! Its long but going through it carefully, we can see what each part of the code is doing.

Code:

CloudDeploy[FormFunction[{{"subject", "Article Title"} -> "String", {"horiz", "Percent of Words that should be horizontal"} -> "Number"}, WordCloud[DeleteStopwords[WikipediaData[#subject]], WordOrientation -> {"HorizontalVertical", #horiz}] &, "PNG", AppearanceRules -> <|"Title" -> "WikiWord Cloud", "Description" -> "Generate a word cloud for any wikipedia article"|>], "WikiWordCloudform", Permissions -> "Public"]

About This Instructable

602views

7favorites

License:

More by Seb Pattom:How to Create an Instant API using Wolfram Language Creating a Wiki Word Cloud using Wolfram Language Creating a Web Form in Wolfram Language 
Add instructable to: