ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* [NTG-context] How to implement word corrections
@ 2023-12-28  4:09 Jairo A. del Rio
  0 siblings, 0 replies; only message in thread
From: Jairo A. del Rio @ 2023-12-28  4:09 UTC (permalink / raw)
  To: mailing list for ConTeXt users


[-- Attachment #1.1: Type: text/plain, Size: 502 bytes --]

Hi, list! I'm writing a small module to print word corrections, e.g.,
things like "irregardless" > "regardless"or "alverja" > "arveja" ('pea' in
Spanish) with overstrikes and underlines, but I want feedback to make a
module in the ConTeXt fashion. I hope the PDF attached explains better what
I mean. Just in case, I want to know if anyone else in the mailing list is
acquainted with grammar and orthography quizzes/exercises/exams in ConTeXt.
Merry Christmas and Happy New Year!

Best regards,

Jairo

[-- Attachment #1.2: Type: text/html, Size: 632 bytes --]

[-- Attachment #2: correction_draft.tex --]
[-- Type: application/octet-stream, Size: 3006 bytes --]

\startluacode

  local math, table, utf = math, table, utf
  local max = math.max
  local insert = table.insert
  local len, sub = utf.len, utf.sub
  local interfaces = interfaces
  local constants = interfaces.constants
  local c_command = constants.command

  local implement = interfaces.implement

  local function LCS(s1, s2)
    local m, n = len(s1), len(s2)
    local c = {}
    for i = 0, m do
      c[i] = {}
      for j = 0, n do
        c[i][j] = 0
      end
    end
    for i = 1, m do
      for j = 1, n do
        if sub(s1, i, i) == sub(s2, j, j) then
          c[i][j] = c[i - 1][j - 1] + 1
        else
          c[i][j] = max(c[i - 1][j], c[i][j - 1])
        end
      end
    end
    return c
  end

  local function backtrack(c, s1, s2, i, j)
    if i == 0 or j == 0 then
      return ""
    elseif sub(s1, i, i) == sub(s2, j, j) then
      return backtrack(c, s1, s2, i - 1, j - 1) .. sub(s1, i, i)
    elseif c[i][j - 1] > c[i - 1][j] then
      return backtrack(c, s1, s2, i, j - 1)
    else
      return backtrack(c, s1, s2, i - 1, j)
    end
  end

  local function difftable(s1, s2)
    local c = LCS(s1, s2)
    local lcs = backtrack(c, s1, s2, len(s1), len(s2))

    local result = {}
    local i, j = 1, 1

    local i_, j_
    local aux

    for k = 1, #lcs do
      i_, j_ = i, j
      while sub(s1, i, i) ~= sub(lcs, k, k) do
        i = i + 1
      end
      aux = sub(s1, i_, i-1)

      if #aux > 0 then
        insert(result, {opt = false, data = aux})
      end

      while sub(s2, j, j) ~= sub(lcs, k, k) do
        j = j + 1
      end
      aux = sub(s2, j_, j-1)

      if #aux > 0 then
        insert(result, {opt = true, data = aux})
      end

      insert(result, sub(lcs, k, k))
      i = i + 1
      j = j + 1
    end

    aux = sub(s1, i, len(s1))
    if #aux > 0 then
      insert(result, {opt = false, data = aux})
    end

    aux = sub(s2, j, len(s2))
    if #aux > 0 then
      insert(result, {opt = false, data = aux})
    end

    return result
  end

  implement{
    name = "correction",
    arguments = "2 strings",
    actions = function(s1, s2)
      local diff = difftable(s1, s2) 
      for _, v in ipairs(diff) do
        if type(v) == "string" then
          context(v)
        elseif type(v) == "table" then
          context.correctionparameter(c_command .. (v.opt and "a" or "b"), v.data)
        end
      end
    end
  }
\stopluacode

\unprotect

\installnamespace                          {correction}
\installsimplecommandhandler\????correction{correction}\????correction

\setupcorrection
  [\c!command a=\underbar,
   \c!command b=\overstrike]

\tolerant\protected\def\correction[#1]#:#2#3%
  {\begingroup\setupcorrection[#1]\clf_correction{#2}{#3}\endgroup}

\protect

\starttext

\correction{alverja}{arveja}

\correction{irregardless}{regardless}

\correction{transgiversar}{tergiversar}

\stoptext

[-- Attachment #3: correction_draft.pdf --]
[-- Type: application/pdf, Size: 6551 bytes --]

[-- Attachment #4: Type: text/plain, Size: 511 bytes --]

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
webpage  : https://www.pragma-ade.nl / https://context.aanhet.net (mirror)
archive  : https://github.com/contextgarden/context
wiki     : https://wiki.contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-12-28  4:13 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-12-28  4:09 [NTG-context] How to implement word corrections Jairo A. del Rio

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).