Relationship Visualizer
Natural Language Processing
In the past few blog posts, we have seen how RStudio works to analyze numbers, yet RStudio can be used to analyze text just as well. Below is an R Shiny app developed from the Knowledge Graph Visualization blog from July of last year.
The above R Shiny app shows a network graph showing connections between consecutive nouns in a sentence.
YouTube Tutorial
The video that goes through the above in streaming format is below.
The code for this are as below:
Load Libraries
The Shiny library is for enabling the web app.
udpipe is for natural language processing.
igraph is for creating and manipulating graphs.
visNetwork is for creating network visualizations in the R environment.
Load Models
udpipe_download_model downloads the pre-trained model for English.
udpipe_load_model loads the downloaded model into the ud_model object.
Define Function to Extract Noun Chunks
“text” in the above function are text inputted by the user in the text input box.
The ud_model defined in the previous step is then used in the annotate text step.
The annotations are then converted to a dataframe.
This dataframe is then subsetted to only include nouns and proper nouns that refer to specific people, places, things, and organizations.
Lemma which is then used to reduce the nouns and proper nouns identified to singular forms. The from column takes all elements of the lemma column except the last one, where the to column takes all elements of the lemma column except the first one. This way, when we pair the elements in the from and to columns based on their order, we would be pairing each noun with the next one in a sentence.
The unique function then identifies the unique nodes in the from and to columns.
The return function returns a list containing the unique nodes and their edges.
User Interface
The above code chunk defines the elements for the user interface of the app, with the following text pre-filled in the user input field.
The Solar System is the gravitationally bound system of the Sun and the objects that orbit it, either directly or indirectly.
Of the objects that orbit the Sun directly, the largest are the eight planets, with the remainder being smaller objects, such as the five dwarf planets including Pluto, Eris, Haumea, Makemake, and Ceres and small Solar System bodies.
The largest planet is Jupiter, followed by Saturn, Uranus, and Neptune. The inner planets are Mercury, Venus, Earth, and Mars. Earth's moon, Luna, is one of the largest moons in the solar system.
The Solar System formed 4.6 billion years ago from the gravitational collapse of a giant interstellar molecular cloud.
Server Function
In the above server function code chunk, the observeEvent observes when the ‘plot’ button is being clicked. When it is clicked, the input text is retrieved from the text input field by input$text. The nouns in the input text are then extracted using the extract_noun_chunks function defined earlier.
is.null(noun_chunks) is then used to check to noun chunk extraction was successful.
visNetwork is then used to create the network plot with the specified nodes and edges in defined by the noun_chunks variable in the extract_noun_chunks function.
You Look Like a Thing and I Love You
It’s unusual to find a cell in a text-generating or text-analyzing algorithm that behaves as transparently as the sentiment neuron. The same goes for other types of neural networks - and that’s too bad, since we’d love to be able to tell when they’re making unfortunate mistakes and to learn from their strategies.







