Created
July 13, 2010 15:34
-
-
Save nmandery/474039 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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