/**
 * Licensed to DigitalPebble Ltd under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * DigitalPebble licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.digitalpebble.stormcrawler.parse.filter;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.w3c.dom.DocumentFragment;

import com.digitalpebble.stormcrawler.parse.ParseFilter;
import com.digitalpebble.stormcrawler.parse.ParseResult;
import com.fasterxml.jackson.databind.JsonNode;
import com.optimaize.langdetect.DetectedLanguage;
import com.optimaize.langdetect.LanguageDetector;
import com.optimaize.langdetect.LanguageDetectorBuilder;
import com.optimaize.langdetect.ngram.NgramExtractors;
import com.optimaize.langdetect.profiles.LanguageProfile;
import com.optimaize.langdetect.profiles.LanguageProfileReader;
import com.optimaize.langdetect.text.CommonTextObjectFactories;
import com.optimaize.langdetect.text.TextObject;
import com.optimaize.langdetect.text.TextObjectFactory;

/**
 * Language identification; the language codes gets stored in the metadata. <br>
 * 
 * To use it, just add the module as a dependency in your pom and include
 * 
 * ```json { "class": "com.digitalpebble.stormcrawler.parse.filter.LanguageID",
 * "name": "LanguageID", "params": { "key": "lang" , "minProb": 0.99} }
 * 
 * in the parse filter config. ```
 **/

public class LanguageID extends ParseFilter {

    private static LanguageDetector languageDetector;

    private static final TextObjectFactory textObjectFactory = CommonTextObjectFactories
            .forDetectingOnLargeText();

    private String mdKey = "lang";
    private float minProb = 0.999f;

    static {
        try {
            // load all languages:
            List<LanguageProfile> languageProfiles = new LanguageProfileReader()
                    .readAllBuiltIn();
            // build language detector:
            languageDetector = LanguageDetectorBuilder
                    .create(NgramExtractors.standard())
                    .withProfiles(languageProfiles).build();
        } catch (IOException e) {
            throw new RuntimeException("Error while loading language profiles",
                    e);
        }
    }

    @Override
    public void configure(Map stormConf, JsonNode filterParams) {
        JsonNode node = filterParams.get("key");
        if (node != null && node.isTextual()) {
            mdKey = node.asText("lang");
        }
        node = filterParams.get("minProb");
        if (node != null && node.isNumber()) {
            minProb = node.floatValue();
        }
    }

    @Override
    public void filter(String url, byte[] content, DocumentFragment doc,
            ParseResult parse) {

        String text = parse.get(url).getText();
        if (StringUtils.isBlank(text)) {
            return;
        }

        TextObject textObject = textObjectFactory.forText(text);
        synchronized (languageDetector) {
            List<DetectedLanguage> probs = languageDetector
                    .getProbabilities(textObject);
            if (probs == null || probs.size() == 0) {
                return;
            }
            for (DetectedLanguage lang : probs) {
                if (lang.getProbability() >= minProb) {
                    String code = lang.getLocale().getLanguage();
                    parse.get(url).getMetadata().addValue(mdKey, code);
                }
            }
        }
    }

}

@@ -2326,16 +2326,50 @@
  0.999f;
+%0A    private String mdSkip = null;
 %0A%0A    st
@@ -3289,130 +3289,623 @@
     
-%7D%0A%0A    @Override%0A    public void filter(String url, byte%5B%5D content, DocumentFragment doc,%0A            ParseResult parse) %7B
+    node = filterParams.get(%22md.skip%22);%0A        if (node != null && node.isTextual()) %7B%0A            mdSkip = node.asText();%0A        %7D%0A    %7D%0A%0A    @Override%0A    public void filter(String url, byte%5B%5D content, DocumentFragment doc,%0A            ParseResult parse) %7B%0A%0A        // check whether the metadata already contains a lang value%0A        // in which case we might want to skip%0A        if (mdSkip != null) %7B%0A            String existingVal = parse.get(url).getMetadata()%0A                    .getFirstValue(mdSkip);%0A            if (StringUtils.isNotBlank(existingVal)) %7B%0A                return;%0A            %7D%0A        %7D
 %0A%0A  

Add option to LanguageID to skip if metadata already set, fixes #570