ナイツ関数(ボケの方)
Posted feedbacks - Groovy
javaの解法を参考にさせていただきました。
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 | #!/usr/bin/env groovy
final MIN_SIMILARITY_RATE = 0.51
def wordList = []
["fam25_10.txt", "fam40_25.txt", "fam55_40.txt", "fam70_55.txt"].each{
Collections.addAll(wordList, new File(it).text.split(/\s/))
}
def file = new File("inputSample.txt")
assert wordList
assert file?.exists()
println file.text.replaceAll(/(?m)\S+/){ word ->
def convertList = [ word, *wordList.findAll{ other ->
getSimilarityRate(word, other) >= MIN_SIMILARITY_RATE
} ]
Collections.shuffle(convertList)
convertList[0]
}
double getSimilarityRate(String str1, String str2){
def len1 = str1.size()
def len2 = str2.size()
def matrix = new int[len1+1][len2+1]
for(int i = 0; i < matrix.size(); i++){
matrix[i][0] = i
}
for(int i = 0; i < matrix[0].size(); i++){
matrix[0][i] = i
}
for(int i = 0; i < len1; i++){
for(int j = 0; j < len2; j++){
int cost = (str1[i] == str2[j]) ? 0 : 1
matrix[i+1][j+1] = [matrix[i][j+1] +1, matrix[i+1][j] +1, matrix[i][j] +cost].min()
}
}
def longer = [len1, len2].max()
(longer - matrix[len1][len2])/longer
}
|


syat
#8549()
[
Other
]
Rating-2/2=-1.00
(ナベアツ算を見てて思いつきました)
入出力の方法は標準入出力や引数・戻り値など、扱いやすい方法でかまいません。
文字単位でランダムに間違えても面白くないので、単語のリストから似た単語の候補を探すようにしてください。英単語でもOKです。単語のリストは参考ページからダウンロードしたものを加工して利用すると良いと思います。(4000個あります)
結果がつまらなくても構いませんが、面白いボケをうむ工夫があると良いです。
※人が考えたボケは求めてませんよ!
入力の例として「どう書く?org」の前文をお借りしました。ご自分でヤホーで調べたりして手ごろな文章を見つけて下しあ。
see: 日本語単語リスト
Rating-2/2=-1.00-0+
[ reply ]