Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

78
LINES

< > BotCompany Repo | #520 // Backport keywords from new case to known case

Lua code

get("#137") -- split
get("#515") -- splitLines
get("#389") -- mminsert/mmcontains
get("#521") -- asciiTable
get("#522") -- tryCopyFromParents

keywordsText = getSnippet("#2000023")
new = getSnippet("#2000019") -- with partial annotations
old = getSnippet("#2000015") -- with annotations
overwriteAnnotations = true

-- find all keywords

keywords = {}
for _, l in ipairs(splitLines(keywordsText)) do
  _, _, x, kw = l:find("^line ([^:]+): +(.*)")
  if x == nil then
    print("Warning: ignoring line. "..l)
  else
    keywords[kw] = true
  end
end

-- look for them in known annotations

kw2ann = {}
for _, l in ipairs(splitLines(old)) do
  local _, _, ann, s = l:find("^(.*) | (.*)$")
  if ann then
    ann = ann:gsub("^ +", ""):gsub(" +$", "")
    for kw, _ in pairs(keywords) do
      if s:find(kw, 1, true) then
        if not mmcontains(kw2ann, kw, ann) then
          mminsert(kw2ann, kw, ann)
        end
      end
    end
  end
end

uniquekeywords = {}
for kw, l in pairs(kw2ann) do
  print("keyword "..kw.." maps to "..table.concat(l, "/"))
  if #l == 1 then
    uniquekeywords[kw] = l[1]
  end
end

print()
line = 0
newLines, annotations = {}, {}
for _, l in ipairs(splitLines(new)) do
  line = line+1
  local _, _, ann, s = l:find("^(.*) | (.*)$")
  if ann then
    ann = ann:gsub("^ +", ""):gsub(" +$", "")
    if overwriteAnnotations or ann == '' then
      for kw, ann2 in pairs(uniquekeywords) do
        if s:find(kw, 1, true) then
          ann = ann2
          goto found
        end
      end
      ::found::
      if ann == nil then error() end
      if ann ~= '' then
        print("New annotation derived for line "..line..": "..ann)
      end
    end
    table.insert(newLines, s)
    table.insert(annotations, ann)
  end
end

tryCopyFromParents(newLines, annotations)

print()
print(mergeTexts(annotations, newLines))

Author comment

Began life as a copy of #512

test run  test run with input  download  show line numbers   

Travelled to 12 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #520
Snippet name: Backport keywords from new case to known case
Eternal ID of this version: #520/1
Text MD5: af924905e452cf8bc826f28109cb123c
Author: stefan
Category:
Type: Lua code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-03-11 19:00:48
Source code size: 1918 bytes / 78 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 796 / 196
Referenced in: [show references]