From 6c0f8b437d80d1fa6d9bd83619a44980cb535a89 Mon Sep 17 00:00:00 2001 From: Er2 Date: Sun, 27 Feb 2022 13:22:07 +0300 Subject: [PATCH] more reply reactions and match whole words, replace Unicode library --- .gitmodules | 3 ++ etc/utf8 | 1 + etc/utf8.lua | 101 ---------------------------------------- src/events/message.moon | 28 ++++++----- src/parts/core.moon | 6 +++ 5 files changed, 27 insertions(+), 112 deletions(-) create mode 160000 etc/utf8 delete mode 100644 etc/utf8.lua diff --git a/.gitmodules b/.gitmodules index 510857e..588f26a 100644 --- a/.gitmodules +++ b/.gitmodules @@ -2,3 +2,6 @@ path = etc/api url = https://gitdab.com/er2/tg-api-lua +[submodule "etc/utf8"] + path = etc/utf8 + url = https://github.com/Stepets/utf8.lua diff --git a/etc/utf8 b/etc/utf8 new file mode 160000 index 0000000..17f4e00 --- /dev/null +++ b/etc/utf8 @@ -0,0 +1 @@ +Subproject commit 17f4e009a22fb2f2e6ad316a05b2cca8e071fc3b diff --git a/etc/utf8.lua b/etc/utf8.lua deleted file mode 100644 index 6452bcc..0000000 --- a/etc/utf8.lua +++ /dev/null @@ -1,101 +0,0 @@ --- Unicode library - -local pattern = '[%z\1-\127\194-\244][\128-\191]*' - --- helper function -local function posrelat(pos, len) - if pos < 0 then - pos = len + pos + 1 - end - return pos -end - -local utf8 = {} - --- THE MEAT - -function utf8.map(s, f, no_subs) - local i = 0 - - if no_subs then - for b, e in s:gmatch('()' .. pattern .. '()') do - i = i + 1 - local c = e - b - f(i, c, b) - end - else - for b, c in s:gmatch('()(' .. pattern .. ')') do - i = i + 1 - f(i, c, b) - end - end -end - --- THE REST - -function utf8.chars(s, no_subs) - return coroutine.wrap(function() - return utf8.map(s, coroutine.yield, no_subs) - end) -end - -function utf8.len(s) - return select(2, s:gsub('[^\128-\193]', '')) -end - -function utf8.replace(s, map) - return s:gsub(pattern, map) -end - -function utf8.reverse(s) - s = s:gsub(pattern, function(c) - return #c > 1 and c:reverse() - end) - return s:reverse() -end - -function utf8.strip(s) - return s:gsub(pattern, function(c) - return #c > 1 and '' - end) -end - -function utf8.sub(s, i, j) - local l = utf8.len(s) - - i = posrelat(i, l) - j = j and posrelat(j, l) or l - - if i < 1 then i = 1 end - if j > l then j = l end - if i > j then return '' end - - local diff = j - i - local iter = utf8.chars(s, true) - - for _ = 1, i - 1 do iter() end - local c, b = select(2, iter()) - - if diff == 0 - then return s:sub(b, b + c - 1) end - - i = b - for _ = 1, diff - 1 do iter() end - local c, b = select(2, iter()) - - return s:sub(i, b + c - 1) -end - --- ADDITION - -require 'etc.utf8data' - -function utf8.upper(s) - return utf8.replace(s, utf8_lc_uc) -end - -function utf8.lower(s) - return utf8.replace(s, utf8_uc_lc) -end - -return utf8 diff --git a/src/events/message.moon b/src/events/message.moon index 3cf078e..e1df67e 100644 --- a/src/events/message.moon +++ b/src/events/message.moon @@ -1,21 +1,27 @@ reg = { - {'эх', 'хуех'} -- надоели эхать - {'мета', 'хуета'} - {'meta', 'xueta'} - {'цукерберг', 'цукерхуй'} - {'zuckerberg', 'zuckerhui'} - {'whatsapp', 'вадзад'} - {'tiktok', 'деградация'} - {'.*че%?*$', 'пиши ё, грамотей'} - {'.*чё%?*$', 'ничё'} + {'эх+%.*', 'хуех'} -- надоели эхать + {'мета', 'хуета'} + {'meta', 'xueta'} + {'цукерберг', 'цукерхуй'} + {'zuckerberg', 'zuckerhui'} + {'wh?atsapp?', 'вадзад'} + {'в[ао][тсц]+апп?', 'вадзад'} + {'tiktok', 'деградация'} + {'ч[ую]ма', 'капитализм'} + {'минет', 'еблет'} + {'еблет', 'пакет'} + {'да', 'пизда'} + {'нет', 'минет'} + {'че%?*', 'пиши ё, грамотей'} + {'чё%?*', 'ничё'} } (api, msg) => if msg.text - msg.text = utf8.lower msg.text + msg.text = utf8.lower ' '.. msg.text ..' ' t = '' for _, v in pairs reg - if msg.text\match v[1] + if utf8.match msg.text, '%s+'.. v[1] ..'%s+' t ..= "#{v[2]} " api\reply msg, t if t ~= '' diff --git a/src/parts/core.moon b/src/parts/core.moon index 95c084f..0cdbf1b 100644 --- a/src/parts/core.moon +++ b/src/parts/core.moon @@ -36,6 +36,12 @@ Core = @\load 'parts' export utf8 = require 'etc.utf8' + require 'etc.utf8data' + utf8.config = + conversion: + uc_lc: utf8_uc_lc, + lc_uc: utf8_lc_uc + utf8\init! print 'Done!' @\emit 'ready'