## ----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("Enki", 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", dic)

## -----------------------------------------------------------------------------
prior <- prior_probs(dic, sentence_prob = 0.25)

## -----------------------------------------------------------------------------
gp <- grammar_probs(sg, prior, 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)

## ----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"),
#   sentence_prob = 0.25
# )
# 
# # Open line 16 to see the full translation of our example
# translate_line(16, ctx)

