Skip to content

Instantly share code, notes, and snippets.

@nmandery
Created July 13, 2010 15:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nmandery/474039 to your computer and use it in GitHub Desktop.
Save nmandery/474039 to your computer and use it in GitHub Desktop.
#!/usr/bin/lua
--[[
a parser for google-like search queries
]]--
lex_keywords = {
-- keywords with quotes
"([a-zA-Z]+)%s*:%s*[\"\']([^\"\']+)[\"\']",
-- keywords without quotes
"([a-zA-Z]+)%s*:%s*([^\"\'%s]+)",
}
function lex(query)
terms={}
-- match keywords
for i,lx in ipairs(lex_keywords) do
-- match
for kw, q in query:gmatch(lx) do
kw = kw:lower()
-- add it to our terms
if terms[kw] == nil then
terms[kw] = {}
end
table.insert(terms[kw], q)
end
-- remove matched
query = query:gsub(lx, "")
end
terms.query = {query}
return terms
end
function dump_terms(t)
for kw, qs in pairs(t) do
print(kw)
for i, q in pairs(qs) do
print(" <" .. q .. ">")
end
end
end
local t = lex("blaaa inTitle : 'zuu u' inurl:dfdfdf inurl:34 das ist")
dump_terms(t)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment