ITeye博客

计算相似程度、推荐

googya · 2010-05-05 10:07 · JRuby&Ruby · 编程语言 · Markdown

在聚类分析中,尤其是文本聚类中通常要进行2对象间的相似性的比较,以便于将相似程度大的归为一组。这样的结果就是,一个组内的对象相似度大,而不同组之间对象的相似性小。这个就是所谓的聚类。以下说说在文本中聚类的实现。为了将文本分组,首先要将文本的相似性计算出来(我用的是22比较)。这时候的相似性就要有一个参照,这个标准通常是2文本中相同或相似字符。如果相同字符的次数与2文本的总长度之和的比较大的话,就认为这2个文本是相似的,否则,不相似。这种方式的效率不高(每2个文本都要进行比较,复杂度是n^2,n为文本的数量),但是比较出来的效果很可观。

下面是一个与此有关的代码片段:



include Math
#将那本书上的字典数据结构转换成Ruby的hash也不容易啊,要进行替换。先转换成字符串s,把s中的:用=>取代,再把\n去掉,这才成为下面的结果
critics={'Lisa Rose'=> {'Lady in the Water'=> 2.5, 'Snakes on a Plane'=> 3.5, 'Just My Luck'=> 3.0, 'Superman Returns'=> 3.5, 'You, Me and Dupree'=> 2.5, 'The Night Listener'=> 3.0},'Gene Seymour'=> {'Lady in the Water'=> 3.0, 'Snakes on a Plane'=> 3.5, 'Just My Luck'=> 1.5, 'Superman Returns'=> 5.0, 'The Night Listener'=> 3.0, 'You, Me and Dupree'=> 3.5},'Michael Phillips'=> {'Lady in the Water'=> 2.5, 'Snakes on a Plane'=> 3.0, 'Superman Returns'=> 3.5, 'The Night Listener'=> 4.0},'Claudia Puig'=> {'Snakes on a Plane'=> 3.5, 'Just My Luck'=> 3.0, 'The Night Listener'=> 4.5, 'Superman Returns'=> 4.0, 'You, Me and Dupree'=> 2.5},'Mick LaSalle'=> {'Lady in the Water'=> 3.0, 'Snakes on a Plane'=> 4.0, 'Just My Luck'=> 2.0, 'Superman Returns'=> 3.0, 'The Night Listener'=> 3.0, 'You, Me and Dupree'=> 2.0},'Jack Matthews'=> {'Lady in the Water'=> 3.0, 'Snakes on a Plane'=> 4.0, 'The Night Listener'=> 3.0, 'Superman Returns'=> 5.0, 'You, Me and Dupree'=> 3.5},'Toby'=> {'Snakes on a Plane'=>4.5,'You, Me and Dupree'=>1.0,'Superman Returns'=>4.0}}

def sim_distance(prefs,person1,person2)
		si={}
		prefs[person1].each_key do |k1|
			if prefs[person2].include? k1
					si[k1]=1
			end
		end
		
		return 0  if si.size==0
		sum=0
		prefs[person1].each_key do |k1|
					if prefs[person2].include? k1
						sum+=(prefs[person1][k1]-prefs[person2][k1])**2
					end
		end
		1/(1+sum)
end
	
#p sim_distance(critics,'Lisa Rose','Gene Seymour')
#p sim_distance(critics,'Lisa Rose','Michael Phillips')
#p sim_distance(critics,'Lisa Rose','Claudia Puig')
#p sim_distance(critics,'Lisa Rose','Mick LaSalle')
#p sim_distance(critics,'Lisa Rose','Jack Matthews')
#p sim_distance(critics,'Lisa Rose','Toby')
		
	def find_most_similar(lis)#将hash中值最大的那一对找出来
		most={}
		max=0
		#lis=lis.sort
			lis.each_key do |k1|
				lis.each_key do |k2|
					if k1<k2 && k1 && k2						
							most["#{k1}" +" & "+ "#{k2}"]=sim_distance(lis,k1,k2)#这里有个问题,hash中存在重复。如何消除
						
					end
				end
			end
			most.each_key do|k|
				if most[k]>max
					max=most[k]
				end
			end
			#most.sort{|a,b|a[1]<=>b[1]}#这样就变成二维数组了
			most.each_pair do |k,v|
				puts "#{k}"+" "*(35-"#{k}".size)+": "+"#{v}"
			end
			
			[most.index(max),max]			
	end 
	 
	#p find_most_similar(critics)
	#p critics.size
	
	
	def sim_person(prefs,p1,p2)
			si={}
			prefs[p1].each_key do |k1|
				if prefs[p2].include? k1
						si[k1]=1
				end
			end
			n=si.size
		return 0  if n==0
		
		sum1=0
		sum2=0
		sum1Sq=0
		sum2Sq=0
		pSum=0
		si.each_key do |k|
			sum1+=prefs[p1][k]
			sum2+=prefs[p2][k]
			sum1Sq+=prefs[p1][k]**2
			sum2Sq+=prefs[p2][k]**2
			pSum+=prefs[p1][k]*prefs[p2][k]			
		end
		num=pSum-(sum1*sum2/n)
		den=sqrt((sum1Sq-sum1**2/n)*(sum2Sq-sum2**2/n))
		return 0 if den==0
		num/den	
	end
	
	
	#p sim_person(critics,'Lisa Rose','Gene Seymour')
	def topMatcher(prefs,person,n=5)
		scores=[]
		prefs.each_key do |k|
			if person!=k
				scores << [sim_person(prefs,person,k),k]
			end
		end
		scores=scores.sort.reverse
		scores[0...3]
	end
	#p topMatcher(critics,'Toby',n=3)
	
	
	def getRecommendations(prefs,person,t=self.method(:sim_person))
		totals={}
		simSums={}
		prefs.each_key do |other|
			if other==person
				next
			end
			
			sim=t.call(prefs,person,other)
			if sim<=0
				next
			end
			
			prefs[other].each_key do |item|
				if !prefs[person].include?(item)  || prefs[person][item]==0
					totals[item]||=0
					totals[item]+=prefs[other][item]*sim
					simSums[item]||=0
					simSums[item]+=sim
				end
			end
		end
		
		rankings=[]
		totals.each_pair do |item,total|
			rankings<<[total/simsums[item],item]
		end
		rankings.sort
		rankings.reverse
		rankings		
	end
	#t=self.method(:sim_distance)#t=self.method(:sim_person)
	#p t
	#p getRecommendations(critics,'Toby')
	
	
	def transformPrefs(prefs)
		result={}
		prefs.each_key do |person|
			prefs[person].each_key do |item|
				result[item]||={}
				result[item][person]=prefs[person][item]
			end
		end
		result
	end
	
	#p transformPrefs(critics)

  对于文本的话,要对文中出现的字符进行比较(当然还包括去除停用词之类的)。方法与此相似。

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。