From c48132bdce1d10a790ecb91ae622a5f67974dda1 Mon Sep 17 00:00:00 2001 From: lfenzo Date: Thu, 11 Apr 2024 14:43:11 -0300 Subject: [PATCH 1/2] fix(street): fixing problem with street format locale in impostor templates --- src/data/localization/country/pt_BR.csv | 1 + src/data/localization/street_format/en_US.csv | 6 +-- src/data/localization/street_format/pt_BR.csv | 4 +- src/providers/localization.jl | 52 +++++++++++++++++-- src/providers/relation_restrictions.jl | 1 + 5 files changed, 55 insertions(+), 9 deletions(-) diff --git a/src/data/localization/country/pt_BR.csv b/src/data/localization/country/pt_BR.csv index 1f61152..a5b0878 100644 --- a/src/data/localization/country/pt_BR.csv +++ b/src/data/localization/country/pt_BR.csv @@ -19,3 +19,4 @@ pt_BR,CYM,KY,Ilhas Cayman,Ilhas Cayman pt_BR,COD,CD,Congo,República Democrática do Congo pt_BR,FRA,FR,França,República da França pt_BR,MYS,MY,Malásia,Malásia +pt_BR,USA,US,Estados Unidos,Estados Unidos da América diff --git a/src/data/localization/street_format/en_US.csv b/src/data/localization/street_format/en_US.csv index 4d02b73..05db1a7 100644 --- a/src/data/localization/street_format/en_US.csv +++ b/src/data/localization/street_format/en_US.csv @@ -1,3 +1,3 @@ -"surname street_suffix" -"firstname surname street_suffix" -"street_prefix street_suffix" +surname street_suffix +firstname surname street_suffix +street_prefix street_suffix diff --git a/src/data/localization/street_format/pt_BR.csv b/src/data/localization/street_format/pt_BR.csv index 026e505..7b4eb16 100644 --- a/src/data/localization/street_format/pt_BR.csv +++ b/src/data/localization/street_format/pt_BR.csv @@ -1,2 +1,2 @@ -"street_prefix firstname surname" -"street_prefix complete_name" +street_prefix firstname surname +street_prefix complete_name diff --git a/src/providers/localization.jl b/src/providers/localization.jl index cd78075..9d3e41a 100644 --- a/src/providers/localization.jl +++ b/src/providers/localization.jl @@ -1,12 +1,11 @@ """ - + render_localization_map(; src = nothing, dst = nothing, locale = nothing) -using Base: ocachefile_from_cachefile """ function render_localization_map(; src = nothing, dst = nothing, locale = nothing) locales = _load!("localization", "locale", "noloc") - all_locales = convert.(String, unique(locales[:, :locale])) + all_locales = convert.(String, locales[:, :locale]) df = @chain begin locales @@ -16,6 +15,8 @@ function render_localization_map(; src = nothing, dst = nothing, locale = nothin rightjoin(_load!("localization", "district", all_locales); on = "city") end + df[:, :street] = street(convert.(String, df[:, :country_code])) + if !isnothing(locale) df = filter(r -> r[:locale] in locale, df) end @@ -363,6 +364,8 @@ end """ street(n::Integer = 1; kws...) + street(options::Vector{<:AbstractString}, n::Integer; kws...) + street(mask::Vector{<:AbstractString}; kws...) Generate `n` street names. @@ -388,6 +391,47 @@ function street(n::Integer = 1; locale = session_locale()) return streets |> coerse_string_type end +function street(options::Vector{<:AbstractString}, n::Integer; kws...) + streets = String[] + + locales = _load!("localization", "locale", "noloc") + country_code_locale_map = Dict( + String(row[:country_code]) => String(row[:locale]) for row in eachrow(locales) + ) + + street_formats = Dict( + code => _load!("localization", "street_format", country_code_locale_map[code]) + for code in unique(options) + ) + + for _ in 1:n + country_code = rand(keys(country_code_locale_map)) + format = rand(street_formats[country_code][:, :street_format]) |> String + push!(streets, render_template(format; locale = country_code_locale_map[country_code])) + end + + return streets |> coerse_string_type +end + +function street(mask::Vector{<:AbstractString}; kws...) + streets = String[] + + locales = _load!("localization", "locale", "noloc") + country_code_locale_map = Dict( + String(row[:country_code]) => String(row[:locale]) for row in eachrow(locales) + ) + + street_formats = Dict( + code => _load!("localization", "street_format", country_code_locale_map[code]) + for code in unique(mask) + ) + + for country_code in mask + format = rand(street_formats[country_code][:, :street_format]) |> String + push!(streets, render_template(format; locale = country_code_locale_map[country_code])) + end + return streets |> coerse_string_type +end """ @@ -459,7 +503,7 @@ function address(n::Integer = 1; locale = session_locale()) return addresses |> coerse_string_type end -function address(options::Vector{<:AbstractString}, n::Integer; level::Symbol = :state_cide, kws...) +function address(options::Vector{<:AbstractString}, n::Integer; level::Symbol = :state_code, kws...) addresses = String[] gb = @chain begin diff --git a/src/providers/relation_restrictions.jl b/src/providers/relation_restrictions.jl index a57cad5..34ece9e 100644 --- a/src/providers/relation_restrictions.jl +++ b/src/providers/relation_restrictions.jl @@ -45,6 +45,7 @@ const LOCALIZATIONS::Dict{Symbol, Vector} = Dict( :city, :district, :address, + :street, ], ) From c46554a3e5cdb065028552dbcb632a915ba2ac42 Mon Sep 17 00:00:00 2001 From: lfenzo Date: Thu, 11 Apr 2024 18:49:38 -0300 Subject: [PATCH 2/2] doc(street): small additions for the new street generator functions --- src/providers/localization.jl | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/providers/localization.jl b/src/providers/localization.jl index 9d3e41a..be7ed9d 100644 --- a/src/providers/localization.jl +++ b/src/providers/localization.jl @@ -367,10 +367,13 @@ end street(options::Vector{<:AbstractString}, n::Integer; kws...) street(mask::Vector{<:AbstractString}; kws...) -Generate `n` street names. +Generate `n` street names. Note that for option and mask-based generation the only valid options +to provide are `country_code`s. -# Kwargs -- `locale::Vector{String}`: locale(s) from which entries are sampled. If no `locale` is provided, the current session locale is used. +# Parameters +- `n::Integer = 1`: number of street names entries to generate. +- `options::Vector{<:AbstractString}`: vector with with options restricting the possible values generated. +- `mask::Vector{<:AbstractString}`: mask vector with element-wise option restrictions. """ function street(n::Integer = 1; locale = session_locale()) streets = String[]