Permalink
Browse files

refactoring similarity code, adding in additional API services such a…

…s dictionary-based search
  • Loading branch information...
1 parent 7903642 commit f7db063d53895b4c76c745d198966208bf97b722 Addy Osmani committed Jun 9, 2011
View
0 css/handheld.css 100644 → 100755
File mode changed.
View
0 css/style.css 100644 → 100755
File mode changed.
View
0 favicon.ico 100644 → 100755
File mode changed.
View
0 img/.gitignore 100644 → 100755
File mode changed.
View
4 index.html 100644 → 100755
@@ -31,7 +31,7 @@
<form action="/" id="q">
<input name="speechQuery" id="speechQuery" type="text" x-webkit-speech speech error onwebkitspeechchange="speechBrowser.query(true);" />
<input type="submit" name="submitQuery" id="submitQuery" value="Submit" />
- <h3>Supports: hello, how is the weather, what is your name, search [something something], whats your favorite movie</h3>
+ <h3>Supports: hello, how is the weather? (variations), what is your name?, search [something something], whats your favorite movie?, define [something]</h3>
</form>
<div class="search-case">
<div id="search-results"></div>
@@ -63,7 +63,7 @@
s.parentNode.insertBefore(g,s)}(document,"script"));
</script>
-<!--tts-->
+<!--tts, to be replaced with marack node package soon-->
<script type="text/javascript" src="http://chachakawooka.com/js/cckw_tts.js"></script>
<!--weather-->
<script type="text/javascript" src="js/mylibs/jquery.simpleWeather-1.8.min.js"></script>
View
0 js/libs/dd_belatedpng.js 100644 → 100755
File mode changed.
View
0 js/libs/jquery-1.5.1.js 100644 → 100755
File mode changed.
View
0 js/libs/jquery-1.5.1.min.js 100644 → 100755
File mode changed.
View
0 js/libs/modernizr-1.7.min.js 100644 → 100755
File mode changed.
View
0 js/mylibs/.gitignore 100644 → 100755
File mode changed.
View
0 js/mylibs/gsearch.js 100644 → 100755
File mode changed.
View
0 js/mylibs/jquery.simpleWeather-1.8.min.js 100644 → 100755
File mode changed.
View
0 js/mylibs/levens.js 100644 → 100755
File mode changed.
View
78 js/mylibs/speechBrowser.js 100644 → 100755
@@ -78,36 +78,53 @@ var speechBrowser = function(){
$('#search-results').html('');
},
- loadMedia:function(type,url){
+ loadMedia:function(type,src){
switch(type){
case 'image':
- $('#search-results').html("<img src='" + url + "'/>");
+ $('#search-results').html("<img src='" + src + "'/>");
break;
+ case 'dictionary':
+ this.getDictionaryDefinition(src);
+ break;
+ }
+
+ },
+
+ getQueryStartsWith: function(needle, haystack){
+ var inputPhrase = needle,
+ inputTest = haystack.indexOf(inputPhrase),
+ lenInput = needle.length,
+ lenHaystack = haystack.length,
+ result = "";
+
+ if(inputTest !== -1){
+ result = haystack.slice(inputTest + lenInput, haystack.length);
+ //console.log('getqueryendswith:' + result);
}
+ return result;
},
query: function(val){
+ //set qStr = val if you want to test without speaking.
var qStr = speech.val();
this.prepareStage();
console.log(qStr);
if(qStr !== null){
- /*everything here should move to a JSON-dictionary*/
-
- /*detect if the user is attempting to search for something*/
- var searchPhrase="search",searchTest = qStr.indexOf(searchPhrase);
- if(searchTest !== -1){
- console.log('you tried a search');
- /*now let's get everything after search*/
- var searchQuery = qStr.slice(searchTest+searchPhrase.length, qStr.length);
- console.log('you searched for:' + searchQuery);
- this.textToSpeech('i hope these results for' + searchQuery + ' help ');
- $("#search-results").gSearch({search_text : searchQuery ,count:4,pagination:true});
+ var defineTest = $.trim(this.getQueryStartsWith('define',qStr)),
+ searchTest = $.trim(this.getQueryStartsWith('search',qStr));
- }else{
+ console.log('def test:' + defineTest);
+ if(searchTest.length > 0){
+ this.textToSpeech('i hope these results for' + searchTest + ' help ');
+ $("#search-results").gSearch({search_text : searchTest ,count:4,pagination:true});
+ }else if(defineTest.length >0){
+ this.getDictionaryDefinition($.trim(defineTest));
+ }else{
+
/*if not performing a search check for other shortcuts supported*/
/*all of this logic is currently very dumb. we need proper NLP*/
@@ -134,6 +151,7 @@ var speechBrowser = function(){
}
}
+
},
textToSpeech: function(val){
@@ -148,6 +166,37 @@ var speechBrowser = function(){
$('#cckw_rm').css("margin-left", "-2001px");
},
+ getDictionaryDefinition: function(src){
+
+ var word = src,
+ API_BASE_URL = "http://api.wordnik.com/api/",
+ API_KEY = "fe1c5d72e1c9ab7dde3040c8e6d0ae1063c79bae6ba86a1ce";
+
+ var url = (API_BASE_URL+"word.json/"+encodeURIComponent(word)+"/definitions?callback=?&api_key="+API_KEY);
+
+ $.getJSON(url, function(response){
+ var the_html = "<h2>Definitions for \"<span>"+word+"</span>\"</h2>";
+ the_html += "<ul>";
+ if (response.length>0) {
+ $.each(response, function(i, definition) {
+ //
+ if(i==0){
+ /*say aloud the first result of the set*/
+ speechBrowser.textToSpeech(definition.text);
+ }
+ //
+ if (definition.text) { the_html += "<li>"+definition.text+"</li>" };
+ });
+ } else {
+ the_html += "<li><em>No definitions! Try a different word.</em></li>";
+ }
+ the_html += "</ul>";
+ the_html += "</ul>";
+ $('#search-results').html(the_html);
+ });
+
+ },
+
getWeather:function(){
return $.Deferred(function(dfd){
$.simpleWeather({
@@ -200,6 +249,7 @@ var speechBrowser = function(){
$(function($) {
speechBrowser.init();
+ //speechBrowser.query('search house');
});
View
0 js/plugins.js 100644 → 100755
File mode changed.
View
0 js/script.js 100644 → 100755
File mode changed.

0 comments on commit f7db063

Please sign in to comment.