blob: 2b0d49250f30f61f2b25517ad72fe1b02d06deeb [file] [log] [blame]
Marc Kupietzbb4f54c2023-10-19 21:22:44 +02001#' syntagmaticNeighbours
2#'
3#' Get the syntagmatic neighbours of a word from the predictive derekovecs model.
4#' @param word The word to get the syntagmatic neighbours for.
5#' @param ... Additional parameters to pass to the API.
6#'
7#' @return Data frame syntagmatic neighbours of a word from the predictive derekovecs model.
8#' @export
Marc Kupietze981eae2023-10-18 09:00:17 +02009syntagmaticNeighbours <- function(word = "Test", ...) {
Marc Kupietzbb4f54c2023-10-19 21:22:44 +020010 derekovecsApiCall("", word = word, json = 1, ...)$collocators
Marc Kupietze981eae2023-10-18 09:00:17 +020011}
12
Marc Kupietzbb4f54c2023-10-19 21:22:44 +020013#' countbasedCollocates
14#'
15#' Get the collocates of a word in the count-based dereko model.
16#'
17#' @param w The word to get the collocates for.
18#' @param ... Additional parameters to pass to the API.
19#'
20#' @return A data fram with the most salient collcates and their association scores.
21#' @export
Marc Kupietze981eae2023-10-18 09:00:17 +020022countbasedCollocates <- function(w = "Test", ...) {
Marc Kupietzbb4f54c2023-10-19 21:22:44 +020023 derekovecsApiCall(method = "/getClassicCollocators", w = w, ...)$collocates
Marc Kupietze981eae2023-10-18 09:00:17 +020024}
25
Marc Kupietzbb4f54c2023-10-19 21:22:44 +020026#' paradigmaticNeighbours
27#'
28#' Get the paradigmatic neighbours of a word in the derekovecs model.
29#'
30#' @param word The word to get the paradigmatic neighbours for.
31#' @param ... Additional parameters to pass to the API.
32#' @return A list of words with their similarity scores.
33#' @export
34#'
Marc Kupietze981eae2023-10-18 09:00:17 +020035paradigmaticNeighbours <- function(word = "Test", ...) {
Marc Kupietzbb4f54c2023-10-19 21:22:44 +020036 derekovecsApiCall("", word = word, json = 1, ...)$list[[1]]
Marc Kupietze981eae2023-10-18 09:00:17 +020037}
38
Marc Kupietzbb4f54c2023-10-19 21:22:44 +020039#' collocationScores
40#'
41#' Calculate the association scores between a node (target word) and words in a window around the it.
42#'
43#' @param w The target word/node.
44#' @param c The collocate.
45#' @param ... Additional parameters to pass to the API.
46#'
47#' @return A one row data frame with collocate and its association scores.
48#' @export
49#'
Marc Kupietze981eae2023-10-18 09:00:17 +020050collocationScores <- function(w, c, ...) {
Marc Kupietzbb4f54c2023-10-19 21:22:44 +020051 derekovecsApiCall("/getCollocationAssociation",
52 w = w, c = c, ...)$collocates
Marc Kupietze981eae2023-10-18 09:00:17 +020053}
54
Marc Kupietzbb4f54c2023-10-19 21:22:44 +020055#' cosineSimilarity
56#'
57#' @param w1 The first word.
58#' @param w2 The second word.
59#' @param ... Additional parameters to pass to the API.
60#'
61#' @return The cosine similarity between the two words.
62#' @export
63#'
64#' @description Calculate the cosine similarity between two words in the derekovecs model.
Marc Kupietze981eae2023-10-18 09:00:17 +020065cosineSimilarity <- function(w1, w2, ...) {
Marc Kupietzbb4f54c2023-10-19 21:22:44 +020066 derekovecsApiCall("/getSimilarity", w1 = w1, w2 = w2, ...)
67}
68
69#' DeReKoVecsServer
70#'
71#' @return The URL of the DeReKoVecs API server.
72#' @export
73#'
74derekovecsServer <- function() {
75 api_server <- Sys.getenv("DEREKOVECS_SERVER")
76 if (!identical(api_server, "")) {
77 return(api_server)
78 }
79 'https://corpora.ids-mannheim.de/openlab/derekovecs/'
80}
81
82#' DeReKoVecsCall
83#'
84#' Call the DeReKoVecs API.
85#'
86#' @param method The method to call.
87#' @param ... The parameters to pass to the method.
88#' @return The result of the call.
89#' @importFrom httr2 request req_url_path_append req_url_query req_perform resp_body_json
90#'
91#' @include utils-pipe.R
92#'
93derekovecsApiCall <- function(method = "", ...) {
94 httr2::request(derekovecsServer()) %>%
95 httr2::req_url_path_append(method) %>%
96 httr2::req_url_query(...) %>%
97 httr2::req_perform() %>%
98 httr2::resp_body_json(simplifyVector = TRUE)
Marc Kupietze981eae2023-10-18 09:00:17 +020099}