1
0
mirror of https://github.com/lxsang/antd-web-apps synced 2024-11-20 02:18:20 +01:00
antd-web-apps/blog/api.lua

131 lines
3.8 KiB
Lua
Raw Normal View History

2018-02-23 19:52:10 +01:00
local get = {}
2018-03-07 14:18:25 +01:00
get.fetch = function(user, cnd, limit, order)
2018-02-23 19:52:10 +01:00
local db = require("db.model").get(user,"blogs",nil)
if not db then return nil end
2018-02-27 01:09:20 +01:00
local exp = {}
exp[1] = {["="] = { publish = 1 }}
if cnd then
exp[2] = cnd
else
end
2018-03-07 14:18:25 +01:00
local cond = {
exp = {["and"] = exp },
order = { ctime = "DESC" },
fields = {
"id", "title", "utime", "ctime", "utimestr", "ctimestr", "rendered", "tags"
}
}
2018-02-23 19:52:10 +01:00
if limit then
cond.limit = limit
end
2018-03-07 14:18:25 +01:00
if order then
cond.order = order
end
2018-02-23 19:52:10 +01:00
local data, sort = db:find(cond)
db:close()
return data, sort
end
get.top = function(user, limit)
return get.fetch(user, nil, limit)
end
get.id = function(user, id)
return get.fetch(user, { ["="] = { id = id } }, nil)
end
2018-03-07 14:18:25 +01:00
get.minid = function(user)
local db = require("db.model").get(user,"blogs",nil)
local cond = { fields = { "MIN(id)" } }
local data = db:find(cond)
db:close()
return data[1]["MIN(id)"]
end
get.maxid = function(user)
local db = require("db.model").get(user,"blogs",nil)
local cond = { fields = { "MAX(id)" }}
2018-03-07 15:49:17 +01:00
cond.exp = {["="] = { publish = 1 }}
2018-03-07 14:18:25 +01:00
local data = db:find(cond)
db:close()
return data[1]["MAX(id)"]
end
2018-02-23 19:52:10 +01:00
get.afterof = function(user, id, limit)
2018-03-07 14:18:25 +01:00
local data, sort = get.fetch(user, { [">"] = { id = id } }, limit, { ctime = "ASC" })
table.sort(sort, function(a, b) return a > b end)
return data, sort
2018-02-23 19:52:10 +01:00
end
get.beforeof = function(user, id, limit)
return get.fetch(user, { ["<"] = { id = id } }, limit)
end
get.nextof = function(user, id)
return get.afterof(user, id, 1)
end
get.prevof = function(user, id)
return get.beforeof(user, id, 1)
end
2018-03-07 14:18:25 +01:00
get.bytag = function(user, b64tag, limit, action, id)
LAST_QUERY = b64tag
2018-02-23 19:52:10 +01:00
local tag = bytes.__tostring(std.b64decode(b64tag.."=="))
2018-03-07 14:18:25 +01:00
local cond = { ["LIKE"] = { tags = "%%"..tag.."%%" } }
local order = nil
if action == "before" then
cond = { ["and"] = { cond, { ["<"] = {id = id} } } }
elseif action == "after" then
cond = { ["and"] = { cond, { [">"] = {id = id} } } }
order = { ctime = "ASC" }
end
local data, sort = get.fetch(user, cond, limit, order)
if(action == "after") then
table.sort(sort, function(a, b) return a > b end)
end
return data, sort
2018-02-23 19:52:10 +01:00
end
2018-06-05 16:28:53 +02:00
get.analyse = function(user, n)
if not n then n = 5 end
2018-06-05 14:36:39 +02:00
local path = "/home/mrsang/aiws/blog-clustering"
local gettext = loadfile(path.."/gettext.lua")()
local cluster = loadfile(path.."/cluster.lua")()
local data = gettext.get({publish=1})
local documents = {}
if data then
local sw = gettext.stopwords(path.."/stopwords.txt")
for k,v in pairs(data) do
local bag = cluster.bow(data[k].content, sw)
documents[data[k].id] = bag
end
cluster.tfidf(documents)
--local v = cluster.search("arm", documents)
--echo(JSON.encode(v))
local vectors, maxv, size = cluster.get_vectors(documents)
local sample_data = {pid = 1, sid = 2, score = 0.1}
local db = require("db.model").get(user, "st_similarity", sample_data)
if db then
-- purge the table
db:delete({["="] = {["1"] = 1}})
-- get similarity and put to the table
for id,v in pairs(vectors) do
2018-06-05 16:28:53 +02:00
local top = cluster.top_similarity(id,vectors,n)
2018-06-05 14:36:39 +02:00
for a,b in pairs(top) do
local record = {pid = id, sid = a, score = b}
db:insert(record)
end
end
db:close()
return "<h3>Analyse complete</h3>"
else
return "<h3>Cannot get database objectw/h3>"
end
else
return "<h3>Cannot find data to analyse</h3>"
end
end
2018-02-23 19:52:10 +01:00
2018-06-05 14:36:39 +02:00
return get