Skip to content

Commit

Permalink
primeiro rascunho da padronizar_estados()
Browse files Browse the repository at this point in the history
  • Loading branch information
dhersz committed Jan 25, 2024
1 parent 05303af commit eeae19c
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 13 deletions.
7 changes: 4 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,19 @@ Description: Padroniza endereços brasileiros a partir de diferentes
License: MIT + file LICENSE
URL: https://github.com/ipeaGIT/enderecopadrao
BugReports: https://github.com/ipeaGIT/enderecopadrao/issues
Depends:
R (>= 2.10)
Imports:
checkmate,
cli,
rlang,
stringi,
stringr
Suggests:
testthat (>= 3.0.0)
Config/testthat/edition: 3
Encoding: UTF-8
Language: pt
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
Depends:
R (>= 2.10)
LazyData: true
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Generated by roxygen2: do not edit by hand

export(padronizar_ceps)
export(padronizar_estados)
export(padronizar_numeros)
2 changes: 2 additions & 0 deletions R/enderecopadrao.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
#'
#' @keywords internal
"_PACKAGE"

utils::globalVariables("codigos_estados")
75 changes: 75 additions & 0 deletions R/padronizar_estados.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#' Padronizar estados
#'
#' Padroniza um vetor de estados. Veja a seção *Detalhes* para mais informações
#' sobre a padronização.
#'
#' @param estados Um vetor de caracteres ou números. Os estados a serem
#' padronizados.
#'
#' @return Um vetor de caracteres com os estados padronizados.
#'
#' @section Detalhes:
#' TODO: revisar isso aqui
#' Operações realizadas durante a padronização:
#'
#' 1. conversão para caracter, se o input for numérico;
#' 2. remoção de espaços em branco antes e depois dos números e de espaços em
#' branco em excesso entre números;
#' 3. remoção de zeros à esquerda;
#' 4. substituição de números vazios e de variações de SN (SN, S N, S.N., S./N.,
#' etc) por S/N.
#'
#' @examples
#' numeros <- c("0210", "001", "1", "", "S N", "S/N", "SN", "0180 0181")
#' padronizar_numeros(numeros)
#'
#' numeros <- c(210, 1, 10000, NA)
#' padronizar_numeros(numeros)
#'
#' @export
padronizar_estados <- function(estados) {
checkmate::assert(
checkmate::check_character(estados),
checkmate::check_numeric(estados),
combine = "or"
)

# alguns estados podem vir vazios e devem permanecer vazios ao final.
#
# FIXME: esse texto continua relevante?
# nesse caso,
# a chamada da str_pad() abaixo faz com que esses ceps virem '00000000'. para
# evitar que o resultado contenha esses valores, identificamos o indice dos
# ceps vazios para "reesvazia-los" ao final

indice_estado_vazio <- which(estados == "" | is.na(estados))

estados_padrao <- if (is.numeric(estados)) {
formatC(estados, format = "d")
} else {
estados
}

# FIXME: provavelmente dá pra fazer isso aqui só condicionalmente, depois da busca com os vetores
estados_padrao <- stringr::str_squish(estados_padrao)
estados_padrao <- toupper(estados_padrao)
estados_padrao <- stringi::stri_trans_general(estados_padrao, "Latin-ASCII")

vetor_busca_com_cod <- vetor_busca_com_abrev <- codigos_estados$nome_estado
names(vetor_busca_com_cod) <- codigos_estados$codigo_estado
names(vetor_busca_com_abrev) <- codigos_estados$abrev_estado

result_busca_com_cod <- vetor_busca_com_cod[estados_padrao]
result_busca_com_abrev <- vetor_busca_com_abrev[estados_padrao]

estados_padrao <- ifelse(
is.na(result_busca_com_cod),
result_busca_com_abrev,
result_busca_com_cod
)
names(estados_padrao) <- NULL

estados_padrao[indice_estado_vazio] <- ""

return(estados_padrao)
}
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
reference:
- title: "Padronização de campos individuais"
- contents:
- padronizar_estados
- padronizar_ceps
- padronizar_numeros
- title: "Tabelas de códigos"
Expand Down
32 changes: 22 additions & 10 deletions codemeta.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@
],
"softwareRequirements": {
"1": {
"@type": "SoftwareApplication",
"identifier": "R",
"name": "R",
"version": ">= 2.10"
},
"2": {
"@type": "SoftwareApplication",
"identifier": "checkmate",
"name": "checkmate",
Expand All @@ -60,7 +66,7 @@
},
"sameAs": "https://CRAN.R-project.org/package=checkmate"
},
"2": {
"3": {
"@type": "SoftwareApplication",
"identifier": "cli",
"name": "cli",
Expand All @@ -72,7 +78,7 @@
},
"sameAs": "https://CRAN.R-project.org/package=cli"
},
"3": {
"4": {
"@type": "SoftwareApplication",
"identifier": "rlang",
"name": "rlang",
Expand All @@ -84,7 +90,19 @@
},
"sameAs": "https://CRAN.R-project.org/package=rlang"
},
"4": {
"5": {
"@type": "SoftwareApplication",
"identifier": "stringi",
"name": "stringi",
"provider": {
"@id": "https://cran.r-project.org",
"@type": "Organization",
"name": "Comprehensive R Archive Network (CRAN)",
"url": "https://cran.r-project.org"
},
"sameAs": "https://CRAN.R-project.org/package=stringi"
},
"6": {
"@type": "SoftwareApplication",
"identifier": "stringr",
"name": "stringr",
Expand All @@ -96,15 +114,9 @@
},
"sameAs": "https://CRAN.R-project.org/package=stringr"
},
"5": {
"@type": "SoftwareApplication",
"identifier": "R",
"name": "R",
"version": ">= 2.10"
},
"SystemRequirements": null
},
"fileSize": "60.577KB",
"fileSize": "64.462KB",
"readme": "https://github.com/ipeaGIT/enderecopadrao/blob/main/README.md",
"contIntegration": ["https://github.com/ipeaGIT/enderecopadrao/actions?query=workflow%3Acheck", "https://app.codecov.io/gh/ipeaGIT/enderecopadrao?branch=main"],
"developmentStatus": "https://lifecycle.r-lib.org/articles/stages.html"
Expand Down
41 changes: 41 additions & 0 deletions man/padronizar_estados.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit eeae19c

Please sign in to comment.