Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial

Slava Zimogorov
Courses Plus Student 41 PointsSmarter search using regular expressions
Hi there.
Now I'm trying to make search of my app a liitle bit smarter (not very sensitive to misspellings).
It sends SQL request to POSTGRE SQL, and if initial query returns empty results, uses POSTGRE regexp search.
At the moment basically it just replaces 2 charachters of initial seach with any-charachter regex symbol in all combinations.
if query[i]=~/[\p{L}]/
for k in 0...query.length
if query[k]=~/[\p{L}]/ && i!=k
if k<i
possible_queries.push(query[0...k]+'[^\s!\n\\,\.`""''?:;<=>_\)\(-]'+query[k+1...i]+'[^\s!\n\\,\.`""''?:;<=>_\)\(- ]'+query[i+1...query.length])
else
possible_queries.push(query[0...i]+'[^\s!\n\\,\.`""''?:;<=>_\)\(-]' +query[i+1...k]+ '[^\s!\n\\,\.`""''?:;<=>_\)\(-]' +query[k+1...query.length])
end
end
end
end
end
output=[]
possible_queries.each do |p|
if where("title ~* '#{p}' or text_for_test~*'#{p}' and state @@ :state", state:'saved').length!=0
output=output+where("title ~* '#{p}'or text_for_test~*'#{p}' and state @@ :state", state:'saved')
end
end
return output.uniq
But, it is obviosly not the best approach. How can I improve it?
1 Answer

Jason Seifer
Treehouse Guest TeacherHi Slava Zimogorov what are you trying to do exactly? Can you give more context with your code?
Slava Zimogorov
Courses Plus Student 41 PointsSlava Zimogorov
Courses Plus Student 41 PointsFor example, there is object in database named "object". With strict search, if user searches for "obkect" (he made a misspelling) he will find nothing. But with regex and this method, after founding nothing for "obkect", app will send regexp request to SQL [\w][\w]kect, [\w]b[\w]ect (and all other combinations, this will capture object).
But. 1) This doesn't catch mispelling with additional letter ("objkect" will find nothing) 2) I want to do number of any-letters in regexp dymanic, depending on query length 3) And I feel unsure is this approach is good idea.
Jason Seifer
Treehouse Guest TeacherJason Seifer
Treehouse Guest TeacherA better approach might be to use a spellcheck library and find the possible misspellings of the words that the user submits then do a single, combined search on all of those.
Slava Zimogorov
Courses Plus Student 41 PointsSlava Zimogorov
Courses Plus Student 41 PointsThanks... any example of such library?