# blumenbach-skulls.R
#
# Builds the data tables for the post "Blumenbach's Skull Collection, Transcribed"
# (teaching/blumenbach/index.qmd) from the transcription of the appendix
# "Index of the Author's Anthropological Materials" (Skulls of Different Nations,
# items 1-82) in The Anthropological Treatises of Johann Friedrich Blumenbach,
# ed. T. Bendyshe, 1865, pp. 155-158 (= PDF pp. 174-177 of data/Blumenbach-en.pdf).
#
# Transcription was made visually from page images of the Google Books scan and
# cross-checked against its OCR text at archive.org/details/anthropological00bendgoog.
#
# Outputs (teaching/blumenbach/data/):
#   blumenbach-skull-entries.rds : one row per printed catalogue entry (66 rows;
#     comma-joined number runs such as "72-74. Female Turk" kept as single entries)
#   blumenbach-skull-samples.rds : one row per skull (82 rows; number runs expanded)
#
# Run from the repository root:  Rscript teaching/blumenbach/blumenbach-skulls.R

library(tidyverse)

# ---------------------------------------------------------------------------
# One row per printed catalogue entry
# ---------------------------------------------------------------------------
# number = printed number or number run; race_stated = Blumenbach's own label
# (Nos. 1-28); race_inferred = placement assigned for Nos. 29-82 using only the
# categories used above that number, with the anchor for each inference in
# inference_basis ("judgment:" marks placements anchored in my reading rather
# than the book's own statements).

