1 /* 2 * Copyright (C) 2012-2016 DFKI GmbH 3 * Deutsches Forschungszentrum fuer Kuenstliche Intelligenz 4 * German Research Center for Artificial Intelligence 5 * http://www.dfki.de 6 * 7 * Permission is hereby granted, free of charge, to any person obtaining a 8 * copy of this software and associated documentation files (the 9 * "Software"), to deal in the Software without restriction, including 10 * without limitation the rights to use, copy, modify, merge, publish, 11 * distribute, sublicense, and/or sell copies of the Software, and to 12 * permit persons to whom the Software is furnished to do so, subject to 13 * the following conditions: 14 * 15 * The above copyright notice and this permission notice shall be included 16 * in all copies or substantial portions of the Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 21 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 22 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 */ 26 27 importScripts('workerUtil.js'); 28 29 var semanticInterpreterImpl = { 30 31 _grammars: {}, 32 33 addGrammar: function(langCode, grammarFunc, options){ 34 35 var grammarData = this._grammars[langCode]; 36 37 if(!grammarData){ 38 39 grammarData = { 40 id: langCode, 41 exec: grammarFunc 42 }; 43 44 this._grammars[langCode] = grammarData; 45 46 } else { 47 48 grammarData.exec = grammarFunc; 49 50 } 51 52 self.postMessage({cmd: 'setgrammar', id: langCode, options: options}); 53 }, 54 55 setStopwords: function(langCode, stopwordList){ 56 57 self.postMessage({cmd: 'stopwords', id: langCode, stopwords: stopwordList}); 58 }, 59 60 execQuery: function(text, langCode, cmdId){ 61 62 var grammarData = this._grammars[langCode]; 63 if(!grammarData){ 64 postError('no grammar available for "'+langCode+'"'); 65 } 66 67 var result = grammarData.exec(text); 68 69 self.postMessage({cmd: 'parseresult', cmdId: cmdId, result: result}); 70 } 71 72 }; 73 74 require = function(name){ 75 if (name === 'semanticInterpreter') return semanticInterpreterImpl; 76 else console.error('unknown module: "'+name+'"'); 77 }; 78 79 self.onmessage = function(e){ 80 81 switch(e.data.cmd){ 82 case 'load': 83 load(e.data.url, e.data.id); 84 break; 85 case 'init': 86 init(e.data.text, e.data.id); 87 break; 88 case 'parse': 89 parse(e.data.text, e.data.id, e.data.cmdId); 90 break; 91 } 92 93 }; 94 95 function postError(msg){ 96 self.postMessage({cmd: 'error', message: msg}); 97 } 98 99 function load(compiledGrammarUrl, id){ 100 var libPath = getPath(compiledGrammarUrl); 101 importScripts( libPath ); 102 } 103 104 /** 105 * sets the config and echos back 106 * @param config 107 * @private 108 */ 109 function init(phrase, id){ 110 semanticInterpreterImpl.execQuery(phrase, id, 'init'); 111 } 112 113 function parse(phrase, id, cmdid){ 114 semanticInterpreterImpl.execQuery(phrase, id, cmdid); 115 }; 116