## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  dev = "ragg_png"
)

## -----------------------------------------------------------------------------
library(sumer)

## -----------------------------------------------------------------------------
info("lugal")

## -----------------------------------------------------------------------------
info("d-en-lil2")

## -----------------------------------------------------------------------------
# Transliteration -> Cuneiform
as.cuneiform("lugal-e")
as.cuneiform(c("d-en-lil2", "an-ki"))

# Transliteration -> Sign names
as.sign_name("lugal-e")
as.sign_name(c("d-en-lil2", "an-ki"))

## -----------------------------------------------------------------------------
dic <- read_dictionary()

## -----------------------------------------------------------------------------
look_up("lugal", dic)

## -----------------------------------------------------------------------------
look_up("d-suen", dic)

## -----------------------------------------------------------------------------
look_up("Agga", dic, "en")

## -----------------------------------------------------------------------------
look_up("an", dic)

## ----echo = FALSE-------------------------------------------------------------
op_df <- data.frame(
  Notation    = c("Sx->V", "xS->S", "Sx->A", "Sx->S", "xV->V", "Vx->V", "SSx->V"),
  Alternative = c("S\u2612\u2192V", "\u2612S\u2192S", "S\u2612\u2192A", "S\u2612\u2192S",
                   "\u2612V\u2192V", "V\u2612\u2192V", "SS\u2612\u2192V"),
  Meaning     = c("Takes an S to the left, produces a V",
                   "Takes an S to the right, produces an S",
                   "Takes an S to the left, produces an A",
                   "Takes an S to the left, produces an S",
                   "Takes a V to the right, produces a V",
                   "Takes a V to the left, produces a V",
                   "Takes two S to the left, produces a V")
)
knitr::kable(op_df, col.names = c("Notation", "Alternative notation", "Meaning"))

## ----echo = FALSE-------------------------------------------------------------
ex_df <- data.frame(
  Syllable  = c("un", "ma", "gi"),
  Sign      = c("\U00012327", "\U00012220", "\U00012100"),
  Type      = c("\u2612S\u2192S", "S", "S\u2612\u2192S"),
  Translation = c("community of S", "container", "the permanent S")
)
knitr::kable(ex_df, col.names = c("Syllable", "Cuneiform Sign", "Type", "Translation"))

## ----echo = FALSE-------------------------------------------------------------
vt_df <- data.frame(
  Syllable   = c("gan", "ig", "la"),
  Sign       = c("\U000120f6", "\U00012145", "\U000121b7"),
  Type      = c("\u2612V\u2192V", "\u2612V\u2192V", "Vt"),
  Translation = c("may V",
                   "V with the task of establishing sustenance of human existence",
                   "to equip S")
)
knitr::kable(vt_df, col.names = c("Syllable", "Cuneiform Sign", "Type", "Translation"))

## -----------------------------------------------------------------------------
path <- system.file("extdata", "project", "enki_and_the_world_order.txt", package = "sumer")
text <- readLines(path, encoding = "UTF-8")

## -----------------------------------------------------------------------------
freq <- ngram_frequencies(text, min_freq = c(6, 4, 2))
head(freq, 10)

## -----------------------------------------------------------------------------
text_marked <- mark_ngrams(text, freq)
cat(text_marked[1:5], sep = "\n")

## -----------------------------------------------------------------------------
term    <- "IGI.DIB.TU"
pattern <- mark_ngrams(term, freq)
pattern
result  <- text_marked[grepl(pattern, text_marked, fixed = TRUE)]
cat(result, sep = "\n")

## -----------------------------------------------------------------------------
sg  <- sign_grammar("a-ma-ru ba-ur3 ra ta", dic)

## -----------------------------------------------------------------------------
gp <- grammar_probs(sg, dic)

## ----fig.width = 7, fig.height = 4--------------------------------------------
plot_sign_grammar(gp, sign_names = TRUE)

## ----eval = FALSE-------------------------------------------------------------
# plot_sign_grammar(gp, output_file = "grammar.png")

## -----------------------------------------------------------------------------
x <- "mec3-ki-aj2-ga-ce-er-ce-du"
x <- paste0(info(x)$reading, collapse = "-")
x
expr <- split_sumerian(x)$signs
type <- c("S", "S", "Sx->A", "xS->A", "S", "Sx->S", "S", "Sx->V")

grammatical_structure(x, type, expr)

## ----echo = FALSE, out.width = "100%"-----------------------------------------
img_path <- system.file("extdata", "grammatical_structure.png", package = "sumer")
if (file.exists(img_path)) knitr::include_graphics(img_path)

