Comment detail

ナイツ関数(ボケの方) (Nested Flatten)

ボケるのに「レーベンシュタイン距離( http://ja.wikipedia.org/wiki/%E3%83%AC%E3%83%BC%E3%83%99%E3%83%B3%E3%82%B7%E3%83%A5%E3%82%BF%E3%82%A4%E3%83%B3%E8%B7%9D%E9%9B%A2 )」を使ってみました。

入力はテキストファイルからで。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
 * NightsFormula.java
 *
 */
import java.io.*;
import java.util.*;

public class NightsFormula {
    
    private static final double _MINSimilarityRate = 0.51;
    
    public static void main(String[] args){
        
        String[] wordList = getWordList( new String[]{"fam25_10.txt","fam40_25.txt","fam55_40.txt","fam70_55.txt"});
        
        String[] srcList = getWordList(new String[]{"inputSample.txt"});
        
        if(wordList == null || srcList == null){
            System.out.println("word list or src list is NULL");
            return;
        }
        StringBuffer outb = new StringBuffer();
        for(int i = 0; i < srcList.length; i++){
            StringBuffer sb = new StringBuffer();
            sb.append(srcList[i]).append("\t");
            for(int j = 0; j < wordList.length; j++){
                double sr = getSimilarityRate(srcList[i], wordList[j]);
                if(sr >=_MINSimilarityRate){
                    sb.append(wordList[j]).append("\t");
                }
            }
            String[] cand = split(sb);
            String sc = cand[(int)(cand.length*Math.random())];
            outb.append(sc).append(" ");
        }
        System.out.println(outb.toString());
    }
    
    private static double getSimilarityRate(String str1, String str2){
        int ld = getLevenshteinDistance(str1, str2);
        int len = (str1.length() > str2.length() ? str1.length() : str2.length());
        double result = ((double)(len - ld) / len);
        return result;
    }
    
    private static int getLevenshteinDistance(String str1, String str2){
        
    int[][] lArr = new int[str1.length()+1][str2.length()+1];
        
    for(int i = 0; i < lArr.length; i++) lArr[i][0] = i;
    for(int i = 0; i < lArr[0].length; i++) lArr[0][i] = i;
    
    for(int i = 0; i < str1.length(); i++){
            for(int j = 0; j < str2.length(); j++){
                int cost = (str1.charAt(i) == str2.charAt(j)) ? 0 : 1;
                lArr[i+1][j+1] = getMinimum( lArr[i][j+1] +1, lArr[i+1][j] +1, lArr[i][j] +cost );
            }
    }
    return lArr[str1.length()][str2.length()];
    }
    
    private static int getMinimum(int a, int b, int c){
        int m = a < b ? a : b;
        return m < c ? m : c;
    }
    
    private static String[] getWordList(String[] filenames){
        FileReader fr = null;
        BufferedReader in = null;
        StringBuffer sb = new StringBuffer();
        for(int i = 0; i < filenames.length; i++){
            try{
                in = new BufferedReader(fr = new FileReader(filenames[i]));
                String line = null;
                while((line=in.readLine())!= null){
                    sb.append(line).append("\t");
                }
            }catch(Throwable t){
                t.printStackTrace();
                return null;
            }finally{
                if(in != null){ try{ in.close(); }catch(Throwable t){ ; }finally{ in = null;} }
                if(fr != null){ try{ fr.close(); }catch(Throwable t){ ; }finally{ fr = null;} }
            }
        }
        return split(sb);
    }
    
    private static String[] split(StringBuffer sb){
        StringTokenizer st = new StringTokenizer(sb.toString(), " \t");
        int len = st.countTokens();
        String[] wlist = new String[len];
        for(int i = 0; i < len; i++){
            wlist[i] = st.nextToken();
        }
        return wlist;
    }
}

Index

Feed

Other

Link

Pathtraq

loading...