Skip to content

Instantly share code, notes, and snippets.

@rklancer
Created November 12, 2014 16:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rklancer/6e166033bbe28d96482c to your computer and use it in GitHub Desktop.
Save rklancer/6e166033bbe28d96482c to your computer and use it in GitHub Desktop.
getContext <- function(re, data=sampleData) {
reWithContext <- paste0(".{0,10}", re, ".{0,10}")
context <- data[grep(reWithContext, data$text, perl=T),]
m <- regexpr(reWithContext, context$text, perl=T)
cbind(context, data.frame(context=regmatches(context$text, m)))
}
writeOut <- function(df, fname) {
f <- file(fname, 'wt')
for (rownum in 1:nrow(df)) {
row <- df[rownum, ]
write(paste(row.names(row), row$source, row$context, sep='\t'), f)
}
close(f)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment