Skip to contents

cjk_char_counts() returns one row per distinct CJK character in a text column, with the script and Unicode block it belongs to and how often it occurred. It is the frequency table you would otherwise write by hand before every CJK analysis.

Usage

cjk_char_counts(data, col)

Arguments

data

A data frame or tibble containing a text column.

col

The text column to scan, supplied unquoted. A non-character column is coerced with as.character(); see has_cjk() for why that makes a numeric column a poor thing to measure.

Value

A tibble with one row per distinct character and columns char, codepoint (integer), script, block and n. A column with no CJK in it gives a zero-row tibble with those columns.

Details

Only CJK characters appear; Latin letters, digits and whitespace are dropped. "CJK" means any block listed by cjk_blocks(), so ideographic punctuation and fullwidth forms are included and are labelled as such in script – which makes this the quickest way to find out that a column you thought was clean is full of fullwidth spaces.

Rows are ordered by descending count, and ties are broken by first appearance in the column rather than by the session's collation, so the output does not change with the locale.

See also

cjk_summary() for the column-level figures, cjk_blocks() for the block table these labels come from.

Examples

df <- data.frame(
  text = c("\u4e2d\u6587\u4e2d\u6587",
           "\u65e5\u672c\u306e\u3053\u3068\u3070",
           "ascii only")
)
cjk_char_counts(df, text)
#> # A tibble: 8 × 5
#>   char  codepoint script   block                      n
#>   <chr>     <int> <chr>    <chr>                  <int>
#> 120013 han      CJK Unified Ideographs     2
#> 225991 han      CJK Unified Ideographs     2
#> 326085 han      CJK Unified Ideographs     1
#> 426412 han      CJK Unified Ideographs     1
#> 512398 hiragana Hiragana                   1
#> 612371 hiragana Hiragana                   1
#> 712392 hiragana Hiragana                   1
#> 812400 hiragana Hiragana                   1