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

1  
get("#137") -- split
2  
get("#515") -- splitLines
3  
get("#389") -- mminsert/mmcontains
4  
get("#521") -- asciiTable
5  
get("#522") -- tryCopyFromParents
6  
7  
keywordsText = getSnippet("#2000023")
8  
new = getSnippet("#2000019") -- with partial annotations
9  
old = getSnippet("#2000015") -- with annotations
10  
overwriteAnnotations = true
11  
12  
-- find all keywords
13  
14  
keywords = {}
15  
for _, l in ipairs(splitLines(keywordsText)) do
16  
  _, _, x, kw = l:find("^line ([^:]+): +(.*)")
17  
  if x == nil then
18  
    print("Warning: ignoring line. "..l)
19  
  else
20  
    keywords[kw] = true
21  
  end
22  
end
23  
24  
-- look for them in known annotations
25  
26  
kw2ann = {}
27  
for _, l in ipairs(splitLines(old)) do
28  
  local _, _, ann, s = l:find("^(.*) | (.*)$")
29  
  if ann then
30  
    ann = ann:gsub("^ +", ""):gsub(" +$", "")
31  
    for kw, _ in pairs(keywords) do
32  
      if s:find(kw, 1, true) then
33  
        if not mmcontains(kw2ann, kw, ann) then
34  
          mminsert(kw2ann, kw, ann)
35  
        end
36  
      end
37  
    end
38  
  end
39  
end
40  
41  
uniquekeywords = {}
42  
for kw, l in pairs(kw2ann) do
43  
  print("keyword "..kw.." maps to "..table.concat(l, "/"))
44  
  if #l == 1 then
45  
    uniquekeywords[kw] = l[1]
46  
  end
47  
end
48  
49  
print()
50  
line = 0
51  
newLines, annotations = {}, {}
52  
for _, l in ipairs(splitLines(new)) do
53  
  line = line+1
54  
  local _, _, ann, s = l:find("^(.*) | (.*)$")
55  
  if ann then
56  
    ann = ann:gsub("^ +", ""):gsub(" +$", "")
57  
    if overwriteAnnotations or ann == '' then
58  
      for kw, ann2 in pairs(uniquekeywords) do
59  
        if s:find(kw, 1, true) then
60  
          ann = ann2
61  
          goto found
62  
        end
63  
      end
64  
      ::found::
65  
      if ann == nil then error() end
66  
      if ann ~= '' then
67  
        print("New annotation derived for line "..line..": "..ann)
68  
      end
69  
    end
70  
    table.insert(newLines, s)
71  
    table.insert(annotations, ann)
72  
  end
73  
end
74  
75  
tryCopyFromParents(newLines, annotations)
76  
77  
print()
78  
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: 740 / 176
Referenced in: [show references]