/*
 * The MIT License
 *
 * Copyright 2015 Zack.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.narvis.engine;

import com.narvis.dataaccess.DataAccessFactory;
import com.narvis.dataaccess.exception.NoDataException;
import com.narvis.dataaccess.interfaces.dataproviders.IDataModelProvider;
import com.narvis.dataaccess.models.lang.word.Dictionary;
import com.narvis.dataaccess.models.lang.word.Word;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Permet de parser une chaine de caractère en une liste de mots.
 *
 * @author Zack
 */
public class Parser {

    private final IDataModelProvider<Dictionary> dictionaryProvider;

    /**
     * Constructeur par défaut du Parser
     *
     * @throws java.lang.Exception
     */
    public Parser() throws Exception {
        // Récupération du RoutesProvider
        this.dictionaryProvider = (IDataModelProvider<Dictionary>) DataAccessFactory.getMetaDataProvider().getDataProvider("Dictionary");
    }

    /**
     * Transforme une chaine de caractères en une liste de mots en retirant les
     * mots inutiles
     *
     * @param sentence : Chaine de caractères contenant le message à parser
     * @return Le message parsé en une liste de mots
     * @throws com.narvis.dataaccess.exception.NoDataException
     */
    public List<String> parse(String sentence) throws NoDataException {
        List<String> parsedMessage = new ArrayList<>();
        
        /* We put the sentence to lowercase and remove all the non-alphanumeric caracters.
           This prevents us to the differents way users can write there messages (with punctuation or not, etc.)*/
        sentence = sentence.toLowerCase().replaceAll("[^a-z0-9|\" ]", "");
        
        /* We replace doubl-space that could be caused by the suppression of a single caracter with one space */
        sentence = sentence.toLowerCase().replaceAll("  ", " ");

        sentence = transformSpaceInQuoteWithUnderscore(sentence);
        
        parsedMessage.addAll(Arrays.asList(sentence.split(" ")));
        
        List<Word> ignoredWords = dictionaryProvider.getModel().getIgnoredWords();
        parsedMessage.removeAll(wordsToStrings(ignoredWords));
        
        parsedMessage = replaceUndescoreBySpace(parsedMessage);
        return parsedMessage;
    }
    
    /**
     * Brows details map to find details that are sentences and parse these sentences.
     * @param detailsToValue : Details map to scan
     * @return Parsed sentences
     * @throws com.narvis.dataaccess.exception.NoDataException
     */
    public List<List<String>> getParsedSentencesFromDetails(Map<String, String> detailsToValue) throws NoDataException
    {
        List<List<String>> parsedSentences = new LinkedList<>();
        
        Set keys = detailsToValue.keySet();
        Iterator it = keys.iterator();
        while (it.hasNext()){
           String key = (String) it.next();
           
           /* If the key contain spaces, this is actualy a sentence */
           if(key.contains(" "))
           {
               List<String> currentParsedSentence = this.parse(key);
               parsedSentences.add(currentParsedSentence);
           }
        }
        return parsedSentences;
    }

    /**
     * Remplace les espaces entre les mots entourés de " par des _ afin qu'ils
     * soient concidérés comme un ensemble de mots
     *
     * @param sentence
     */
    private String transformSpaceInQuoteWithUnderscore(String sentence) {
        boolean replaceSpace = false;

        char[] sentenceChar = sentence.toCharArray();

        for (int i = 0; i < sentenceChar.length; i++) {
            if (sentenceChar[i] == '"') {
                replaceSpace = !replaceSpace;
            }else if (replaceSpace && sentenceChar[i] == ' ')
            {
                sentenceChar[i] = '_';
            }
        }

        sentence = String.copyValueOf(sentenceChar);
        
        sentence = sentence.replaceAll("\"", "");
        
        return sentence;
    }

    /**
     * Remplace dans tous les mots d'une liste de mots les underscore par des espaces
     * @param parsedSentence  : La liste de mot à traiter
     * @return
     */
    private List<String> replaceUndescoreBySpace(List<String> parsedSentence)
    {
        List<String> newParsedSentence = new LinkedList<>();
        for(String word : parsedSentence)
        {
            word = word.replace("_", " ");
            newParsedSentence.add(word);
        }
        return newParsedSentence;
    }

    /**
     * Transforme une liste de Word en une liste de String correspondant aux
     * attributs "Value"
     *
     * @param words : Liste de mots (Word) à convertir
     * @return Liste de String correspondante
     */
    private List<String> wordsToStrings(List<Word> words) {
        List<String> strings = new ArrayList<>();
        
        for(Word word : words)
        {
            strings.add(word.getValue());
        }

        return strings;
    }
}

@@ -3412,24 +3412,137 @@
 edMessage);%0A
+        %0A        /* Remove empty words in word list */%0A        parsedMessage.removeAll(Arrays.asList(%22%22, null));%0A
         retu

Optimize Parser