Skip to content

Instantly share code, notes, and snippets.

@kiwina
Forked from kidd/aggregator.lua
Created September 15, 2013 04:09
Show Gist options
  • Save kiwina/6567953 to your computer and use it in GitHub Desktop.
Save kiwina/6567953 to your computer and use it in GitHub Desktop.
local path = ngx.var.request:split(" ")[2] -- path
local t={}
local cjson = require "cjson"
local m = ngx.re.match(path,[=[^/sentence/(.+)]=])
local words = m[1]:split("%%20") -- words in the sentence
for i,k in pairs(words) do
local res_word = ngx.location.capture("/real/".. k )
local value=cjson.new().decode(res_word.body)
table.insert(t, value.sentiment)
end
local final=0
for i,k in pairs(t) do
final=final+k
end
ngx.say(final)
local xml = require("LuaXml")
require("os")
local cjson = require "cjson"
local path = ngx.var.request:split(" ")[2]
local m = ngx.re.match(path,[=[/([^/]+)\.(json|xml)$]=]) -- match last word
local res = ngx.location.capture("/v1/word/".. m[1] .. ".json" )
local value=cjson.new().decode(res.body)
local response = xml.new("response")
response.word= xml.new("word")
response.sentiment = xml.new("sentiment")
response.timestamp = xml.new("timestamp")
table.insert(response.word, value.word)
table.insert(response.sentiment, value.sentiment)
table.insert(response.timestamp, os.date())
ngx.say('<?xml version="1.0" encoding="UTF-8"?>', xml.str(response,0))
local path = ngx.var.request:split(" ")[2] -- path
local t={}
local cjson = require "cjson"
ngx.log(0, path[2])
local m = ngx.re.match(path,[=[^/v1/max/(.+).json]=])
local words = m[1]:split("+") -- words in the sentence
local max = nil
for i,k in pairs(words) do
local res_word = ngx.location.capture("/v1/word/".. k .. ".json" )
local value=cjson.new().decode(res_word.body)
if max == nil or max.sentiment < value.sentiment then
max = value
end
end
ngx.say(cjson.new().encode(max))
--[[
Auxiliary function to split a string based on a delimiter
]]--
function string:split(delimiter)
local result = { }
local from = 1
local delim_from, delim_to = string.find( self, delimiter, from )
while delim_from do
table.insert( result, string.sub( self, from , delim_from-1 ) )
from = delim_to + 1
delim_from, delim_to = string.find( self, delimiter, from )
end
table.insert( result, string.sub( self, from ) )
return result
end
local xml = require("LuaXml")
local cjson = require "cjson"
local path = ngx.var.request:split(" ")[2]
local m = ngx.re.match(path,[=[/([^/]+)\.json]=])
local res = ngx.location.capture("/v1/word/".. m[1] .. ".xml")
local my_xml = xml.eval(res.body)
local sent_val = my_xml:find("sentiment")[1]
local word_val = my_xml:find("word")[1]
local t = {sentiment = sent_val, word = word_val}
local value=cjson.encode(t)
ngx.say(value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment