Marc Kupietz | c6a66ee | 2023-10-23 13:18:48 +0200 | [diff] [blame] | 1 | #' Get syntagmatic neighbours |
Marc Kupietz | bb4f54c | 2023-10-19 21:22:44 +0200 | [diff] [blame] | 2 | #' |
Marc Kupietz | c6a66ee | 2023-10-23 13:18:48 +0200 | [diff] [blame] | 3 | #' Get the syntagmatic neighbour predictions of a word from the DeReKoVecs model (see Fankhauser/Kupietz 2022, 2017). |
| 4 | #' |
Marc Kupietz | bb4f54c | 2023-10-19 21:22:44 +0200 | [diff] [blame] | 5 | #' @param word The word to get the syntagmatic neighbours for. |
| 6 | #' @param ... Additional parameters to pass to the API. |
| 7 | #' |
Marc Kupietz | c6a66ee | 2023-10-23 13:18:48 +0200 | [diff] [blame] | 8 | #' @return Data frame with the syntagmatic neighbours of a node predicted from derekovecs model, with the following columns: |
| 9 | #' |
| 10 | #' \describe{ |
| 11 | #' \item{average}{⟨a⟩ - Average raw activation of the collocator in the columns selected by auto-focus.} |
| 12 | #' \item{heat}{Vector of activation of the respective collocator in the slots around the target normalized by its maximum.} |
| 13 | #' \item{max}{max(a) - Maximum activation of the collocator anywhere in the output layer.} |
| 14 | #' \item{overall}{Σa/Σw – Sum of the activations over the whole window normalized by the total window sum (no auto-focus).} |
| 15 | #' \item{pos}{Binary encoded position of where in the window around the node the collocate is predecited with above 0 probability, e.g. 64 = 2^6 ≙ 00010 node 00000} |
| 16 | #' \item{rank}{Frequency rank of predicted collocate} |
| 17 | #' \item{word}{Predicted collocate} |
| 18 | #' } |
Marc Kupietz | bb4f54c | 2023-10-19 21:22:44 +0200 | [diff] [blame] | 19 | #' @export |
Marc Kupietz | e981eae | 2023-10-18 09:00:17 +0200 | [diff] [blame] | 20 | syntagmaticNeighbours <- function(word = "Test", ...) { |
Marc Kupietz | bb4f54c | 2023-10-19 21:22:44 +0200 | [diff] [blame] | 21 | derekovecsApiCall("", word = word, json = 1, ...)$collocators |
Marc Kupietz | e981eae | 2023-10-18 09:00:17 +0200 | [diff] [blame] | 22 | } |
| 23 | |
Marc Kupietz | c6a66ee | 2023-10-23 13:18:48 +0200 | [diff] [blame] | 24 | #' Get count-based collocates |
Marc Kupietz | bb4f54c | 2023-10-19 21:22:44 +0200 | [diff] [blame] | 25 | #' |
| 26 | #' Get the collocates of a word in the count-based dereko model. |
| 27 | #' |
| 28 | #' @param w The word to get the collocates for. |
| 29 | #' @param ... Additional parameters to pass to the API. |
| 30 | #' |
Marc Kupietz | c6a66ee | 2023-10-23 13:18:48 +0200 | [diff] [blame] | 31 | #' @return A data frame with the most salient collocates and their association scores. |
| 32 | #' @seealso [collocationScores()] for details |
Marc Kupietz | bb4f54c | 2023-10-19 21:22:44 +0200 | [diff] [blame] | 33 | #' @export |
Marc Kupietz | e981eae | 2023-10-18 09:00:17 +0200 | [diff] [blame] | 34 | countbasedCollocates <- function(w = "Test", ...) { |
Marc Kupietz | bb4f54c | 2023-10-19 21:22:44 +0200 | [diff] [blame] | 35 | derekovecsApiCall(method = "/getClassicCollocators", w = w, ...)$collocates |
Marc Kupietz | e981eae | 2023-10-18 09:00:17 +0200 | [diff] [blame] | 36 | } |
| 37 | |
Marc Kupietz | c6a66ee | 2023-10-23 13:18:48 +0200 | [diff] [blame] | 38 | #' Get paradigmatic neighbours |
Marc Kupietz | bb4f54c | 2023-10-19 21:22:44 +0200 | [diff] [blame] | 39 | #' |
| 40 | #' Get the paradigmatic neighbours of a word in the derekovecs model. |
| 41 | #' |
| 42 | #' @param word The word to get the paradigmatic neighbours for. |
| 43 | #' @param ... Additional parameters to pass to the API. |
| 44 | #' @return A list of words with their similarity scores. |
| 45 | #' @export |
| 46 | #' |
Marc Kupietz | e981eae | 2023-10-18 09:00:17 +0200 | [diff] [blame] | 47 | paradigmaticNeighbours <- function(word = "Test", ...) { |
Marc Kupietz | bb4f54c | 2023-10-19 21:22:44 +0200 | [diff] [blame] | 48 | derekovecsApiCall("", word = word, json = 1, ...)$list[[1]] |
Marc Kupietz | e981eae | 2023-10-18 09:00:17 +0200 | [diff] [blame] | 49 | } |
| 50 | |
Marc Kupietz | db9bb1a | 2023-10-24 16:03:36 +0200 | [diff] [blame] | 51 | #' Get word embedding |
| 52 | #' |
| 53 | #' Get the normalized embedding vector of a word from the derekovecs model. |
| 54 | #' |
| 55 | #' @param word The word to get the paradigmatic neighbours for. |
| 56 | #' @param ... Additional parameters to pass to the API. |
| 57 | #' @return Normalized embedding vector of the given word. |
| 58 | #' @export |
| 59 | #' |
| 60 | wordEmbedding <- function(word = "Test", ...) { |
| 61 | derekovecsApiCall("", word = word, n=1, json = 1, ...)[["list"]][[1]][["vector"]][[1]] |
| 62 | } |
| 63 | |
| 64 | #' Get frequency rank |
| 65 | #' |
| 66 | #' Gets the frequency rank of a word in the training data. |
| 67 | #' |
| 68 | #' @param word The word to get the frequency rank of. |
| 69 | #' @param ... Additional parameters to pass to the API. |
| 70 | #' @return Frequency rank. |
| 71 | #' @export |
Marc Kupietz | db9bb1a | 2023-10-24 16:03:36 +0200 | [diff] [blame] | 72 | frequencyRank <- function(word = "Test", ...) { |
| 73 | derekovecsApiCall("/getWord", w = word, ...)$frequencyRank |
| 74 | } |
| 75 | |
Marc Kupietz | f977fa7 | 2023-11-05 18:02:39 +0100 | [diff] [blame] | 76 | #' Get derekovecs server version |
| 77 | #' @return The version of the derekovecs server. |
| 78 | #' @export |
| 79 | serverVersion <- function() { |
| 80 | derekovecsApiCall("/getVersion") |
| 81 | } |
| 82 | |
| 83 | #' Get vocabulary size |
| 84 | #' @return The vocabulary size of the model. |
| 85 | #' @export |
| 86 | #' @seealso [frequencyRank()] |
| 87 | vocabSize <- function() { |
| 88 | derekovecsApiCall("/getVocabSize") |
| 89 | } |
| 90 | |
| 91 | #' Get model name |
| 92 | #' @return The name of the model. |
| 93 | #' @export |
| 94 | modelName <- function() { |
| 95 | derekovecsApiCall("/getModelName") |
| 96 | } |
| 97 | |
Marc Kupietz | c6a66ee | 2023-10-23 13:18:48 +0200 | [diff] [blame] | 98 | #' Get collocation scores |
Marc Kupietz | bb4f54c | 2023-10-19 21:22:44 +0200 | [diff] [blame] | 99 | #' |
| 100 | #' Calculate the association scores between a node (target word) and words in a window around the it. |
| 101 | #' |
| 102 | #' @param w The target word/node. |
| 103 | #' @param c The collocate. |
| 104 | #' @param ... Additional parameters to pass to the API. |
| 105 | #' |
| 106 | #' @return A one row data frame with collocate and its association scores. |
Marc Kupietz | c6a66ee | 2023-10-23 13:18:48 +0200 | [diff] [blame] | 107 | #' \describe{ |
| 108 | #' \item{word}{collocate} |
| 109 | #' \item{f2}{abs. frequency of collocate} |
| 110 | #' \item{f}{abs. frequency of collocation} |
| 111 | #' \item{npmi}{normalized pmi (Bouma 2009)} |
| 112 | #' \item{pmi}{pointwise mutual information} |
| 113 | #' \item{dice}{dice score} |
| 114 | #' \item{ld}{log-dice score (Rychlý 2008) for whole window} |
| 115 | #' \item{lfmd}{log-frequency biased mutual dependency ≙ pmi³ (Dalle 1994; Thanopoulos et al. 2002)} |
| 116 | #' \item{llr}{log-likelihood (Dunning 1993; Evert 2004)} |
| 117 | #' \item{ln_count}{frequency of collocate as left neighbour of node} |
| 118 | #' \item{ln_pmi}{pmi as left neighbour} |
| 119 | #' \item{md}{mutual dependency ≙ pmi² (Dalle 1994; Thanopoulos et al. 2002)} |
| 120 | #' \item{rn_count}{frequency of collocate as right neighbour of node} |
| 121 | #' \item{rn_pmi}{pmi as right neighbour} |
| 122 | #' \item{ldaf}{log-dice score for auto focus window} |
| 123 | #' \item{win}{binary encoded positions at which the collocate appears at least once, e.g.: 1023 = 2^10-1 ≙ 11111 node 11111} |
| 124 | #' \item{afwin}{binary encoded auto-focus window (see Perkuhn et al. 2012: E8-15), e.g. 64 = 2^6 ≙ 00010 node 00000 (Aus gutem Grund)} |
| 125 | #' } |
| 126 | #' @references |
| 127 | #' Daille, B. (1994): Approche mixte pour l’extraction automatique de terminologie: statistiques lexicales et filtres linguistiques. PhD thesis, Université Paris 7. |
| 128 | #' |
| 129 | #' Dunning, T. (1993): Accurate methods for the statistics of surprise and coincidence. Comput. Linguist. 19, 1 (March 1993), 61-74. |
| 130 | #' |
| 131 | #' Evert, Stefan (2004): The Statistics of Word Cooccurrences: Word Pairs and Collocations. PhD dissertation, IMS, University of Stuttgart. Published in 2005, URN urn:nbn:de:bsz:93-opus-23714. |
| 132 | #' Free PDF available from <https://purl.org/stefan.evert/PUB/Evert2004phd.pdf> |
| 133 | #' |
| 134 | #' Thanopoulos, A., Fakotakis, N., Kokkinakis, G. (2002): Comparative evaluation of collocation extraction metrics. In: Proc. of LREC 2002: 620–625. |
| 135 | #' |
Marc Kupietz | bb4f54c | 2023-10-19 21:22:44 +0200 | [diff] [blame] | 136 | #' @export |
| 137 | #' |
Marc Kupietz | e981eae | 2023-10-18 09:00:17 +0200 | [diff] [blame] | 138 | collocationScores <- function(w, c, ...) { |
Marc Kupietz | bb4f54c | 2023-10-19 21:22:44 +0200 | [diff] [blame] | 139 | derekovecsApiCall("/getCollocationAssociation", |
| 140 | w = w, c = c, ...)$collocates |
Marc Kupietz | e981eae | 2023-10-18 09:00:17 +0200 | [diff] [blame] | 141 | } |
| 142 | |
Marc Kupietz | c6a66ee | 2023-10-23 13:18:48 +0200 | [diff] [blame] | 143 | #' Get cosine similarity |
Marc Kupietz | bb4f54c | 2023-10-19 21:22:44 +0200 | [diff] [blame] | 144 | #' |
| 145 | #' @param w1 The first word. |
| 146 | #' @param w2 The second word. |
| 147 | #' @param ... Additional parameters to pass to the API. |
| 148 | #' |
| 149 | #' @return The cosine similarity between the two words. |
| 150 | #' @export |
| 151 | #' |
| 152 | #' @description Calculate the cosine similarity between two words in the derekovecs model. |
Marc Kupietz | e981eae | 2023-10-18 09:00:17 +0200 | [diff] [blame] | 153 | cosineSimilarity <- function(w1, w2, ...) { |
Marc Kupietz | bb4f54c | 2023-10-19 21:22:44 +0200 | [diff] [blame] | 154 | derekovecsApiCall("/getSimilarity", w1 = w1, w2 = w2, ...) |
| 155 | } |
| 156 | |
Marc Kupietz | c6a66ee | 2023-10-23 13:18:48 +0200 | [diff] [blame] | 157 | #' Get the DeReKoVecs server |
Marc Kupietz | bb4f54c | 2023-10-19 21:22:44 +0200 | [diff] [blame] | 158 | #' |
| 159 | #' @return The URL of the DeReKoVecs API server. |
| 160 | #' @export |
| 161 | #' |
| 162 | derekovecsServer <- function() { |
| 163 | api_server <- Sys.getenv("DEREKOVECS_SERVER") |
| 164 | if (!identical(api_server, "")) { |
| 165 | return(api_server) |
| 166 | } |
| 167 | 'https://corpora.ids-mannheim.de/openlab/derekovecs/' |
| 168 | } |
| 169 | |
| 170 | #' DeReKoVecsCall |
| 171 | #' |
| 172 | #' Call the DeReKoVecs API. |
| 173 | #' |
| 174 | #' @param method The method to call. |
| 175 | #' @param ... The parameters to pass to the method. |
| 176 | #' @return The result of the call. |
| 177 | #' @importFrom httr2 request req_url_path_append req_url_query req_perform resp_body_json |
| 178 | #' |
| 179 | #' @include utils-pipe.R |
Marc Kupietz | f977fa7 | 2023-11-05 18:02:39 +0100 | [diff] [blame] | 180 | #' @export |
Marc Kupietz | bb4f54c | 2023-10-19 21:22:44 +0200 | [diff] [blame] | 181 | #' |
| 182 | derekovecsApiCall <- function(method = "", ...) { |
| 183 | httr2::request(derekovecsServer()) %>% |
| 184 | httr2::req_url_path_append(method) %>% |
| 185 | httr2::req_url_query(...) %>% |
| 186 | httr2::req_perform() %>% |
| 187 | httr2::resp_body_json(simplifyVector = TRUE) |
Marc Kupietz | e981eae | 2023-10-18 09:00:17 +0200 | [diff] [blame] | 188 | } |