Skip to content

Commit

Permalink
finalize tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sborms committed Aug 10, 2018
1 parent 4c37fa8 commit 35d10d2
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 20 deletions.
1 change: 1 addition & 0 deletions R/sentomeasures_methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ summary.sentomeasures <- function(object, ...) {
cat("\n")
cat("Aggregate statistics:", "\n")
print(round(rowMeans(sentomeasures$stats), 5))
NULL
}

#' @export
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
## sentometrics: An Integrated Framework for Textual Sentiment Time Series Aggregation and Prediction

<!--- comment out when submitting to CRAN until CRAN/pandoc issues (e.g. handshake) solved --->
[![Build Status](https://travis-ci.org/sborms/sentometrics.svg?branch=master)](https://travis-ci.org/sborms/sentometrics)
[![CRAN](http://www.r-pkg.org/badges/version/sentometrics)](https://cran.r-project.org/package=sentometrics)
[![Build Status](https://travis-ci.org/sborms/sentometrics.svg?branch=master)](https://travis-ci.org/sborms/sentometrics)
[![codecov](https://codecov.io/github/sborms/sentometrics/branch/master/graphs/badge.svg)](https://codecov.io/github/sborms/sentometrics)
[![Downloads](http://cranlogs.r-pkg.org/badges/sentometrics?color=brightgreen)](http://www.r-pkg.org/pkg/sentometrics)
[![Downloads](http://cranlogs.r-pkg.org/badges/grand-total/sentometrics?color=brightgreen)](http://www.r-pkg.org/pkg/sentometrics)
[![codecov](https://codecov.io/github/sborms/sentometrics/branch/master/graphs/badge.svg)](https://codecov.io/github/sborms/sentometrics)
<!--- [![Pending Pull-Requests](http://githubbadges.herokuapp.com/sborms/sentometrics/pulls.svg?style=flat)](https://github.com/sborms/sentometrics/pulls) --->
<!--- [![Github Issues](http://githubbadges.herokuapp.com/sborms/sentometrics/issues.svg)](https://github.com/sborms/sentometrics/issues) --->

Expand Down
16 changes: 11 additions & 5 deletions tests/testthat/test_aggregation.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,20 @@ corpus <- quanteda::corpus_sample(sento_corpus(corpusdf = usnews), size = 1000)

data("list_lexicons")
lex <- list_lexicons[c("GI_en", "LM_en")]
ctr <- ctr_agg(howWithin = "tf-idf", howDocs = "proportional", howTime = "almon", by = "month",
lag = 3, ordersAlm = 1:3, do.inverseAlm = TRUE)

sentMeas <- sento_measures(corpus, lex, ctr)

### tests from here ###

ctr1 <- ctr_agg(howWithin = "tf-idf", howDocs = "equal_weight", howTime = "almon", by = "month",
lag = 5, ordersAlm = 1:3, do.inverseAlm = TRUE)
sentMeas1 <- sento_measures(corpus, lex, ctr1)

ctr2 <- ctr_agg(howWithin = "tf-idf", howDocs = "proportional", howTime = c("equal_weight", "linear", "own"), by = "year",
lag = 2, weights = data.frame(q1 = c(0.25, 0.75), q3 = c(0.75, 0.25)))
sentMeas2 <- sento_measures(corpus, lex, ctr2)

# sento_measures
test_that("Number of columns coincide with provided dimensions", {
expect_equal(nmeasures(sentMeas), length(sentMeas$features) * length(lex) * length(sentMeas$time))
expect_equal(nmeasures(sentMeas1), length(sentMeas1$features) * length(sentMeas1$lexicons) * length(sentMeas1$time))
expect_equal(nmeasures(sentMeas2), length(sentMeas2$features) * length(sentMeas2$lexicons) * length(sentMeas2$time))
})

10 changes: 8 additions & 2 deletions tests/testthat/test_methods_sentomeasures.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,19 @@ test_that("Scaling is properly done", {
expect_equal(s1$stats["sd", ], s4$stats["sd", ])
})

# plot
# summary.sentomeasures, print.sentomeasures
test_that("No output returned when object summarized or printed", {
expect_null(summary(sentMeas))
expect_null(print(sentMeas))
})

# plot.sentomeasures
p <- plot(sentMeas, group = sample(c("features", "lexicons", "time"), 1))
test_that("Plot is a ggplot object", {
expect_true(inherits(p, "ggplot"))
})

# get_measures (to_long)
# get_measures, to_long
measuresLong <- get_measures(sentMeas, format = "long")
test_that("Proper long formatting of sentiment measures", {
expect_true(nrow(measuresLong) == nobs(sentMeas) * nmeasures(sentMeas))
Expand Down
23 changes: 23 additions & 0 deletions tests/testthat/test_model_specification.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ out8 <- sento_model(sentomeasures, y, x = x, ctr = ctrM8)
ctrM9 <- ctrM8
ctrM9$nSample <- N - 1 - 2 + 1

# sento_model
test_that("Different model specifications give specified output", {
expect_equal(N - 8, nrow(out1$x))
expect_equal(N - 1, nrow(out2$x))
Expand All @@ -73,5 +74,27 @@ test_that("Different model specifications give specified output", {
expect_true(all(sapply(c(list(out1, out2, out3, out4, out5, out7), out8$models),
function(out) stats::coef(out$reg)[c("x1", "x2"), ]) != 0))
expect_error(sento_model(sentomeasures, y, x = x, ctr = ctrM9))
expect_null(summary(out1))
expect_null(summary(out5))
expect_null(summary(out6))
expect_null(summary(out8))
})

# summary.sentomodel, summary.sentomodeliter, print.sentomodel, print.sentomodeliter
test_that("No output returned when object summarized or printed", {
expect_null(summary(out1))
expect_null(summary(out5))
expect_null(summary(out6))
expect_null(summary(out8))
expect_null(print(out1))
expect_null(print(out5))
expect_null(print(out6))
expect_null(print(out8))
})

# plot.sentomodeliter
p <- plot(out8)
test_that("Plot is a ggplot object", {
expect_true(inherits(p, "ggplot"))
})

25 changes: 14 additions & 11 deletions tests/testthat/test_sentiment_computation.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,31 @@ data("usnews")
corpus <- sento_corpus(corpusdf = usnews[1:250, ])

data("list_lexicons")
lex <- setup_lexicons(list_lexicons[c("GI_en", "LM_en", "HENRY_en")],
list_valence_shifters[["en"]])
lex <- setup_lexicons(list_lexicons[c("GI_en", "LM_en", "HENRY_en")], list_valence_shifters[["en"]])
lexSplit <- setup_lexicons(list_lexicons[c("GI_en", "LM_en", "HENRY_en")], list_valence_shifters[["en"]], do.split = TRUE)

### tests from here ###

sentiment <- list(
sentimentList <- list(
s1 = compute_sentiment(quanteda::texts(corpus), lex, how = "counts"),
s2 = compute_sentiment(quanteda::texts(corpus), lex, how = "tf-idf"),
s3 = compute_sentiment(quanteda::texts(corpus), lex, how = "proportional"),
s4 = compute_sentiment(quanteda::texts(corpus), lex, how = "proportionalPol"),
s5 = compute_sentiment(quanteda::corpus(usnews[1:250, "texts"]), lex, how = "counts"),
s6 = compute_sentiment(quanteda::corpus(usnews[1:250, c("texts", "wsj", "economy")], text_field = "texts"),
lex, how = "counts"),
s7 = compute_sentiment(corpus, lex, how = "counts")
s7 = compute_sentiment(corpus, lex, how = "counts"),
s8 = compute_sentiment(quanteda::texts(corpus), lexSplit, how = "counts")
)
test_that("Agreement between sentiment scores across input objects", {
expect_true(all(unlist(lapply(sentiment, function(s) nrow(s[["sentiment"]]) == 250))))
expect_true(all(unlist(lapply(sentiment, function(s) all(s[["sentiment"]][["word_count"]]
== sentiment$s1$sentiment$word_count)))))
expect_equivalent(sentiment$s1$sentiment[, c("GI_en", "LM_en", "HENRY_en")],
sentiment$s5$sentiment[, c("GI_en", "LM_en", "HENRY_en")])
expect_equivalent(sentiment$s6$sentiment[, -c(1:2)],
sentiment$s7$sentiment[, colnames(sentiment$s6$sentiment)[-c(1:2)], with = FALSE])
expect_true(all(unlist(lapply(sentimentList, function(s) nrow(s$sentiment) == 250))))
expect_true(all(unlist(lapply(sentimentList, function(s) all(s$sentiment$word_count
== sentimentList$s1$sentiment$word_count)))))
expect_equivalent(sentimentList$s1$sentiment[, c("GI_en", "LM_en", "HENRY_en")],
sentimentList$s5$sentiment[, c("GI_en", "LM_en", "HENRY_en")])
expect_equivalent(sentimentList$s6$sentiment[, -c(1:2)],
sentimentList$s7$sentiment[, colnames(sentimentList$s6$sentiment)[-c(1:2)], with = FALSE])
expect_true(all(sentimentList$s8$sentiment[, c("GI_en_POS", "LM_en_POS", "HENRY_en_POS")] >= 0))
expect_true(all(sentimentList$s8$sentiment[, c("GI_en_NEG", "LM_en_NEG", "HENRY_en_NEG")] <= 0))
})

0 comments on commit 35d10d2

Please sign in to comment.