blumenbach_skull_entries <- tribble(
  ~number, ~n, ~specimen, ~gender, ~location, ~age_category, ~source, ~decanas_reference, ~race_stated, ~race_inferred, ~inference_basis,
  "1", 1L, "A Georgian woman", "female", NA, NA, "gift of de Asch", "Dec. cranior. illustr. III. Tab. XXI.; Pl. III. Fig. 2, Pl. IV. Fig. 3", "Caucasian", NA, NA,
  "2", 1L, "A Reindeer Tungus", NA, NA, NA, "gift of de Asch", "Dec. II. Tab. XVI.; Pl. III. Fig. 1, Pl. IV. Fig. 2", "Mongolian", NA, NA,
  "3", 1L, "A female African of Guinea", "female", "Guinea", NA, "gift of Steph. Jo. Van Geuns, Professor at Utrecht", "Dec. II. Tab. XIX.; Pl. III. Fig. 3, Pl. IV. Fig. 5", "Ethiopian", NA, NA,
  "4", 1L, "A Carib chief from the Isle of St Vincent", "male", "Isle of St Vincent", NA, "gift of Sir Joseph Banks, Bart.", "Dec. I. Tab. x.; Pl. IV. Fig. 2", "American", NA, NA,
  "5", 1L, "An Otaheitan", NA, NA, NA, "from the same", "Dec. III. Tab. XXVI.; Pl. IV. Fig. 4", "Malay", NA, NA,
  "6", 1L, "Natolian of Tocat", NA, "Tocat", NA, "gift of de Asch", NA, "Caucasian", NA, NA,
  "7", 1L, "Chinese or Daürian Tungus", NA, NA, NA, "from the same", "Dec. III. Tab. XXIII.", "Mongolian", NA, NA,
  "8", 1L, "Ethiop.", NA, NA, NA, "from Michael., aulic-counsellor of Hesse-Cassel, and Professor of Marburg", "Dec. I. Pl. 8", "Ethiopian", NA, NA,
  "9", 1L, "Indian of North America", NA, "North America", NA, "from the same", "Dec. I. Tab. IX.", "American", NA, NA,
  "10", 1L, "New Hollander", NA, NA, NA, "from Banks", "Dec. III. Tab. XXVII.", "Malay", NA, NA,
  "11", 1L, "Tartar of Kazan", NA, "Kazan", NA, "gift of de Asch", "Dec. II. Tab. XII.", "Caucasian", NA, NA,
  "12", 1L, "Yacutan", NA, NA, NA, "de Asch", "Dec. II. Tab. XV.", "Mongolian", NA, NA,
  "13", 1L, "Ethiopian", NA, NA, NA, "Sömmerring, aulic-counsellor, and Prof. Mogunt.", NA, "Ethiopian", NA, NA,
  "14", 1L, "Turk", NA, NA, NA, "de Asch", NA, "Caucasian", NA, NA,
  "15", 1L, "Calmuck", NA, NA, NA, "de Asch", "Dec. II. Tab. XIV.", "Mongolian", NA, NA,
  "16", 1L, "Ethiop.", NA, NA, NA, "de Asch", "Dec. II. Tab. XVII.", "Ethiopian", NA, NA,
  "17", 1L, "Jewish girl", "female", NA, "infant", NA, "Dec. III. Tab. XXVIII.", "Caucasian", NA, NA,
  "18", 1L, "Burat girl", "female", NA, "infant", "de Asch", "Dec. III. Tab. XXIX.", "Mongolian", NA, NA,
  "19", 1L, "New-born Ethiop.", NA, NA, "infant", "Billmann, Cassel surgeon", "Dec. III. Tab. XXX.", "Ethiopian", NA, NA,
  "20", 1L, "Skull of a Cossack of the Don", NA, "the Don", NA, "de Asch", "Dec. I. Tab. IV.", "between Caucasian and Mongolian", NA, NA,
  "21", 1L, "Kirgis-Cossack", NA, NA, NA, "de Asch", "Dec. II. Tab. XIII.", "between Caucasian and Mongolian", NA, NA,
  "22", 1L, "Another of the same", NA, NA, NA, "de Asch", NA, "between Caucasian and Mongolian", NA, NA,
  "23", 1L, "Egyptian mummy", NA, "Egypt", NA, NA, "Dec. I. Tab. I.", "between Caucasian and Ethiopian", NA, NA,
  "24", 1L, "Genuine Zingari", NA, NA, NA, "Pataki, physician of Claudinopolis", "Dec. II. Tab. II.", "between Caucasian and Ethiopian", NA, NA,
  "25–26", 2L, "Esquimaux", NA, NA, NA, "Jo. Loretz", "Dec. III. Tabb. XXIV. XXV.", "between Mongolian and American", NA, NA,
  "27", 1L, "Macrocephalic, probably Tartar", NA, NA, NA, "de Asch", "Dec. I. Tab. III.", NA, NA, "deformed skull; variety not stated",
  "28", 1L, "Carib female", "female", NA, NA, "Banks", "Dec. III. Tab. XX.", "American", NA, NA,
  "29", 1L, "German", NA, NA, NA, NA, NA, NA, "Caucasian", "European people",
  "30", 1L, "Female German", "female", NA, NA, NA, NA, NA, "Caucasian", "European people",
  "31", 1L, "Young Jew", NA, NA, "youth", NA, NA, NA, "Caucasian", "European people",
  "32", 1L, "Old Jew", NA, NA, "old", NA, NA, NA, "Caucasian", "European people",
  "33", 1L, "Dutch", NA, NA, NA, "Wolff, Utrecht physician", NA, NA, "Caucasian", "European people",
  "34", 1L, "Frenchman", "male", NA, NA, "Sömmerring", NA, NA, "Caucasian", "European people",
  "35", 1L, "Italian", NA, NA, NA, "de Asch", NA, NA, "Caucasian", "European people",
  "36", 1L, "Italian, Venetian", NA, NA, NA, "Michaelis, camp-physician of Hanover", NA, NA, "Caucasian", "European people",
  "37", 1L, "Lombard", NA, NA, NA, "Ib. (= Michaelis)", NA, NA, "Caucasian", "European people",
  "38", 1L, "Ancient Roman prætorian soldier", "male", NA, NA, "Card. Steph. Borgia", NA, NA, "Caucasian", "ancient Italians; Blumenbach cites ancient nations as unadmixed Caucasian examples",
  "39", 1L, "Lithuanian of Sarmatia", NA, "Sarmatia", NA, "de Asch", NA, NA, "Caucasian", "European people",
  "40", 1L, "Calvaria of ancient Cimbrian", NA, NA, NA, "Bozenhard, imperial consul general in Denmark", NA, NA, "Caucasian", "ancient northern Europeans; Blumenbach cites ancient nations as unadmixed Caucasian examples",
  "41–42", 2L, "Finn", NA, NA, NA, "de Asch", NA, NA, "Mongolian", "Blumenbach puts 'the Finnish populations of the cold part of Europe, the Lapps, &c.' in the Mongolian variety (p. 266), and excludes 'the Lapps, and the rest of the true Finns' from the Caucasian race (p. 304)",
  "43", 1L, "Female Finn", "female", NA, NA, NA, NA, NA, "Mongolian", "as No. 41–42",
  "44", 1L, "Russian Zingari", NA, NA, NA, NA, NA, NA, "between Caucasian and Ethiopian", "the 'genuine Zingari' (No. 24) are placed between the Caucasian and Ethiopian",
  "45", 1L, "Russian youth", NA, NA, "youth", NA, NA, NA, "Caucasian", "the five-race recapitulation places all Europeans except the Lapps and true Finns in the Caucasian race (p. 304); the footnote to the list (the Ruthenian series, Nos. 45–63, 'always more or less approaches the Mongolian, and is doubtless the product of mixed marriages') describes admixture within a securely Caucasian people, not an intermediate placement",
  "46", 1L, "Russian old man", "male", NA, "old", NA, NA, NA, "Caucasian", "as No. 45",
  "47–51", 5L, "Russians of Muscovy", NA, "Muscovy", NA, NA, NA, NA, "Caucasian", "as No. 45",
  "52", 1L, "Female of Muscovy", "female", "Muscovy", NA, NA, NA, NA, "Caucasian", "as No. 45",
  "53", 1L, "Russian of Swenigorod", NA, "Swenigorod", NA, NA, NA, NA, "Caucasian", "as No. 45",
  "54", 1L, "Old Russian youth", NA, NA, NA, NA, NA, NA, "Caucasian", "as No. 45; age left unclassified ('Old Russian youth', as printed)",
  "55", 1L, "Russian of Wenefski", NA, "Wenefski", NA, NA, NA, NA, "Caucasian", "as No. 45",
  "56", 1L, "Russian of Romanoff", NA, "Romanoff", NA, NA, NA, NA, "Caucasian", "as No. 45; ditto marks resolve to 'Russian of'",
  "57", 1L, "Russian of Ribno", NA, "Ribno", NA, NA, NA, NA, "Caucasian", "as No. 45; ditto marks resolve to 'Russian of'",
  "58", 1L, "Russian of Ribnisci", NA, "Ribnisci", NA, NA, NA, NA, "Caucasian", "as No. 45; ditto marks resolve to 'Russian of'",
  "59", 1L, "Russian of Kostroma", NA, "Kostroma", NA, NA, NA, NA, "Caucasian", "as No. 45; bare place name within the Ruthenian series Nos. 45–63",
  "60", 1L, "Female [Russian] of Krasno", "female", "Krasno", NA, "de Asch", NA, NA, "Caucasian", "as No. 45",
  "61", 1L, "Russian of Nyschenovogorod", NA, "Nyschenovogorod", NA, NA, NA, NA, "Caucasian", "as No. 45",
  "62", 1L, "Russian of Kursk", NA, "Kursk", NA, NA, NA, NA, "Caucasian", "as No. 45; bare place name within the Ruthenian series Nos. 45–63",
  "63", 1L, "Russian of Orlov", NA, "Orlov", NA, NA, NA, NA, "Caucasian", "as No. 45; bare place name within the Ruthenian series Nos. 45–63",
  "64", 1L, "Tartar of Orenburg", NA, "Orenburg", NA, NA, NA, NA, "Caucasian", "the 'genuine Tartars' 'beyond a doubt belong to our first variety' (pp. 268–269), and the recapitulation makes the western Asiatics this side the Obi Caucasian (p. 304)",
  "65", 1L, "Tartar (probably of Kazan)", NA, "Kazan", NA, NA, NA, NA, "Caucasian", "parallel to No. 11, the Kazan Tatar skull Blumenbach used to demonstrate the Caucasian *norma verticalis*; the genuine Tartars are Caucasian per pp. 268–269",
  "66–68", 3L, "Tatars", NA, NA, NA, NA, NA, NA, "Caucasian", "as Nos. 11, 64–65",
  "69", 1L, "Tschuwasch", NA, NA, NA, NA, NA, NA, "Caucasian", "judgment: Chuvash of the Middle Volga, within the Caucasian boundary Blumenbach draws at the Obi (p. 304); the Finnish exception in the region covers the Lapps and 'true Finns' (Nos. 41–43)",
  "70", 1L, "Lesghi", NA, NA, NA, NA, NA, NA, "Caucasian", "mountain people of the Caucasus",
  "71", 1L, "Georgian", NA, NA, NA, NA, NA, NA, "Caucasian", "the Georgian is Blumenbach's type example of the Caucasian variety (No. 1)",
  "72–74", 3L, "Female Turk", "female", NA, NA, NA, NA, NA, "Caucasian", "No. 14 is labelled 'Caucasian. Turk'",
  "75–80", 6L, "Calmucks of Orenburg", NA, "Orenburg", NA, NA, "Dec. I. Tab. v. (No. 76)", NA, "Mongolian", "No. 15 is labelled 'Mongolian. Calmuck'; the Calmuck of Orenburg fœtus in section II is likewise labelled Mongolian",
  "81", 1L, "Creole Ethiopian from New York", NA, "New York", NA, "Michaelis, Marburg", NA, NA, "Ethiopian", "named Ethiopian; 'Creole' signals mixed descent",
  "82", 1L, "Ethiopian of Congo", NA, "Congo", NA, "de Asch", "Dec. II. Tab. XVIII.", NA, "Ethiopian", "named Ethiopian"
)

