NLP_LCS
来源:互联网 发布:淘宝客推广大师官网 编辑:程序博客网 时间:2024/06/11 10:06
LCS 作用:
• 求两个序列中最长的公共子序列算法
– 生物学家常利用该算法进行基金序列比对,以推测序列的结构、功能和演化过程。
• 描述两段文字之间的“相似度”
– 辨别抄袭,对一段文字进行修改之后,计算改动前后文字的最长公共子序列,将除此子序列
外的部分提取出来,该方法判断修改的部分
LCS 解决方法:
属于动态规划问题
• 使用二维数组C[m,n]
• C[i,j]记录序列Xi和Yj的最长公共子序列的长度
– 当i=0或j=0时C[i,j]=0
LCS action on MapReduce
shell script
HADOOP_CMD="/usr/local/src/hadoop-1.2.1/bin/hadoop"STREAM_JAR_PATH="/usr/local/src/hadoop-1.2.1/contrib/streaming/hadoop-streaming-1.2.1.jar"INPUT_FILE_PATH_1="/lcs_input.data"OUTPUT_PATH="/lcs_output"#$HADOOP_CMD fs -rmr -skipTrash $OUTPUT_PATH# Step 1.$HADOOP_CMD jar $STREAM_JAR_PATH \ -input $INPUT_FILE_PATH_1 \ -output $OUTPUT_PATH \ -mapper "python map.py" \ -jobconf "mapred.reduce.tasks=0" \ -jobconf "mapred.job.name=mr_lcs" \ -file ./map.py
map.py
# -*- coding: utf-8 -*-#!/usr/bin/pythonimport sysdef cal_lcs_sim(first_str, second_str): len_vv = [[0] * 50] * 50 first_str = unicode(first_str, "utf-8", errors='ignore') second_str = unicode(second_str, "utf-8", errors='ignore') len_1 = len(first_str.strip()) len_2 = len(second_str.strip()) #for a in first_str: #word = a.encode('utf-8') for i in range(1, len_1 + 1): for j in range(1, len_2 + 1): if first_str[i - 1] == second_str[j - 1]: len_vv[i][j] = 1 + len_vv[i - 1][j - 1] else: len_vv[i][j] = max(len_vv[i - 1][j], len_vv[i][j - 1]) return float(float(len_vv[len_1][len_2] * 2) / float(len_1 + len_2))for line in sys.stdin: ss = line.strip().split('\t') if len(ss) != 2: continue first_str = ss[0].strip() second_str = ss[1].strip() sim_score = cal_lcs_sim(first_str, second_str) print '\t'.join([first_str, second_str, str(sim_score)])
Process Screenshot
note
debug时,看log看以.log结尾的文件就行,这就是最新的log,结尾带时间的是以前的log。