當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


R textrecipes step_text_normalization 字符變量的標準化

step_text_normalization() 創建將對字符變量執行 Unicode 規範化的配方步驟的規範。

用法

step_text_normalization(
  recipe,
  ...,
  role = NA,
  trained = FALSE,
  columns = NULL,
  normalization_form = "nfc",
  skip = FALSE,
  id = rand_id("text_normalization")
)

參數

recipe

一個recipe 對象。該步驟將添加到此配方的操作序列中。

...

一個或多個選擇器函數用於選擇受該步驟影響的變量。有關更多詳細信息,請參閱recipes::selections()

role

由於沒有創建新變量,因此此步驟未使用。

trained

指示預處理數量是否已估計的邏輯。

columns

將由 terms 參數(最終)填充的變量名稱字符串。在 recipes::prep.recipe() 訓練該步驟之前,這是 NULL

normalization_form

確定 Unicode 規範化的單個字符串。必須是 "nfc"、"nfd"、"nfkd"、"nfkc" 或 "nfkc_casefold" 之一。默認為"nfc"。有關更多詳細信息,請參閱stringi::stri_trans_nfc()

skip

一個合乎邏輯的。當recipes::bake.recipe() 烘焙食譜時是否應該跳過此步驟?雖然所有操作都是在 recipes::prep.recipe() 運行時烘焙的,但某些操作可能無法對新數據進行(例如處理結果變量)。使用 skip = FALSE 時應小心。

id

該步驟特有的字符串,用於標識它。

recipe 的更新版本,其中新步驟添加到現有步驟(如果有)的序列中。

整理

當您tidy()此步驟時,會出現一個包含列terms(選擇的選擇器或變量)和normalization_form(規範化類型)的小標題。

箱重

底層操作不允許使用案例權重。

也可以看看

step_texthash() 用於特征哈希。

例子

library(recipes)

sample_data <- tibble(text = c("sch\U00f6n", "scho\U0308n"))

rec <- recipe(~., data = sample_data) %>%
  step_text_normalization(text)

prepped <- rec %>%
  prep()

bake(prepped, new_data = NULL, text) %>%
  slice(1:2)
#> # A tibble: 2 × 1
#>   text 
#>   <fct>
#> 1 schön
#> 2 schön

bake(prepped, new_data = NULL) %>%
  slice(2) %>%
  pull(text)
#> [1] schön
#> Levels: schön

tidy(rec, number = 1)
#> # A tibble: 1 × 3
#>   terms normalization_form id                      
#>   <chr> <chr>              <chr>                   
#> 1 text  NA                 text_normalization_j8gw4
tidy(prepped, number = 1)
#> # A tibble: 1 × 3
#>   terms normalization_form id                      
#>   <chr> <chr>              <chr>                   
#> 1 text  nfc                text_normalization_j8gw4

相關用法


注:本文由純淨天空篩選整理自大神的英文原創作品 Normalization of Character Variables。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。