From 93da415c035091913ce1d49fbc7c3026f0b982a2 Mon Sep 17 00:00:00 2001 From: UndeadMaelys Date: Thu, 24 Mar 2022 21:47:24 +0100 Subject: [PATCH] added a way to edit, remove, and add words --- add_word.lua | 89 ++++++++++++++--------------- edit_word.lua | 143 +++++++++++++++++++++++++++++++++++++++++++++++ lexicon | 135 ++++++++++++++++++++++++++++---------------- output_sizes.lua | 12 ++-- output_word.lua | 16 ++++-- remove_word.lua | 116 ++++++++++++++++++++++++-------------- search_list.lua | 36 ++++++------ show_list.lua | 4 +- words.lua | 22 ++++---- 9 files changed, 396 insertions(+), 177 deletions(-) create mode 100644 edit_word.lua diff --git a/add_word.lua b/add_word.lua index ced6cf7..a36f304 100644 --- a/add_word.lua +++ b/add_word.lua @@ -1,44 +1,45 @@ -return function(query) - results_table = {} - for i=1, #words do - local exit = true - local all = true - for _, v in pairs(arg) do - if v == "-r" or v == "--raw" then - all = false - word = string.gsub(words[i][1],"%p","") - if string.find(word, query) then - exit = false - break - end - end - if v == "-e" or v == "--english" then - all = false - word = string.gsub(words[i][2],"%p","") - if string.find(word, query) then - exit = false - break - end - end - end - if all == true then - for j=1, #words[i]-1 do - word = string.gsub(words[i][j],"%p","") - if string.find(word, query) then - exit = false - break - end - end - end - if not exit then - local word = {i,words[i]} - table.insert(results_table,word) - end - end - if #results_table ~= 0 then - results_table = adjustTableSizes(results_table) - printOutputTable(results_table) - else - print("no words found for <" .. query .. ">") - end -end \ No newline at end of file +function addWord() + number = tonumber(number) + -- open file + local file = io.open(data_get, "r") + local pos = 0 + + -- get insertion point + pos = file:seek("end",-9) + -- store after + local after = file:read("*all") + + -- store before + file:seek("set") + local before = file:read(pos-1) + + -- done reading + file:close() + + modify = {} + print("insert new phonetics:") + modify[1] = io.read() + print("insert new meaning:") + modify[2] = io.read() + print("insert new type:") + modify[3] = io.read() + print("insert new canon origin:") + modify[4] = io.read() + print("insert new meta origin:") + modify[5] = io.read() + print("insert new notes:") + modify[6] = io.read() + + local file = io.open(data_output, "w+") + file:write(before) + file:write("\ntable.insert(t,{\"") + for i=1, #modify do + if i > 1 then + file:write("\",\"") + end + file:write(modify[i]) + end + file:write("\"})\n") + file:write(after) + file:close() +end diff --git a/edit_word.lua b/edit_word.lua new file mode 100644 index 0000000..4290307 --- /dev/null +++ b/edit_word.lua @@ -0,0 +1,143 @@ +function editWord(number) + number = tonumber(number) + -- determine modifications + local modify_all = true + local reenable_modify_all = false + local mph = false + local mm = false + local mt = false + local mco = false + local mmo = false + local mn = false + + for _, v in pairs(arg) do + if v == "--modify-phonetics" + or v == "-mph" then + modify_all = false + mph = true + end + if v == "--modify-meaning" + or v == "-mm" then + modify_all = false + mm = true + end + if v == "--modify-type" + or v == "-mt" then + modify_all = false + mt = true + end + if v == "--modify-canon-origin" + or v == "-mco" then + modify_all = false + mco = true + end + if v == "--modify-meta-origin" + or v == "-mmo" then + modify_all = false + mmo = true + end + if v == "--modify-notes" + or v == "-mn" then + modify_all = false + mn = true + end + if v == "--modify-all" + or v == "-ma" then + reenable_modify_all = true + end + end + if reenable_modify_all then + modify_all = true + end + -- open file + local file = io.open(data_get, "r") + local text = file:read("*all") + local pos = 0 + + -- move to line + for i=1, number do + local npos = string.find(text,"\n",pos+1) + if npos == nil then break end + pos = npos + end + + file:seek("set",0) + local before = file:read(pos) + file:seek("set",pos) + + -- move to things to modify + pos = string.find(text,"{\"",pos)+1 + file:seek("set",pos) + + modify = {} + for i=1, 6 do + -- find next " + length = string.find(text,"\"",pos+1)-pos + table.insert(modify,file:read(length-1)) + if i < 6 then + pos = string.find(text,"\"",pos+length+1) + end + file:seek("set",pos) + end + file:seek("set",string.find(text,"})",pos+length+1)+1) + local after = file:read("*all") or "" + file:close() + + print("FILE:") + print(" phonetics",modify[1]) + print(" meaning",modify[2]) + print(" type",modify[3]) + print(" canon origin",modify[4]) + print(" meta origin",modify[5]) + print(" notes",modify[6]) + + print("") + if modify_all or mph then + print("insert new phonetics:") + modify[1] = io.read() + end + if modify_all or mm then + print("insert new meaning:") + modify[2] = io.read() + end + if modify_all or mt then + print("insert new type:") + modify[3] = io.read() + end + if modify_all or mco then + print("insert new canon origin:") + modify[4] = io.read() + end + + if modify_all or mmo then + print("insert new meta origin:") + modify[5] = io.read() + end + + if modify_all or mn then + print("insert new notes:") + modify[6] = io.read() + end + + print("NEW:") + print(" phonetics",modify[1]) + print(" meaning",modify[2]) + print(" type",modify[3]) + print(" canon origin",modify[4]) + print(" meta origin",modify[5]) + print(" notes",modify[6]) + + --rewrite with new info + local file = io.open(data_output, "w+") + file:write(before) + file:write("table.insert(t,{\"") + for i=1, #modify do + if i > 1 then + file:write("\",\"") + end + file:write(modify[i]) + end + file:write("\"})") + file:write(after) + file:close() +end diff --git a/lexicon b/lexicon index 60ec69f..d56167b 100755 --- a/lexicon +++ b/lexicon @@ -6,23 +6,27 @@ require "R2H2" require "color" -adjustTableSizes = require "output_sizes" -printOutputTable = require "output_word" +require "output_sizes" +require "output_word" -showList = require "show_list" -searchList = require "search_list" -removeWord = require "remove_word" -addWord = require "add_word" +require "show_list" +require "search_list" +require "remove_word" +require "add_word" +require "edit_word" -words = dofile("words.lua") +data_get = "words.lua" +data_output = "words.lua" + +words = dofile(data_get) for _, v in pairs(arg) do if v == "-h" or v == "--help" or v == "--usage" or v == "--how" - then - please_help = true + then + please_help = true end end @@ -31,38 +35,42 @@ or arg[1] == "help" or arg[1] == "how" or arg[1] == "howdo" then print([[ - -[l]ist - - shows all words in heonian. + +[e]dit + - removes the word from the lexicon [h]elp - shows this message - + +[l]ist + - shows all words in heonian. + +[n]ew + - add new word to the data + +[r]emove + - removes the word from the lexicon + [s]earch - searches all words and shows only those that return to string.find() - + [tr]anscript - transcripts the word to heonian script -Parameters: --a / --all (shows more info on the word, if any) --c / --copy (copies transcription to keyboard) --e / --english (limits the search to the translated words) +General parameters: -h / --help (shows what does the command do) --r / --raw (limits the search to the heonian words) --v / --verbose (shows the transcription process) ]]) return end if arg[1] == "l" or arg[1] == "list" then - if please_help then + if please_help then print([[ - -[l]ist + +[l]ist - shows all words in heonian. - -Parameters: + +Parameters: -h / --help (shows what does the command do) -a / --all (shows more info on the word, if any) ]]) @@ -73,13 +81,13 @@ Parameters: end if arg[1] == "s" or arg[1] == "search" then - if please_help then + if please_help then print([[ - + [s]earch - searches all words and shows only those that return to string.find() - -Parameters: + +Parameters: -h / --help (shows what does the command do) -a / --all (shows more info on the word, if any) -e / --english (limits the search to the translated words) @@ -94,13 +102,13 @@ Parameters: end if arg[1] == "tr" or arg[1] == "transcript" then - if please_help then + if please_help then print([[ - + [tr]anscript - transcripts the word to heonian script - -Parameters: + +Parameters: -h / --help (shows what does the command do) -v / --verbose (shows the transcription process) -c / --copy (copies transcription to keyboard) @@ -114,36 +122,67 @@ end if arg[1] == "r" or arg[1] == "remove" then - if please_help then + if please_help then print([[ - + [r]emove - removes the word from the lexicon - -Parameters: + +Parameters: -h / --help (shows what does the command do) ]]) else - if arg[2] then removeWord(arg[2]) + if arg[2] + and tonumber(arg[2]) <= #words + and tonumber(arg[2]) > 0 then + removeWord(arg[2]) else print("no word to remove") end return end end -if arg[1] == "a" -or arg[1] == "remove" then - if please_help then +if arg[1] == "n" +or arg[1] == "new" then + if please_help then print([[ - -[r]emove - - removes the word from the lexicon - -Parameters: + +[n]ew + - add new word to the data + +Parameters: -h / --help (shows what does the command do) ]]) else - if arg[2] then addWord(...) - else print("no word to add") end + addWord() + end +end + +if arg[1] == "e" +or arg[1] == "edit" then + if please_help then + print([[ + +[e]dit + - removes the word from the lexicon + +Parameters: +-h / --help / (shows what does the command do) +-mph / --modify-phonetics / edit the phonetics field +-mm / --modify-meaning / edit the meaning field +-mt / --modify-type / edit the type field +-mco / --modify-canon-origin / edit the canon origin field +-mmo / --modify-meta-origin / edit the meta origin field +-mn / --modify-notes / edit the notes field +-ma / --modify-all / edit all the fields + +(-ma is enabled by default, disabled if other flags) +]]) + else + if arg[2] + and tonumber(arg[2]) <= #words + and tonumber(arg[2]) > 0 then + editWord(arg[2]) + else print("no word to edit") end return end end diff --git a/output_sizes.lua b/output_sizes.lua index f45361a..eb8cfcc 100644 --- a/output_sizes.lua +++ b/output_sizes.lua @@ -1,11 +1,11 @@ -return function(tbl) +function adjustTableSizes(tbl) local words_max_length = {1,1,1,1,1,1} - + for i=1, #tbl do local this_word = tbl[i][2] for j=1, #this_word-1 do - local length = string.len(this_word[j]) - if length + 1 > words_max_length[j+1] then + local length = string.len(this_word[j]) + if length + 1 > words_max_length[j+1] then words_max_length[j+1] = length + 1 end end @@ -14,7 +14,7 @@ return function(tbl) words_max_length[1] = length + 1 end end - + for i=1, #words do for j=1, #words[i]-1 do if j > 3 then break end @@ -27,4 +27,4 @@ return function(tbl) end end return tbl -end \ No newline at end of file +end diff --git a/output_word.lua b/output_word.lua index cf18724..fc07f69 100644 --- a/output_word.lua +++ b/output_word.lua @@ -1,10 +1,14 @@ -return function(tbl) +function printOutputTable(tbl,all) local show_all = false - - for _, v in pairs(arg) do + + for _, v in pairs(arg) do if v == "-a" or v == "--all" then show_all = true end - end - + end + + if all then + show_all = true + end + for i=1, #tbl do local this_number = tbl[i][1] local this_word = tbl[i][2] @@ -18,7 +22,7 @@ return function(tbl) text = text .. colorTextBackground(COLOR.Black,colorText(COLOR.HighBlue,this_word[1])) text = text .. colorTextBackground(COLOR.Black,colorText(COLOR.Gray,this_word[3])) text = text .. colorTextBackground(COLOR.Black,colorText(COLOR.Green,this_word[2])) - + print(text) if show_all then if this_word[4] ~= "" diff --git a/remove_word.lua b/remove_word.lua index ced6cf7..c2d326c 100644 --- a/remove_word.lua +++ b/remove_word.lua @@ -1,44 +1,76 @@ -return function(query) - results_table = {} - for i=1, #words do - local exit = true - local all = true - for _, v in pairs(arg) do - if v == "-r" or v == "--raw" then - all = false - word = string.gsub(words[i][1],"%p","") - if string.find(word, query) then - exit = false - break - end - end - if v == "-e" or v == "--english" then - all = false - word = string.gsub(words[i][2],"%p","") - if string.find(word, query) then - exit = false - break - end - end - end - if all == true then - for j=1, #words[i]-1 do - word = string.gsub(words[i][j],"%p","") - if string.find(word, query) then - exit = false - break - end - end - end - if not exit then - local word = {i,words[i]} - table.insert(results_table,word) - end +function removeWord(number) + number = tonumber(number) + + local file = io.open(data_get, "r") + local text = file:read("*all") + local pos = 0 + -- move to line + for i=1, number do + local npos = string.find(text,"\n",pos+1) + if npos == nil then + print("last line") + break + end + pos = npos end - if #results_table ~= 0 then - results_table = adjustTableSizes(results_table) - printOutputTable(results_table) - else - print("no words found for <" .. query .. ">") + + -- get before + file:seek("set",0) + local before = file:read(pos-1) + file:seek("set",pos) + + -- move to things to modify + pos = string.find(text,"\n",pos+1) + file:seek("set",pos) + + -- find next section + local next_section = string.find(text,"\"",pos+1) or file:seek("end") + file:seek("set",pos) + + local length = next_section-pos + + -- get after + file:seek("set",pos) + local after = file:read("*all") or "" + + -- done w file + file:close() + + -- get word going to be removed + local results_table = {} + if words[number] then + table.insert(results_table,{number,words[number]}) end -end \ No newline at end of file + results_table = adjustTableSizes(results_table) + print("\nare you sure you want to remove the following entry? (y/N)") + printOutputTable(results_table,true) + print("") + local delete = string.lower(io.read()) + if delete == "y" + or delete == "ye" + or delete == "yes" + then + delete = true + else + delete = false + end + --[[ + print("Before") + print(text) + + print("After:") + print(before) + print(after) + ]] + if delete then + --rewrite with new info + local file = io.open(data_output, "w+") + file:write(before) + file:write("\n") + file:write(after) + file:close() + print("\nentry was deleted.\n") + else + print("\nentry was not deleted.\n") + end +end diff --git a/search_list.lua b/search_list.lua index a9c7b0b..6c79274 100644 --- a/search_list.lua +++ b/search_list.lua @@ -1,7 +1,7 @@ -return function(query) +function searchList(query) results_table = {} local skip_regular = false - for _, v in pairs(arg) do + for _, v in pairs(arg) do if v == "-i" or v == "--id" then skip_regular = true local id = tonumber(query) @@ -9,40 +9,40 @@ return function(query) local word = {id,words[id]} table.insert(results_table,word) end - break + break end end for i=1, #words do if skip_regular then break end local exit = true local all = true - for _, v in pairs(arg) do + for _, v in pairs(arg) do if v == "-r" or v == "--raw" then all = false - word = string.gsub(words[i][1],"%p","") - if string.find(word, query) then + word = string.gsub(words[i][1],"%p","") + if string.find(word, query) then exit = false - break - end + break + end end if v == "-e" or v == "--english" then all = false - word = string.gsub(words[i][2],"%p","") - if string.find(word, query) then + word = string.gsub(words[i][2],"%p","") + if string.find(word, query) then exit = false - break - end + break + end end end if all == true then - for j=1, #words[i]-1 do - word = string.gsub(words[i][j],"%p","") - if string.find(word, query) then + for j=1, #words[i] do + word = string.gsub(words[i][j],"%p","") + if string.find(word, query) then exit = false - break + break end end - end + end if not exit then local word = {i,words[i]} table.insert(results_table,word) @@ -58,4 +58,4 @@ return function(query) print("no words found for query <" .. query .. ">") end end -end \ No newline at end of file +end diff --git a/show_list.lua b/show_list.lua index ceb3553..f08ea53 100644 --- a/show_list.lua +++ b/show_list.lua @@ -1,4 +1,4 @@ -return function() +function showList() results_table = {} for i=1, #words do local word = {i,words[i]} @@ -6,4 +6,4 @@ return function() end results_table = adjustTableSizes(results_table) printOutputTable(results_table) -end \ No newline at end of file +end diff --git a/words.lua b/words.lua index eb0b860..fdb80bf 100644 --- a/words.lua +++ b/words.lua @@ -1,5 +1,5 @@ local t = {} -table.insert(t,{"-el","bringer of","suffix","demonic","ariel","used in demonic names."}) +table.insert(t,{"el","bringer of","suffix","demonic","ariel","used in demonic titles"}) table.insert(t,{"-lae","feed from","suffix","demonic","",""}) table.insert(t,{"-sh","intention of","suffix","umyr intention/power","","used in myrean names."}) table.insert(t,{"a.bae","young","modifier","","",""}) @@ -18,10 +18,9 @@ table.insert(t,{"an.ku","to walk, to experience","verb","","fern's name",""}) table.insert(t,{"an.me","anime","noun","","",""}) table.insert(t,{"ba","you","pronoun","","",""}) table.insert(t,{"ba.bii.ku","to count","verb","","",""}) -table.insert(t,{"ba.ku","to swim","verb","","",""}) +table.insert(t,{"be.ku","to swim","verb","","",""}) table.insert(t,{"ba.lo","hand","noun","","",""}) -table.insert(t,{"ba.pa.ba.pa.ku","to rub, to hug","verb","","",""}) -table.insert(t,{"ba.pa.ku","to hold","verb","","",""}) +table.insert(t,{"ba.pa.ku","to hold, to hug","verb","","",""}) table.insert(t,{"bae","small","modifier","","",""}) table.insert(t,{"bae.la.pa","egg","noun","","",""}) table.insert(t,{"bae.ma.ra","yesterday","noun","","",""}) @@ -31,7 +30,7 @@ table.insert(t,{"be.taf","big","modifier","","",""}) table.insert(t,{"beg","many, big quantity","modifier","","",""}) table.insert(t,{"bi.men","bird","noun","","",""}) table.insert(t,{"bo.ku","to sew","verb","","",""}) -table.insert(t,{"bokuch","clothes, anything sewn","noun","","",""}) +table.insert(t,{"bo.kuch","clothes, anything sewn","noun","","",""}) table.insert(t,{"bu.lu.ku","to kill","verb","","",""}) table.insert(t,{"cha.we","narrow","modifier","","",""}) table.insert(t,{"chib","tongue","noun","","",""}) @@ -123,7 +122,7 @@ table.insert(t,{"kan.ku","to come here","verb","","",""}) table.insert(t,{"kash.ku","to break","verb","","",""}) table.insert(t,{"ki.ku","to say, to chat","verb","","",""}) table.insert(t,{"ku","to be","verb","","","basic verbal form."}) -table.insert(t,{"ku.ku.ku","to embody","noun","","",""}) +table.insert(t,{"ku.ku.ku","to embody","verb","","",""}) table.insert(t,{"kya.ny","long","modifier","","",""}) table.insert(t,{"la.e","spouse","noun","","",""}) table.insert(t,{"la.fen","feather","noun","","",""}) @@ -151,7 +150,7 @@ table.insert(t,{"mi.gu.ra","daytime","noun","","",""}) table.insert(t,{"mi.ku","to put into words","verb","","bincat",""}) table.insert(t,{"mi.la.ta","sky","noun","","",""}) table.insert(t,{"mi.me","past","noun","","",""}) -table.insert(t,{"mi.min.ku","to see","veb","","",""}) +table.insert(t,{"mi.min.ku","to see","verb","","",""}) table.insert(t,{"mi.nya","present","noun","","",""}) table.insert(t,{"mi.ra.ku","to fear","verb","","",""}) table.insert(t,{"mi.sha.ku","to fight","verb","","",""}) @@ -197,8 +196,9 @@ table.insert(t,{"pi.she.ku","to drink","verb","","",""}) table.insert(t,{"po.me","stone","noun","","",""}) table.insert(t,{"po.nya","good","modifier","","",""}) table.insert(t,{"pon.me","because","marker","","",""}) -table.insert(t,{"pu","star","noun","heonian","actually puropu was first",""}) -table.insert(t,{"pu.ro.pu","sol, sun","noun","heonian pu + ro + pu, star among stars","",""}) +table.insert(t,{"pu","sun","noun","heonian","",""}) +table.insert(t,{"bae.pu","star","noun","heonian","",""}) +table.insert(t,{"shi.ro.bae.pu","night sky","noun","heonian shi + ro + bae.pu, star amidst darkness","",""}) table.insert(t,{"pu.ro.ton","yellow","modifier","","",""}) table.insert(t,{"ra.ya","parent","noun","","",""}) table.insert(t,{"re","i","pronoun","","",""}) @@ -292,5 +292,5 @@ table.insert(t,{"yan.kuch.wa","ocean","noun","","",""}) table.insert(t,{"yea.mat","area","noun","","",""}) table.insert(t,{"yesh","adorable","modifier","","",""}) table.insert(t,{"yi.ma","year","noun","","",""}) -table.insert(t,{"yu","denotes topic, emphasis","marker","","","overwrites subject if they are the same. otherwise, goes after subject marker"}) -return t \ No newline at end of file +table.insert(t,{"yu","denotes topic, emphasis","marker","","","overwrites subject marker if they are in the same position, otherwise goes after it"}) +return t