de.tudarmstadt.lt.n2n.utilities.PatternGenerator.java Source code

Java tutorial

Introduction

Here is the source code for de.tudarmstadt.lt.n2n.utilities.PatternGenerator.java

Source

/*
 *   Copyright 2012
 *
 *   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 de.tudarmstadt.lt.n2n.utilities;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;

/**
 *
 * @author Steffen Remus
 **/
public class PatternGenerator {

    private static int[] EMPTY_ARR = new int[0];

    public String WILDCARD = "*";

    public String[] get_patterns(String str) {
        String[] words = str.split("\\s+");
        List<String> patterns = new ArrayList<String>();
        for (int i = 1; i < words.length; i++) {
            for (String[] pattern : get_merged_patterns(words, i))
                patterns.add(StringUtils.join(pattern, " "));
        }
        return patterns.toArray(new String[0]);
    }

    public String[][] get_merged_patterns(String[] words, int num_wildcards) {
        return get_merged_patterns(words, num_wildcards, EMPTY_ARR);
    }

    public String[][] get_merged_patterns(String[] words, int num_wildcards, int[] fixed) {
        String[][] raw_patterns = get_raw_patterns(words, num_wildcards, fixed);
        String[][] merged_patterns = merge_wildcards(raw_patterns);
        return merged_patterns;

    }

    public String[][] merge_wildcards(String[][] patterns) {
        String[][] merged_pattern = new String[patterns.length][];
        for (int i = 0; i < patterns.length; i++) {
            merged_pattern[i] = merge_wildcards(patterns[i]);
        }
        return merged_pattern;
    }

    public String[] merge_wildcards(String[] pattern) {
        String[] merged_pattern = new String[1];
        merged_pattern[0] = pattern[0];
        for (int i = 1; i < pattern.length; i++) {
            if (WILDCARD.equals(pattern[i]) && WILDCARD.equals(pattern[i - 1]))
                continue;
            merged_pattern = (String[]) ArrayUtils.add(merged_pattern, pattern[i]);
        }
        return merged_pattern;
    }

    public String[][] get_raw_patterns(String[] words, int num_wildcards, int[] fixed) {
        int[][] combinations = comb(num_wildcards, words.length, fixed, true);
        String[][] patterns = new String[combinations.length][];
        for (int c = 0; c < combinations.length; c++) {
            int[] comb = combinations[c];
            String[] pattern = words.clone();
            for (int i = 0; i < comb.length; i++) {
                pattern[comb[i]] = WILDCARD;
            }
            patterns[c] = pattern;
        }
        return patterns;
    }

    public static int[] bitadd(int u, int k) {
        int[] r_ = new int[k];
        for (int n = 0, i = 0; u > 0; ++n, u >>= 1)
            if ((u & 1) > 0)
                r_[i++] = n;
        return r_;
    }

    public static int bitcount(int u) {
        int n;
        for (n = 0; u > 0; ++n, u &= (u - 1))
            ;//Turn the last set bit to a 0
        return n;
    }

    public static int[][] comb(int k, int n, int[] fixed, boolean sort) {
        int[][] s = new int[0][];
        for (int u = 0; u < 1 << n; u++)
            if (bitcount(u) == k) {
                int[] c = bitadd(u, k);
                boolean add = true;
                for (int f : fixed)
                    add &= !ArrayUtils.contains(c, f);
                if (add)
                    s = (int[][]) ArrayUtils.add(s, c);
            }
        if (sort)
            Arrays.sort(s, new Comparator<int[]>() {
                @Override
                public int compare(int[] o1, int[] o2) {
                    for (int i = 0; i < o1.length; i++) {
                        int r = o2[i] - o1[i];
                        if (r != 0)
                            return r;
                    }
                    return 0;
                }
            });
        return s;
    }

}