| |
|
|
| |
|
|
| source("requirements.R") |
|
|
| OUTPUTDIR_data_wrangling <- here("data_wrangling") |
| |
| if (!dir.exists(OUTPUTDIR_data_wrangling)) { |
| dir.create(OUTPUTDIR_data_wrangling) |
| } |
|
|
| if (!file.exists(here(OUTPUTDIR_data_wrangling, "glottolog_AUTOTYPE_areas.tsv"))) { |
| |
| GB_langs <- |
| read_tsv("data/GB_wide/GB_wide_strict.tsv", col_types = WIDE_COLSPEC) %>% |
| dplyr::select(Language_ID) |
| |
| |
| |
| |
| glottolog_fn <- "data_wrangling/glottolog_cldf_wide_df.tsv" |
| if (!file.exists(glottolog_fn)) { |
| source("generating_GB_input_file.R") |
| } |
| |
| glottolog_df <- read.delim(glottolog_fn , sep = "\t") %>% |
| dplyr::select(Language_ID, Longitude, Latitude) %>% |
| inner_join(GB_langs, by = "Language_ID") |
| |
| |
| |
| AUTOTYP <- |
| read.delim( |
| "https://raw.githubusercontent.com/autotyp/autotyp-data/master/data/csv/Register.csv", |
| sep = "," |
| ) %>% |
| dplyr::select(Language_ID = Glottocode, Area, Longitude, Latitude) %>% |
| group_by(Language_ID, Area) %>% |
| sample_n(1) |
| |
| |
| |
| |
| lgs_with_known_area <- |
| as.matrix(AUTOTYP[!is.na(AUTOTYP$Area), c("Longitude", "Latitude")]) |
| rownames(lgs_with_known_area) <- |
| AUTOTYP[!is.na(AUTOTYP$Area), ]$Language_ID |
| |
| known_areas <- AUTOTYP %>% |
| dplyr::filter(!is.na(Area)) %>% |
| dplyr::select(Language_ID, Area) %>% |
| distinct() %>% |
| dplyr::select(AUTOTYP_Language_ID = Language_ID, everything()) |
| |
| rm(AUTOTYP) |
| |
| lgs_with_unknown_area <- |
| as.matrix(glottolog_df[, c("Longitude", "Latitude")]) |
| rownames(lgs_with_unknown_area) <- glottolog_df$Language_ID |
| |
| |
| atDist <- |
| rdist.earth(lgs_with_known_area, lgs_with_unknown_area, miles = F) |
| |
| rm(lgs_with_known_area, lgs_with_unknown_area) |
| |
| df_matched_up <- |
| as.data.frame(unlist(apply(atDist, 2, function(x) { |
| names(which.min(x)) |
| })), stringsAsFactors = F) %>% |
| rename(AUTOTYP_Language_ID = `unlist(apply(atDist, 2, function(x) { names(which.min(x)) }))`) |
| |
| glottolog_df_with_AUTOTYP <- df_matched_up %>% |
| tibble::rownames_to_column("Language_ID") %>% |
| full_join(known_areas, by = "AUTOTYP_Language_ID") %>% |
| right_join(glottolog_df, by = "Language_ID") %>% |
| dplyr::select(-AUTOTYP_Language_ID) %>% |
| group_by(Language_ID) %>% |
| sample_n(1) %>% |
| rename(AUTOTYP_area = Area) |
| |
| glottolog_df_with_AUTOTYP %>% |
| write_tsv(here(OUTPUTDIR_data_wrangling, "glottolog_AUTOTYPE_areas.tsv")) |
| } |
|
|