יחידה:גימטריה

ווערסיע פון 19:27, 15 יולי 2025 דורך תנא קמא (שמועס | ביישטייערונגען) (מספר לאותיות function should output ה'תשס"ו for 5766, not תתתתתתתתתתתתתתקסו)
(חילוק) → עלטערע ווערסיע | איצטיגע ווערסיע (חילוק) | נייערע ווערסיע ← (חילוק)

היחידה גימטריה מייצאת מספר פונקציות. שתי העיקריות הן "גימטריה" שמתמירה מחרוזת למספר המייצג את ערך המחרוזת בגימטריה, ו"מספר לאותיות" שעושה את הפעולה ההפוכה.

דוגמה:

  1. {{#invoke:גימטריה|גימטריה|מטאטא חדש מנקה טוב}} => 584
  2. {{#invoke:גימטריה|גימטריה|ה'תשעד}} => 5774
  3. {{#invoke:גימטריה|גימטריה|ה'תשע"ד}} => 5774

"מספר לאותיות" פונקציע:

  1. {{#invoke:גימטריה|מספר לאותיות|877}} => תתעז
  2. {{#invoke:גימטריה|מספר לאותיות|15}} => טו
  3. {{#invoke:גימטריה|מספר לאותיות|16}} => טז
  4. {{#invoke:גימטריה|מספר לאותיות|5786}} => ה'תשפו

ניתן להעביר לפונקציה מספר לאותיות פרמטר שני, בדרך כלל ' או ", (אבל אפשר להעביר מה שרוצים). אם קיים פרמטר כזה, הוא ייתווסף לפני האות האחרונה של המחרוזת המוחזרת:

  1. {{#invoke:גימטריה|מספר לאותיות|773|"}} => תשע"ג

יש לשים לב: אם הפרמטר האחרון כולל רווחים, גם אלו יתווספו למחרוזת:

  1. {{#invoke:גימטריה|מספר לאותיות|773| " }} => תשע " ג

local values = {
    ['א'] = 1,
    ['ב'] = 2,
    ['ג'] = 3,
    ['ד'] = 4,
    ['ה'] = 5,
    ['ו'] = 6,
    ['ז'] = 7,
    ['ח'] = 8,
    ['ט'] = 9,
    ['י'] = 10,
    ['כ'] = 20,
    ['ל'] = 30,
    ['מ'] = 40,
    ['נ'] = 50,
    ['ס'] = 60,
    ['ע'] = 70,
    ['פ'] = 80,
    ['צ'] = 90,
    ['ק'] = 100,
    ['ר'] = 200,
    ['ש'] = 300,
    ['ת'] = 400,
}

local tab = {}
for k, v in pairs( values ) do tab[v] = k end

local endet = {
    ['ך'] = 20,
    ['ם'] = 40,
    ['ן'] = 50,
    ['ף'] = 80,
    ['ץ'] = 90,
}

local gimatria = function(s)
    -- Check for a thousands separator (apostrophe)
    local thousands, rest = s:match("^(.-)'(.*)$")
    if thousands and rest ~= "" then
        local total = 0
        local thousandsValue = 0
        -- Calculate gematria for the thousands part
        for l in mw.ustring.gmatch(thousands, ".") do
            thousandsValue = thousandsValue + (values[l] or endet[l] or 0)
        end
        total = thousandsValue * 1000
        -- Add gematria for the rest of the number (ignoring extra apostrophes)
        for l in mw.ustring.gmatch(rest, ".") do
            total = total + (values[l] or endet[l] or 0)
        end
        return total
    else
        local total = 0
        for l in mw.ustring.gmatch(s, ".") do
            total = total + (values[l] or endet[l] or 0)
        end
        return total
    end
end

local mispar_lotiot = function(num, geresh)
    local res = ''

    -- Handle thousands
    local thousands = math.floor(num / 1000)
    local remainder = num % 1000

    if thousands > 0 then
        res = res .. tab[thousands] .. "'"
    end

    local n = remainder
    while n > 0 do
        local toadd = 0
        if n >= 400 then
            toadd = 400
        elseif n >= 100 then
            toadd = n - n % 100
        elseif n >= 10 then
            toadd = n - n % 10
        else
            toadd = n
        end
        res = res .. tab[toadd]
        n = n - toadd
    end

    -- Special case for 15 and 16
    res = mw.ustring.gsub(res, 'יה', 'טו')
    res = mw.ustring.gsub(res, 'יו', 'טז')

    -- Add geresh or gershayim if needed
    if mw.ustring.len(res) > 1 then
        res = mw.ustring.gsub(res, '(.)(.)$', '%1' .. (geresh or '"') .. '%2')
    else
        res = res .. (geresh or "'")
    end

    return res
end

local haba = function(s)
    return mispar_lotiot(gimatria(s)+1, '')
end

local haqodem = function(s)
    return mispar_lotiot(gimatria(s)-1, '')
end

return {
    gimatria = gimatria,
    ['גימטריה'] = function( frame )
        return gimatria(frame.args[1] or '')
    end,
    
    mispar_lotiot = mispar_lotiot,
    ['מספר לאותיות'] = function( frame )
        return mispar_lotiot( (tonumber(frame.args[1]) or 0),  ( frame.args[2] or '' ))
    end,
}