## ----echo = FALSE-------------------------------------------------------------
cls_df <- data.frame(
  Name = c("0S0", "", "", "", "", "",
           "0S1", "1S0", "0S2", "1S1", "2S0",
           "0A0", "", "",
           "0A1", "",
           "1A0", "", "",
           "0A2", "1A1", "2A0", "",
           "VP", "VS", "SEN", "R"),
  Type = c("S", "", "", "", "", "",
           "☒S→S", "S☒→S", "☒SS→S", "S☒S→S", "SS☒→S",
           "A", "", "",
           "☒S→A", "",
           "S☒→A", "", "",
           "☒SS→A", "S☒S→A", "SS☒→A", "",
           "☒V→V", "V☒→V", "SEN", ""),
  Criteria = c(
    "type S",
    "☒S→S ending in \"of S\"/\"for S\"",
    "V of the form \"to be [noun]\"",
    "☒S→V of the form \"to be [noun] of S\"",
    "A of the form \"who is [noun]\"",
    "S☒→S of the form \"who is [noun]\"",
    "☒S→S not ending in \"of S\"/\"for S\"",
    "S☒→S, not in 0S0, head-changing (changes the head noun)",
    "☒SS→S",
    "S☒S→S not beginning with \"S1\"",
    "SS☒→S not beginning with \"S1\"",
    "type A, not in 0S0",
    "type V, not in 0S0",
    "S☒→S, not in 0S0, used adjectivally (head unchanged)",
    "☒S→A (introduced relative clause)",
    "S☒S→S beginning with \"S1\"",
    "S☒→A (relative clause)",
    "S☒→V or Vt (transitive verb)",
    "SS☒→S beginning with \"S1\"",
    "☒SS→A (introduced complex relative clause)",
    "S☒S→A (for completeness only)",
    "SS☒→A (complex relative clause)",
    "SS☒→V (complex transitive verb)",
    "☒V→V (verb prefix)",
    "V☒→V (verb suffix, for completeness only)",
    "a whole-sentence reading (lexicalised compound)",
    "everything else (not matched above)"),
  Example = c(
    "king", "king of S", "to be king", "to be king of S",
    "who is king", "S, who is king",
    "great one who creates S", "city of S", "life-fire that binds S1 to S2",
    "one who is defended from S1 by S2", "binding S1 to S2",
    "who is good", "to be good", "good S",
    "who relies on S", "S1 who relies on S2",
    "that is defined by S", "to be defined by S", "S1 that is defined by S2",
    "who binds S1 and S2 together", "", "that equips S1 with S2", "",
    "to not V", "", "The king is a catalyst for change", "Which SEN?"),
  check.names = FALSE, stringsAsFactors = FALSE
)
# Render as HTML and give the Type column a little more width via an explicit colgroup
tbl  <- as.character(knitr::kable(cls_df, format = "html"))
colg <- paste0("<colgroup>",
               "<col style=\"width: 8%\" />",
               "<col style=\"width: 15%\" />",
               "<col style=\"width: 51%\" />",
               "<col style=\"width: 26%\" />",
               "</colgroup>")
tbl  <- sub("(<table[^>]*>)", paste0("\\1\n", colg), tbl)
knitr::asis_output(tbl)

## -----------------------------------------------------------------------------
classify("AN.LUGAL", dic)

## -----------------------------------------------------------------------------
sg_cls <- sign_grammar("a-ma-ru ba-ur3 ra ta", dic, unit = "class")
gp_cls <- grammar_probs(sg_cls, dic)

## ----fig.width = 7, fig.height = 4--------------------------------------------
plot_sign_grammar(gp_cls, sign_names = TRUE)

## ----eval = FALSE-------------------------------------------------------------
# x <- as.cuneiform("cag4-kalam-ma-gi-hal. hal-la-gin7.")
# result <- translate(x)

## ----echo = FALSE-------------------------------------------------------------
ex_df <- data.frame(
  Syllable  = c("un", "ma", "gi"),
  Sign      = c("\U00012327", "\U00012220", "\U00012100"),
  Type      = c("\u2612S\u2192S", "S", "S\u2612\u2192S"),
  Translation = c("community of S", "container", "the permanent S")
)
knitr::kable(ex_df, col.names = c("Syllable", "Cuneiform Sign", "Type", "Translation"))

## ----eval = FALSE-------------------------------------------------------------
# result <- translate(x)
# writeLines(result, "my_translation.txt")

## ----eval = FALSE-------------------------------------------------------------
# path <- system.file("extdata", package = "sumer")
# 
# file.copy(
#   from = file.path(path, "project"),
#   to   = tempdir(),
#   recursive = TRUE
# )
# 
# ctx <- translation_context(
#   line_folder   = file.path(tempdir(), "project/lines"),
#   text          = file.path(tempdir(), "project/enki_and_the_world_order.txt"),
#   dic           = file.path(path, "sumer-dictionary.txt")
# )
# 
# # Open line 16 to see the full translation of our example
# translate_line(16, ctx)

