Skip to content

Commit

Permalink
Merge pull request #14 from lfenzo/fix/street-locale-in-template
Browse files Browse the repository at this point in the history
fix(street): fixing problem with street format locale in impostor templates
  • Loading branch information
lfenzo authored Apr 11, 2024
2 parents 8b5df7e + c46554a commit 2e6668a
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/data/localization/country/pt_BR.csv
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 3 additions & 3 deletions src/data/localization/street_format/en_US.csv
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions src/data/localization/street_format/pt_BR.csv
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
"street_prefix firstname surname"
"street_prefix complete_name"
street_prefix firstname surname
street_prefix complete_name
61 changes: 54 additions & 7 deletions src/providers/localization.jl
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -363,11 +364,16 @@ end

"""
street(n::Integer = 1; kws...)
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[]
Expand All @@ -388,6 +394,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


"""
Expand Down Expand Up @@ -459,7 +506,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
Expand Down
1 change: 1 addition & 0 deletions src/providers/relation_restrictions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const LOCALIZATIONS::Dict{Symbol, Vector} = Dict(
:city,
:district,
:address,
:street,
],
)

Expand Down

0 comments on commit 2e6668a

Please sign in to comment.