Open In App

dictionaRy package in R Language

Last Updated : 02 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will be discussing how to use the dictionaRy package of the R Programming language.

dictionaRy is an R interface to the ‘Free Dictionary API’. It can be used to get data about an English word like definition, part of speech, origins, pronunciation, etc. We will be using this package to get definitions, parts of speech, and examples of the function and also discuss other approaches as well.

Installation:

install.packages(“dictionaRy”)

Get the definition of a word:

  • define() will return JSON data
  • select() will only get the selected data from the JSON data that is declared inside parenthesis.

Note: We will also be using the tidyverse package to use select() for easy syntax. It’s not compulsory.

To get started first we need to import dictionaRy and tidyverse package. Now we will need a word for further process and store it in a variable, for this example, we will be using “greetings” as our input and variable name word.

After all this, we will pass word variable in a define() and store the JSON response in a variable named “data”. For clean and easy syntax we will be using select() to get values from json. Here select() will need a parameter and it will search for the matching key and return its value.

R




# installing package
install.packages("dictionaRy")
 
# importing packages
library(tidyverse)
library(dictionaRy)
 
# storing word in a variable
word <- "greetings"
 
# requesting data of the word
data <- define(word)
 
# getting definition of the word
# note: select() belongs to dplyr
# package
data %>% select(definition)


Output:

# A tibble: 3 x 1

 definition                                                              

 <chr>                                                                  

1 A conventional phrase used to start a letter or conversation or otherw~

2 The action of the verb to greet.                                        

3 (sometimes formal, sometimes humorous) hello  

Scrap more info about the word:

For this, we will again be importing dictionaRy and tidyverse package. Now we will take another word for this example and store it in a variable, for this example we will be using “hello” as our input and variable name word. After all this, we will pass words variable in a define() and store the JSON response in a variable named “data“.

As define() returns JSON so it also has several keys like part of speech, for example, phonetics, and audio, So we will be passing all these values one by one in select() to get a complete response present in JSON.

Example:

R




# installing package
install.packages("dictionaRy")
 
# importing packages
library(tidyverse)
library(dictionaRy)
 
# storing word in a variable
word <- "hello"
 
# requesting data of the word
data <- define(word)
print(data)
 
# getting definition of the word
# note: select() belongs to dplyr package
data %>% select(definition)
 
# getting part of speech of the word
data %>% select(part_of_speech)
 
# getting example of the word
data %>% select(example)
 
# getting phonetics of the word
data %>% select(phonetic)
 
# gets url of audio on how to spell the word
data %>% select(audio)


Output:

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads