Инструмент «Часть слова» SlimParser

tolka1

Пользователь
Регистрация
13.08.15
Сообщения
3
Реакции
0
Баллы
1
Я начал возвращаться к анализаторам текста и анализировать части текста. У меня есть действительно хороший парсер, который я буду использовать в Electron. Другим, создающим текстовые игры, это может понравиться. Есть простая ошибка, и у меня был ответ раньше, но его больше нет. Я использую регулярное выражение для извлечения именованных групп захвата. Единственная ошибка, которую я вижу в этом, заключается в том, что группы для indirectObject и DirectObject должны содержать любое количество символов и пробелов.

alert2 — разделитель будет последним словом в группе захвата.
alert3 - Там же.
alert4 — разделитель является предлогом.
alert5 — первый разделитель для DirectObject — это предлог, IndirectObject — последнее слово в группе захвата.

Код:
// This parser will return a parsedCommand object filled with usefull sentence parts. function SlimParser() { this.parsedCommand = { "patternType":null, "verb":null, "indirectObject":null, "directObject":null, "preposition":null, "error":false }; this.prepositionList = ['on', 'under', 'over', 'above', 'down', 'up', 'with', 'across', 'around', 'from', 'at', 'to', 'for', 'about']; } SlimParser.prototype.parse = function(command){ var commandArray = command.split(" "); if(commandArray.length == 1) { if(/(?<verb>[^ $]*)/.test(command)) { const matches = /(?<verb>[^ $]*)/.exec(command); this.parsedCommand.patternType = "V"; // verb this.parsedCommand.verb = matches.groups.verb; alert("in 1"); }else{ } }else{ if(!this.prepositionInStr(command)) { if(/(?<verb>[^ $]*)( the)? (?<directObject>[\w+]*)/.test(command)) { const matches = /(?<verb>[^ $]*)( the)? (?<directObject>[\w+]*)/.exec(command); this.parsedCommand.patternType = "VO"; // verb object this.parsedCommand.verb = matches.groups.verb; this.parsedCommand.directObject = matches.groups.directObject; alert("in 2"); } }else{ if(this.strIsPrepoistion(commandArray[1])) { if(/(?<verb>[^ $]*) (on|under|over|above|down|up|with|across|around|from|at|to|for|about)( the)? (?<directObject>[^ $]*)/.test(command)) { const matches = /(?<verb>[^ $]*) (on|under|over|above|down|up|with|across|around|from|at|to|for|about)( the)? (?<directObject>[^ $]*)/.exec(command); this.parsedCommand.patternType = "VPO"; // verb preposition object this.parsedCommand.verb = matches.groups.verb; this.parsedCommand.preposition = commandArray[1]; this.parsedCommand.directObject = matches.groups.directObject this.parsedCommand.preposition = this.prepositionFetch(command); alert("in 3"); } }else if(this.strIsPrepoistion(commandArray[commandArray.length - 1])) { if(/(?<verb>[^ $]*)( the)? (?<directObject>[^ $]*) (on|under|over|above|down|up|with|across|around|from|at|to|for|about)/.test(command)) { const matches = /(?<verb>[^ $]*)( the)? (?<directObject>[^ $]*) (on|under|over|above|down|up|with|across|around|from|at|to|for|about)/.exec(command); this.parsedCommand.patternType = "VOP"; // verb object preposition this.parsedCommand.verb = matches.groups.verb; this.parsedCommand.preposition = commandArray[commandArray.length - 1]; this.parsedCommand.directObject = matches.groups.directObject this.parsedCommand.indirectObject = matches.groups.indirectObject alert("in 4"); } }else{ if(/(?<verb>[^ $]*)( the)? (?<directObject>[^ $]*) (on|under|over|above|down|up|with|across|around|from|at|to|for|about)( the)? (?<indirectObject>[^ $]*)/.test(command)) { const matches = /(?<verb>[^ $]*)( the)? (?<directObject>[^ $]*) (on|under|over|above|down|up|with|across|around|from|at|to|for|about)( the)? (?<indirectObject>[^ $]*)/.exec(command); this.parsedCommand.patternType = "VOPO"; // verb object preposition object this.parsedCommand.verb = matches.groups.verb; this.parsedCommand.directObject = matches.groups.directObject this.parsedCommand.indirectObject = matches.groups.indirectObject this.parsedCommand.preposition = this.prepositionFetch(commandArray); alert("in 5"); } } } } return this.parsedCommand; }; SlimParser.prototype.prepositionInStr = function(command) { let prepositionAvailable = false; for(let i = 0; i <= this.prepositionList.length - 1; i++) { if(command.includes(this.prepositionList[i])) { prepositionAvailable = true; } } return prepositionAvailable; } SlimParser.prototype.strIsPrepoistion = function(val) { let isPreposition = false; for(let i = 0; i <= this.prepositionList.length - 1; i++) { if(val == this.prepositionList[i]) { isPreposition = true; } } return isPreposition; }; SlimParser.prototype.prepositionFetch = function(testArr) { let prepositionStr = ""; for(let i = 0; i<= testArr.length - 1; i++) { for(let y = 0; y <= this.prepositionList.length - 1; y++) { if(testArr[i] == this.prepositionList[y]) { prepositionStr = this.prepositionList[y]; } } } return prepositionStr; };
Код (разметка): я понятия не имею, как это написать в регулярном выражении. Спасибо.
Инструмент «Часть слова» SlimParser
 
Сверху Снизу