tests/testthat/test-tagging.R

test_that("tagging supports vector, data frame and legacy formats", {
  engine1 <- worker(type = "tag")

  expect_identical(
    tagging("这是一个测试", engine1),
    stats::setNames(c("v", "m", "vn"), c("这是", "一个", "测试"))
  )

  expect_identical(
    tagging("这是一个测试", engine1, format = "data.frame"),
    data.frame(
      term = c("这是", "一个", "测试"),
      tag = c("v", "m", "vn")
    )
  )

  expect_identical(
    tagging("这是一个测试", engine1, format = "legacy"),
    stats::setNames(c("这是", "一个", "测试"), c("v", "m", "vn"))
  )
})

test_that("tagging requires optional arguments to be named", {
  tagger <- worker(type = "tag")
  input <- c("这是一个测试", "再来一次")

  expect_snapshot(
    tagging(input, tagger, "legacy"),
    error = TRUE
  )

  expect_snapshot(
    tagging_batch(input, tagger, "legacy"),
    error = TRUE
  )
})

test_that("tagging supports vector input with batch aggregation", {
  engine1 <- worker(type = "tag")
  input <- c("这是一个测试", "再来一次")

  expect_identical(
    tagging(input, engine1),
    list(
      stats::setNames(c("v", "m", "vn"), c("这是", "一个", "测试")),
      stats::setNames(c("d", "v", "m"), c("再", "来", "一次"))
    )
  )

  expect_identical(
    tagging(input, engine1, batch = "flatten"),
    stats::setNames(c("v", "m", "vn", "d", "v", "m"), c("这是", "一个", "测试", "再", "来", "一次"))
  )

  expect_identical(
    tagging(input, engine1, format = "legacy", batch = "flatten"),
    stats::setNames(c("这是", "一个", "测试", "再", "来", "一次"), c("v", "m", "vn", "d", "v", "m"))
  )

  expect_identical(
    tagging(input, engine1, format = "data.frame", batch = "flatten"),
    data.frame(
      doc_id = c(1L, 1L, 1L, 2L, 2L, 2L),
      term = c("这是", "一个", "测试", "再", "来", "一次"),
      tag = c("v", "m", "vn", "d", "v", "m")
    )
  )
})

test_that("tagging ignores bylines and still defaults to list for vectors", {
  tagger1 <- suppressWarnings(worker(type = "tag", bylines = TRUE))
  tagger2 <- suppressWarnings(worker(type = "tag", bylines = FALSE))
  input <- c("这是一个测试", "再来一次")

  expected <- list(
    stats::setNames(c("v", "m", "vn"), c("这是", "一个", "测试")),
    stats::setNames(c("d", "v", "m"), c("再", "来", "一次"))
  )

  expect_identical(tagging(input, tagger1), expected)
  expect_identical(tagging(input, tagger2), expected)
})

test_that("tagging rejects an invalid batch value", {
  tagger <- worker(type = "tag")
  input <- c("这是一个测试", "再来一次")

  expect_snapshot(
    tagging(input, tagger, batch = "data.frame"),
    error = TRUE
  )
})

test_that("tagging snapshots invalid inputs", {
  segmenter <- worker()
  tagger <- worker(type = "tag")

  expect_snapshot(tagging("测试", segmenter), error = TRUE)
  expect_snapshot(tagging(NA_character_, tagger), error = TRUE)
  expect_snapshot(tagging("测试", tagger, format = "bad"), error = TRUE)
  expect_snapshot(tagging_batch("测试", tagger, batch = "bad"), error = TRUE)
})

test_that("tagging_batch defaults to list output and ignores bylines", {
  tagger <- suppressWarnings(worker(type = "tag", bylines = FALSE))
  input <- c("这是一个测试", "再来一次")

  expect_identical(
    tagging_batch(input, tagger),
    list(
      stats::setNames(c("v", "m", "vn"), c("这是", "一个", "测试")),
      stats::setNames(c("d", "v", "m"), c("再", "来", "一次"))
    )
  )
})

test_that("tagging_batch forwards explicit formats", {
  tagger <- worker(type = "tag")
  input <- c("这是一个测试", "再来一次")

  expect_identical(
    tagging_batch(input, tagger, batch = "flatten"),
    stats::setNames(c("v", "m", "vn", "d", "v", "m"), c("这是", "一个", "测试", "再", "来", "一次"))
  )

  expect_identical(
    tagging_batch(input, tagger, format = "legacy", batch = "flatten"),
    stats::setNames(c("这是", "一个", "测试", "再", "来", "一次"), c("v", "m", "vn", "d", "v", "m"))
  )

  expect_identical(
    tagging_batch(input, tagger, format = "data.frame", batch = "flatten"),
    data.frame(
      doc_id = c(1L, 1L, 1L, 2L, 2L, 2L),
      term = c("这是", "一个", "测试", "再", "来", "一次"),
      tag = c("v", "m", "vn", "d", "v", "m")
    )
  )
})

Try the jiebaRS package in your browser

Any scripts or data that you put into this service are public.

jiebaRS documentation built on Oct. 13, 2026, 5:07 p.m.