/*
* Copyright [2016-2020] [George Papadakis (gpapadis@yahoo.gr)]
*
* Licensed 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 org.scify.jedai.blockbuilding;

import java.util.HashSet;
import java.util.Set;

import org.apache.jena.atlas.json.JsonArray;
import org.apache.jena.atlas.json.JsonObject;

/**
 *
 * @author gap2
 */
public class ExtendedSuffixArraysBlocking extends SuffixArraysBlocking {

    public ExtendedSuffixArraysBlocking() {
        this(39, 6);
    }
     public ExtendedSuffixArraysBlocking(int maxSize, int minLength) {
        super(maxSize, minLength);
    }

    @Override
    protected Set<String> getBlockingKeys(String attributeValue) {
        final Set<String> suffixes = new HashSet<>();
        for (String token : getTokens(attributeValue)) {
            suffixes.addAll(getExtendedSuffixes(minimumSuffixLength, token));
        }
        return suffixes;
    }

    @Override
    public String getMethodInfo() {
        return getMethodName() + ": it creates one block for every substring (not just suffix) that appears in the tokens of at least two entities.";
    }

    @Override
    public String getMethodName() {
        return "Extended Suffix Arrays Blocking";
    }

    public Set<String> getExtendedSuffixes(int minimumLength, String blockingKey) {
        final Set<String> suffixes = new HashSet<>();
        suffixes.add(blockingKey);
        if (minimumLength <= blockingKey.length()) {
            for (int nGramSize = blockingKey.length() - 1; minimumLength <= nGramSize; nGramSize--) {
                int currentPosition = 0;
                final int length = blockingKey.length() - (nGramSize - 1);
                while (currentPosition < length) {
                    String newSuffix = blockingKey.substring(currentPosition, currentPosition + nGramSize);
                    suffixes.add(newSuffix);
                    currentPosition++;
                }
            }
        }
        return suffixes;
    }

    @Override
    public JsonArray getParameterConfiguration() {
        final JsonObject obj1 = new JsonObject();
        obj1.put("class", "java.lang.Integer");
        obj1.put("name", getParameterName(0));
        obj1.put("defaultValue", "6");
        obj1.put("minValue", "2");
        obj1.put("maxValue", "6");
        obj1.put("stepValue", "1");
        obj1.put("description", getParameterDescription(0));

        final JsonObject obj2 = new JsonObject();
        obj2.put("class", "java.lang.Integer");
        obj2.put("name", getParameterName(1));
        obj2.put("defaultValue", "39");
        obj2.put("minValue", "2");
        obj2.put("maxValue", "100");
        obj2.put("stepValue", "1");
        obj2.put("description", getParameterDescription(1));

        final JsonArray array = new JsonArray();
        array.add(obj1);
        array.add(obj2);
        return array;
    }

    @Override
    public String getParameterDescription(int parameterId) {
        switch (parameterId) {
            case 0:
                return "The " + getParameterName(0) + " determines the minimum number of characters in an attribute value substring that is used as blocking key.";
            case 1:
                return "The " + getParameterName(1) + " determines the maximum number of entities that correspond to a valid substring (i.e., maximum block size).";
            default:
                return "invalid parameter id";
        }
    }

    @Override
    public String getParameterName(int parameterId) {
        switch (parameterId) {
            case 0:
                return "Minimum Substring Length";
            case 1:
                return "Maximum Substring Frequency";
            default:
                return "invalid parameter id";
        }
    }
}

@@ -2742,17 +2742,17 @@
 terName(
-0
+1
 ));%0D%0A   
@@ -2774,33 +2774,34 @@
 defaultValue%22, %22
-6
+39
 %22);%0D%0A        obj
@@ -2855,17 +2855,19 @@
 alue%22, %22
-6
+100
 %22);%0D%0A   
@@ -2956,17 +2956,17 @@
 ription(
-0
+1
 ));%0D%0A%0D%0A 
@@ -3098,33 +3098,33 @@
 etParameterName(
-1
+0
 ));%0D%0A        obj
@@ -3138,34 +3138,33 @@
 defaultValue%22, %22
-39
+6
 %22);%0D%0A        obj
@@ -3210,35 +3210,33 @@
 ut(%22maxValue%22, %22
-100
+6
 %22);%0D%0A        obj
@@ -3309,38 +3309,46 @@
 eterDescription(
-1
+0
 ));%0D%0A
+        
 %0D%0A        final 
@@ -3669,18 +3669,18 @@
 es the m
-in
+ax
 imum num
@@ -3690,40 +3690,43 @@
  of 
-characters in an attribute
+entities that correspond to a
  val
-ue
+id
  sub
@@ -3736,36 +3736,34 @@
 ing 
-that is used as blocking key
+(i.e., maximum block size)
 .%22;%0D
@@ -3848,34 +3848,34 @@
 determines the m
-ax
+in
 imum number of e
@@ -3877,43 +3877,40 @@
  of 
-entities that correspond to a
+characters in an attribute
  val
-id
+ue
  sub
@@ -3920,34 +3920,36 @@
 ing 
-(i.e., maximum block size)
+that is used as blocking key
 .%22;%0D
@@ -4187,18 +4187,18 @@
 eturn %22M
-in
+ax
 imum Sub
@@ -4204,22 +4204,25 @@
 bstring 
-Length
+Frequency
 %22;%0D%0A    
@@ -4255,34 +4255,34 @@
        return %22M
-ax
+in
 imum Substring F
@@ -4276,33 +4276,30 @@
 m Substring 
-Frequency
+Length
 %22;%0D%0A        

Update ExtendedSuffixArraysBlocking.java