Learn R Programming

sumer (version 1.3.0)

eval_operator: Evaluate an Operator or Composition

Description

Evaluates a single operator by substituting resolved arguments into its translation string, or composes two elements without an operator using compositional rules.

This function contains the complete translation logic: extraction of specific meanings from {specific} annotations, article insertion ("the "), verb-prefix stripping ("to "), placeholder substitution, and composition rules.

Usage

eval_operator(operator, rule_translation, args = list(), seps = "", trailing_sep = "")

Value

A character vector of length 2: c(result_type, result_translation).

On error (e.g. incompatible types in composition), a character vector of length 1 containing the error message.

Arguments

operator

Type string of the operator (e.g. "Sx->V", "SSx->V", "xS->A"), or NULL for composition (no operator present). For base types without arguments (e.g. "S", "V"), pass the type string here.

rule_translation

Translation string with placeholders (e.g. "to utilize S", "S1 and therefore S2"), or NULL for composition.

args

List of c(name, translation) character vectors:

name

Placeholder name encoding the grammatical type (e.g. "S", "S1", "V", "SEN2"). The type is reconstructed internally by stripping the trailing index digits.

translation

Resolved translation of the argument.

Default: list() (no arguments, for base types). The helper function build_args() can be used to construct this list from a set of elements.

seps

Character vector of separators between elements, as extracted by apply_translation_rules. Hyphens in separators are converted to spaces for composition. Default: "".

trailing_sep

Text after the last element (e.g. trailing punctuation). Hyphens are converted to spaces for composition. Default: "".

Details

The function operates in two modes:

Operator mode (operator is not NULL):

  1. Determine the result type from the operator string via parse_type().

  2. For each argument, apply transformations:

    • Strip "to " from verb-type ("V") arguments.

    • Add "the " before substantive ("S") arguments when the result type is not "S" (i.e., the operator changes the type).

  3. Replace each placeholder in rule_translation by whole-word match.

Composition mode (operator is NULL):

Exactly two arguments are required. The composition rules are:

S + A -> S

Simple juxtaposition: "X Y".

S + S -> S

Without comma separator: "X of/with the Y".

S, S -> S

With comma separator: "X, the Y".

S + V -> SEN

Subject-verb: "X Y" (with "to " stripped from verb).

SEN + SEN -> SEN

Sentence concatenation: "X. Y" (period added if missing).

In both modes, translations of the form "general \{specific\}" are reduced to the specific part before processing.

See Also

apply_translation_rules which calls this function, compose_skeleton_entry for the full pipeline

Examples

Run this code
# --- Base type (operator without arguments) ---
# A simple substantive passes through unchanged:
sumer:::eval_operator("S", "temple")
# [1] "S"      "temple"

# With {specific} extraction:
sumer:::eval_operator("S", "place {earth}")
# [1] "S"     "earth"

# --- Operator with one argument: Sx->V ---
# The S argument gets "the " prepended (since result type is V, not S):
args <- list(c("S", "temple"))
sumer:::eval_operator("Sx->V", "to utilize S", args)
# [1] "V"                      "to utilize the temple"

# --- Operator with two S arguments: SSx->V ---
# Duplicate types get indexed names (S1, S2):
args <- list(c("S1", "leader"), c("S2", "temple"))
sumer:::eval_operator("SSx->V", "to equip S1 with S2", args)
# [1] "V"                                  "to equip the leader with the temple"

# --- Composition: S + A -> S ---
args <- list(c("S", "temple"), c("A", ", which is a great one"))
sumer:::eval_operator(NULL, NULL, args)
# [1] "S"            "temple, which is a great one"

# --- Composition: S + V -> SEN ---
args <- list(c("S", "men"), c("V", "to bring grain into the temple"))
sumer:::eval_operator(NULL, NULL, args)
# [1] "SEN"       "men bring grain into the temple"

# --- Full pipeline with add_brackets and apply_translation_rules ---
x <- "mec3-ki-aj2-ga-ce-er"
x <- as.cuneiform(x)
x

meaning <- rbind( c("S",      "a man who relies on his own strength"),
                  c("S",      "place {earth}"),
                  c("Sx->A",  ", whose allocated resource is S"),
                  c("xS->A",   ", whose sustenance is S"),
                  c("S",      "grain"),
                  c("Sx->S",  "lamented S"))

df <- data.frame(
    type  = meaning[,1],
    translation = meaning[,2],
    expr  = split_sumerian(x)$signs)

s <- x
for(i in 1:nrow(df)){
 s <- sub(df$expr[i], paste0("#", i), s)
}
s

s_bracketed <- sumer:::add_brackets(s, df$type)
s_bracketed

apply_translation_rules(s_bracketed$string, df$type, df$translation)

Run the code above in your browser using DataLab