Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
58 additions
and
3 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
require "./spec_helper" | ||
|
||
describe TextToSqlSearch do | ||
it "can create config" do | ||
t= TextToSqlSearch::Base.new | ||
c = t.config | ||
c.class.should eq TextToSqlSearch::Config | ||
c.operators_allowed.should eq ["<", ">", "=", ":"] | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
require "./spec_helper" | ||
|
||
describe TextToSqlSearch do | ||
it "can create config" do | ||
t= TextToSqlSearch::Base.new | ||
c = t.config | ||
|
||
tokens= %w(at stage = 7th leg of long ! dreary road) | ||
|
||
t.peek_next(tokens, 0).should eq :todo | ||
t.peek_next(tokens, 1).should eq :operator | ||
t.peek_next(tokens, 6).should eq :negation | ||
t.peek_next(tokens, 6, ["!"]).should eq :todo | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,6 @@ | ||
require "./spec_helper" | ||
|
||
describe TextToSqlSearch do | ||
# TODO: Write tests | ||
|
||
it "works" do | ||
false.should eq(true) | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,37 @@ | ||
require "yaml" | ||
require "./text_to_sql_search/**" | ||
|
||
module TextToSqlSearch | ||
class Config | ||
@operators_allowed = ["<", ">", "=", ":"] | ||
@negation_words = ["!", "no", "not"] | ||
@ignored_words = ["with", "than"] | ||
|
||
def initialize; end | ||
|
||
YAML.mapping( | ||
operators_allowed: {type: Array(String)}, | ||
negation_words: {type: Array(String)}, | ||
ignored_words: {type: Array(String)}, | ||
) | ||
end | ||
|
||
class Base | ||
# Instantiates new, default config. Returns existing config on subsequent calls. | ||
def config | ||
@config||= Config.new | ||
end | ||
|
||
# Given list and current position, peeks into upcoming elements to determine their type. | ||
# All decision-making is done based on current config. | ||
def peek_next( list, i, ignored= config.ignored_words) | ||
while el= list[i+=1]? | ||
next if ignored.includes? el | ||
return :operator if config.operators_allowed.includes? el | ||
return :negation if config.negation_words.includes? el | ||
return :todo | ||
end | ||
end | ||
|
||
end | ||
end |