Skip to content
This repository has been archived by the owner on Feb 11, 2024. It is now read-only.

Commit

Permalink
reimplement row min in C ref #20 (#26)
Browse files Browse the repository at this point in the history
* reimplement row min in C

* added registration
  • Loading branch information
schochastics authored Nov 17, 2023
1 parent bc06964 commit 45b0dbd
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 76 deletions.
5 changes: 1 addition & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,5 @@ Imports:
purrr,
quanteda,
Matrix,
utils,
Rcpp
utils
VignetteBuilder: knitr
LinkingTo:
Rcpp
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ S3method(convert,tokens_with_proximity)
S3method(dfm,tokens_with_proximity)
S3method(print,tokens_with_proximity)
export(tokens_proximity)
importFrom(Rcpp,sourceCpp)
importFrom(quanteda,convert)
importFrom(quanteda,dfm)
useDynLib(quanteda.proximity, .registration = TRUE)
useDynLib(quanteda.proximity,row_mins_)
7 changes: 0 additions & 7 deletions R/RcppExports.R

This file was deleted.

27 changes: 18 additions & 9 deletions R/get_dist.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
#' @useDynLib quanteda.proximity row_mins_
row_mins_c <- function(mat) {
.Call("row_mins_", mat, as.integer(nrow(mat)), as.integer(ncol(mat)))
}

.cal_dist <- function(y, poss) {
return(abs(y - poss))
}
Expand All @@ -10,7 +15,7 @@
}
res <- sapply(target_idx, .cal_dist, poss = poss)
if (get_min) {
return(row_mins_cpp(res) + count_from)
return(row_mins_c(res) + count_from)
}
return(res)
}
Expand Down Expand Up @@ -165,16 +170,20 @@ dfm.tokens_with_proximity <- function(x, tolower = TRUE, remove_padding = FALSE,
}, ...) {
x_attrs <- attributes(x)
x_docvars <- quanteda::docvars(x)
type <- types(x)
type <- quanteda::types(x)
temp <- unclass(x)
index <- unlist(temp, use.names = FALSE)
val <- weight_function(unlist(docvars(x, "proximity"), use.names = FALSE))
temp <- Matrix::sparseMatrix(j = index,
p = cumsum(c(1L, lengths(x))) - 1L,
x = val,
dims = c(length(x),
length(type)),
dimnames = list(quanteda::docnames(x), type))
val <- weight_function(unlist(quanteda::docvars(x, "proximity"), use.names = FALSE))
temp <- Matrix::sparseMatrix(
j = index,
p = cumsum(c(1L, lengths(x))) - 1L,
x = val,
dims = c(
length(x),
length(type)
),
dimnames = list(quanteda::docnames(x), type)
)
output <- quanteda::as.dfm(temp)
attributes(output)[["meta"]] <- x_attrs[["meta"]]
if (remove_docvars_proximity) {
Expand Down
1 change: 0 additions & 1 deletion R/quanteda.proximity-package.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
## usethis namespace: start
#' @importFrom Rcpp sourceCpp
#' @useDynLib quanteda.proximity, .registration = TRUE
## usethis namespace: end
NULL
33 changes: 0 additions & 33 deletions src/RcppExports.cpp

This file was deleted.

30 changes: 30 additions & 0 deletions src/row_min.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <R.h>
#include <R_ext/Rdynload.h>
#include <Rinternals.h>

SEXP row_mins_(SEXP mat, SEXP nRows, SEXP nCols) {
int nrows = INTEGER(nRows)[0];
int ncols = INTEGER(nCols)[0];
SEXP mins = PROTECT(allocVector(INTSXP, nrows));
int *pmat = INTEGER(mat);
int *pmins = INTEGER(mins);

for (int i = 0; i < nrows; i++) {
int minVal = pmat[i];
for (int j = 1; j < ncols; j++) {
int currentVal = pmat[i + j * nrows];
if (currentVal < minVal) {
minVal = currentVal;
}
}
pmins[i] = minVal;
}

UNPROTECT(1);
return mins;
}

void R_init_row_mins_(DllInfo *info) {
R_registerRoutines(info, NULL, NULL, NULL, NULL);
R_useDynamicSymbols(info, TRUE);
}
21 changes: 0 additions & 21 deletions src/row_min.cpp

This file was deleted.

0 comments on commit 45b0dbd

Please sign in to comment.