Wiki WordCloud Form

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[].

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.

words=WikipediaData["United States of America"]

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.

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

3. Constructing the Word Cloud

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

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

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 - MouseOver.
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.

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

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.

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

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 - MouseOver.
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.

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"|>]

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.

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

That’s it! Now you can have a Word Cloud of any article in Wikipedia in a picture “PNG” format without commonly used English words!

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.

CloudDeploy[FormFunction[{{"subject","Article Title"}->"String",{"horiz","Percent of Words that are 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"]