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

מתוך חב"דטקסט, מאגר טקסטים חב"דים חופשיים
קפיצה לניווט קפיצה לחיפוש

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

דוגמה:

  1. {{#invoke:גימטריה|גימטריה|מטאטא חדש מנקה טוב}} => 584
  2. {{#invoke:גימטריה|מספר לאותיות|877}} => תתעז
  3. {{#invoke:גימטריה|מספר לאותיות|15}} => טו
  4. {{#invoke:גימטריה|מספר לאותיות|16}} => טז

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

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

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

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

פונקציות "הקודם" ו"הבא":

  1. {{#invoke:גימטריה|הקודם|כב}} => כא
  2. {{#invoke:גימטריה|הקודם|א}} =>
  3. {{#invoke:גימטריה|הבא|כב}} => כג
  4. {{#invoke:גימטריה|הבא|א}} => ב

פונקצית "שנה":

  1. {{#invoke:גימטריה|שנה|ה|תשעד}} => 5774
  2. {{#invoke:גימטריה|שנה|ה'|תשע"ד}} => 5774
  3. {{#invoke:גימטריה|שנה|א|א}} => 1001
  4. {{#invoke:גימטריה|שנה||תש}} => 700

מכפלת גימטריה:

  1. {{#invoke:גימטריה|מכפלת גימטריה|אבא|אמא}} => 168

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)
        local sum = 0
        for l in mw.ustring.gmatch( s, "." ) do sum = sum + ( values[l] or endet[l] or 0) end
        return sum
end

local kefel_gimatria = function(s1,s2)
	    return gimatria(s1) * gimatria(s2)
end

local mispar_lotiot = function( num, geresh )
        local res, toadd = '', 0
        while num > 0 do
            if num >= 400 then toadd = 400
            elseif num >= 100 then toadd = num - num % 100
            elseif num >= 10 then toadd = num - num % 10
            else toadd = num end
            res = res .. tab[toadd]
            num = num - toadd 
        end
        res = mw.ustring.gsub( res, 'יה', 'טו' )
        res = mw.ustring.gsub( res, 'יו', 'טז' )
        res = mw.ustring.gsub( res, '(.)(.)$', '%1' .. (geresh or '') .. '%2' )
        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,
    
    ['מכפלת גימטריה'] = function( frame )
        return kefel_gimatria((frame.args[1] or ''), (frame.args[2] or ''))
    end,

    mispar_lotiot = mispar_lotiot,
    ['מספר לאותיות'] = function( frame )
        return mispar_lotiot( (tonumber(frame.args[1]) or 0),  ( frame.args[2] or '' ))
    end,
    
    haba = haba,
    ['הבא'] = function( frame )
        local args = frame.args;
        if args[1] == nil then
            local parent = frame:getParent();
            args = parent.args;
        end
        return haba(args[1] or '')
    end,
    
    haqodem = haqodem,
    ['הקודם'] = function( frame )
        local args = frame.args;
        if args[1] == nil then
            local parent = frame:getParent();
            args = parent.args;
        end
        return haqodem(args[1] or '')
    end,

    ['שנה'] = function( frame )
        local args = frame.args;
        if args[1] == nil then
            local parent = frame:getParent();
            args = parent.args;
        end
        return gimatria(args[1] or '')*1000 + gimatria(args[2] or '')
    end,
}