使用 R 将国家/地区名称转换为其母语?

问题描述 投票:0回答:2

使用 spData,我可以获得世界国家/地区名称 - 但仅限英文 - 例如德国、中国、摩洛哥。

我想用该国自己的语言获得这些名称 - 例如德国、中国、阿拉伯语 .

我知道我可以从 spData(DE、CN、MA)获取他们的 ISO 代码,但我找不到将这些代码转换为国家/地区名称的软件包在该国家/地区的脚本中

countrycode
这样的包可以使用
countryname("China", destination = 'cldr.short.zh')
将 ISO 代码转换为国家/地区名称 - 但这需要知道中国的语言代码是
zh

从 CN → 中国、DE → 德国等有什么办法吗?

r geospatial country-codes
2个回答
1
投票

使用评论中@zx8754建议的链接,您可以确定适当的目标代码并将其应用到每行。请注意,链接使用

zh_hans
,而 cldr 目标代码使用
yue_hans
指定中国语言,因此必须手动交换。这可以简化,但为了清晰起见,请显示步骤。

library(rvest)
library(dplyr)
library(tidyr)
library(spData)
library(countrycode)

country_lang_url <- "https://wiki.openstreetmap.org/wiki/Nominatim/Country_Codes"

iso2c_lang <- 
  read_html(country_lang_url) %>% 
  html_element(css = "table.wikitable") %>% 
  html_table() %>% 
  separate_wider_delim(cols = 4, delim = ",", names = "lang", too_many = "drop") %>% 
  select(iso2c = 1, lang)

data.frame(iso2c = spData::world$iso_a2) %>% 
  filter(iso2c %in% countrycode::codelist$iso2c) %>% 
  left_join(iso2c_lang, by = join_by(iso2c)) %>% 
  mutate(lang = sub("-", "_", lang)) %>% 
  mutate(lang = if_else(lang == "zh_hans", "yue_hans", lang)) %>% 
  mutate(destination_code = paste0("cldr.name.", lang)) %>% 
  mutate(destination_code = if_else(destination_code %in% cldr_examples$Code, destination_code, "cldr.name.en")) %>% 
  mutate(country_name_en = countrycode(iso2c, "iso2c", "cldr.name.en")) %>% 
  rowwise() %>% 
  mutate(country_name = countrycode(iso2c, "iso2c", destination_code)) %>% 
  filter(country_name_en %in% c("Germany", "China", "Armenia", "Sri Lanka", "Morocco", "Russia", "Thailand", "United States"))
#> # A tibble: 8 × 5
#> # Rowwise: 
#>   iso2c lang     destination_code   country_name_en country_name  
#>   <chr> <chr>    <chr>              <chr>           <chr>         
#> 1 US    en       cldr.name.en       United States   United States 
#> 2 RU    ru       cldr.name.ru       Russia          Россия        
#> 3 TH    th       cldr.name.th       Thailand        ไทย           
#> 4 AM    hy       cldr.name.hy       Armenia         Հայաստան      
#> 5 DE    de       cldr.name.de       Germany         Deutschland   
#> 6 LK    si       cldr.name.si       Sri Lanka       ශ්‍රී ලංකාව      
#> 7 CN    yue_hans cldr.name.yue_hans China           中华人民共和国
#> 8 MA    ar       cldr.name.ar       Morocco         المغرب

0
投票

您可以通过API使用维基数据数据库。 WikidataR 包对此非常有帮助。

以下示例:

  • get_native_name("Belgium")
    将返回“Belgi딓Belgique”“Belgien”
  • get_native_name("Poland")
    将返回“Polska”“Польшча”
  • get_native_name("China")
    将返回“中华人民共和国”
#' get_native_name
#' 
#' find name of a country in country's original language(s) based on Wikidata
#'
#' @param name character : the name of the country
#' @param name language character : the current language of the name of the country
#' @param name limit integer : max number of items returned by wikidata
#'
#' @import WikidataR
#' @return list of names

get_native_name <- function(name, language = "en", limit = 10) {
  # getting all Wikidata items matching the name
  lookup <- WikidataR::find_item(name, language, limit)
  # getting ids of those items
  id_list <- sapply(lookup, function(x) x$id)
  # initialising name list
  names_ <- c()
  # looping through id list
  for(id_ in id_list) {
    i_ <- WikidataR::get_item(id_)
    # checking if item is a country ('P31' property set to 'Q6256')
    if("Q6256" %in% i_[[1]]$claims[["P31"]]$mainsnak$datavalue$value$id) {
      # get the property "name in original language" if 'P1705' is filled
      on_ <- i_[[1]]$claims[["P1705"]]$mainsnak$datavalue$value$id
      if(!is.null(on_))  names_<- c(names_, on_) else {
        # getting official language(s)
        l_ <- i_[[1]]$claims[["P37"]]$mainsnak$datavalue$value$id
        # getting current name of the country for each official language
        for(lo_id_ in l_) {
          # getting iso denomination of the language (using iso-639-1 - 'P218')
          iso_ <- WikidataR::get_item(lo_id_)[[1]]$claims[["P218"]]$mainsnak$datavalue$value
          # adding name to the list 
          if(!is.null(iso_)) names_<- c(names_, i_[[1]]$labels[[iso_]]$value)
        }
      }  
    }
  }
  return(names_)
}
© www.soinside.com 2019 - 2024. All rights reserved.