stopifnot(nrow(blumenbach_skull_entries) == 67, sum(blumenbach_skull_entries$n) == 82)

# ---------------------------------------------------------------------------
# Nation named in the catalogue (locations dropped)
# ---------------------------------------------------------------------------
# Bracketed terms are editorial corrections where the printed text uses a
# race-wide term as a nation or leaves the location implicit:
#   "[American] Indian"  <- "Indian of North America"
#   "[New York] Creole"  <- "Creole Ethiopian from New York"
#   "Guinean"            <- "African of Guinea"
#   "Congolese"          <- "Ethiopian of Congo"
# Entries labelled only by the variety name (e.g. "Ethiop.") get no nation (NA).

nation_lookup <- c(
  "1" = "Georgian", "2" = "Tungus", "3" = "Guinean", "4" = "Carib", "5" = "Otaheitan",
  "6" = "Natolian", "7" = "Tungus (Daürian)", "9" = "[American] Indian",
  "10" = "New Hollander", "11" = "Tartar", "12" = "Yacutan", "14" = "Turk",
  "15" = "Calmuck", "17" = "Jew", "18" = "Burat", "20" = "Cossack",
  "21" = "Kirgis-Cossack", "22" = "Kirgis-Cossack", "23" = "Egyptian", "24" = "Zingari",
  "25–26" = "Esquimaux", "27" = "Tartar", "28" = "Carib",
  "29" = "German", "30" = "German", "31" = "Jew", "32" = "Jew", "33" = "Dutch",
  "34" = "Frenchman", "35" = "Italian", "36" = "Italian", "37" = "Lombard",
  "38" = "Roman", "39" = "Lithuanian", "40" = "Cimbrian",
  "41–42" = "Finn", "43" = "Finn", "44" = "Zingari",
  "45" = "Russian", "46" = "Russian", "47–51" = "Russian", "52" = "Russian",
  "53" = "Russian", "54" = "Russian", "55" = "Russian", "56" = "Russian",
  "57" = "Russian", "58" = "Russian", "59" = "Russian", "60" = "Russian",
  "61" = "Russian", "62" = "Russian", "63" = "Russian",
  "64" = "Tartar", "65" = "Tartar", "66–68" = "Tartar", "69" = "Tschuwasch",
  "70" = "Lesghi", "71" = "Georgian", "72–74" = "Turk", "75–80" = "Calmuck",
  "81" = "[New York] Creole", "82" = "Congolese"
)

