Marc Kupietz | bb4f54c | 2023-10-19 21:22:44 +0200 | [diff] [blame] | 1 | #' 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 Kupietz | e981eae | 2023-10-18 09:00:17 +0200 | [diff] [blame] | 9 | syntagmaticNeighbours <- function(word = "Test", ...) { |
Marc Kupietz | bb4f54c | 2023-10-19 21:22:44 +0200 | [diff] [blame] | 10 | derekovecsApiCall("", word = word, json = 1, ...)$collocators |
Marc Kupietz | e981eae | 2023-10-18 09:00:17 +0200 | [diff] [blame] | 11 | } |
| 12 | |
Marc Kupietz | bb4f54c | 2023-10-19 21:22:44 +0200 | [diff] [blame] | 13 | #' 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 Kupietz | e981eae | 2023-10-18 09:00:17 +0200 | [diff] [blame] | 22 | countbasedCollocates <- function(w = "Test", ...) { |
Marc Kupietz | bb4f54c | 2023-10-19 21:22:44 +0200 | [diff] [blame] | 23 | derekovecsApiCall(method = "/getClassicCollocators", w = w, ...)$collocates |
Marc Kupietz | e981eae | 2023-10-18 09:00:17 +0200 | [diff] [blame] | 24 | } |
| 25 | |
Marc Kupietz | bb4f54c | 2023-10-19 21:22:44 +0200 | [diff] [blame] | 26 | #' 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 Kupietz | e981eae | 2023-10-18 09:00:17 +0200 | [diff] [blame] | 35 | paradigmaticNeighbours <- function(word = "Test", ...) { |
Marc Kupietz | bb4f54c | 2023-10-19 21:22:44 +0200 | [diff] [blame] | 36 | derekovecsApiCall("", word = word, json = 1, ...)$list[[1]] |
Marc Kupietz | e981eae | 2023-10-18 09:00:17 +0200 | [diff] [blame] | 37 | } |
| 38 | |
Marc Kupietz | bb4f54c | 2023-10-19 21:22:44 +0200 | [diff] [blame] | 39 | #' 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 Kupietz | e981eae | 2023-10-18 09:00:17 +0200 | [diff] [blame] | 50 | collocationScores <- function(w, c, ...) { |
Marc Kupietz | bb4f54c | 2023-10-19 21:22:44 +0200 | [diff] [blame] | 51 | derekovecsApiCall("/getCollocationAssociation", |
| 52 | w = w, c = c, ...)$collocates |
Marc Kupietz | e981eae | 2023-10-18 09:00:17 +0200 | [diff] [blame] | 53 | } |
| 54 | |
Marc Kupietz | bb4f54c | 2023-10-19 21:22:44 +0200 | [diff] [blame] | 55 | #' 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 Kupietz | e981eae | 2023-10-18 09:00:17 +0200 | [diff] [blame] | 65 | cosineSimilarity <- function(w1, w2, ...) { |
Marc Kupietz | bb4f54c | 2023-10-19 21:22:44 +0200 | [diff] [blame] | 66 | derekovecsApiCall("/getSimilarity", w1 = w1, w2 = w2, ...) |
| 67 | } |
| 68 | |
| 69 | #' DeReKoVecsServer |
| 70 | #' |
| 71 | #' @return The URL of the DeReKoVecs API server. |
| 72 | #' @export |
| 73 | #' |
| 74 | derekovecsServer <- 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 | #' |
| 93 | derekovecsApiCall <- 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 Kupietz | e981eae | 2023-10-18 09:00:17 +0200 | [diff] [blame] | 99 | } |