blumenbach_skull_entries <- blumenbach_skull_entries |>
  mutate(nation = nation_lookup[number]) |>
  relocate(nation, .after = specimen)

# ---------------------------------------------------------------------------
# Expand number runs: one row per skull (82 rows)
# ---------------------------------------------------------------------------

expand_number <- function(number) {
  parts <- strsplit(number, ",\\s*")[[1]]
  unlist(lapply(parts, function(p) {
    if (grepl("–", p, fixed = TRUE)) {
      bounds <- strsplit(p, "–", fixed = TRUE)[[1]]
      seq(as.integer(bounds[1]), as.integer(bounds[2]))
    } else {
      as.integer(p)
    }
  }))
}

blumenbach_skulls <- blumenbach_skull_entries |>
  mutate(number_list = lapply(number, expand_number)) |>
  unnest(number_list) |>
  rename(entry = number) |>
  # The Decas citation on the "75-80" entry belongs to skull No. 76 only
  mutate(
    number = number_list,
    decanas_reference = if_else(number == 76, "Dec. I. Tab. v.", decanas_reference)
  ) |>
  relocate(number, entry) |>
  select(-number_list)

stopifnot(nrow(blumenbach_skulls) == 82)

# ---------------------------------------------------------------------------
# Save
# ---------------------------------------------------------------------------

write_rds(blumenbach_skull_entries, "teaching/blumenbach/data/blumenbach-skull-entries.rds")
write_rds(blumenbach_skulls, "teaching/blumenbach/data/blumenbach-skull-samples.rds")
