CORDEA blog

Android applications engineer

KivyでTwitterのTweetの類似性スコアを算出する

はじめに

PythonTwitterにいる2ユーザーのTweetを取得し類似性スコアを算出した後、KivyでGUI化します。
開発に際しては、集合知プログラミング(ISBN:9784873113647)を参考にさせていただきました。

Kivyは日本語を使用するのが少々面倒なため、今回は英語のTweetをターゲットとします。



 

バージョン、使用モジュール等


 

概要

  1. Twitter APIによってTweetを取得
  2. 空白毎にTweetを分割
  3. 分割したTweetをステミング
  4. URLをディクショナリから排除
  5. Blacklistのワード(a, theなどの単語)をディクショナリから排除
  6. 重複しているワードを纏めてディクショナリとして格納
  7. ピアソン分布/ユークリッド距離で2ユーザーの距離を測定
  8. 今回はラベルに出力


 

内容

コア部分はこのようになっています。
今回はusers01とusers02という2つに分けて書いていますが、この2つは1つに纏めて作成すべきでしたね...
特に複数人の類似性スコアの算出したいような場合、分けて書くことはきわめて非効率です。

user01 = self.screenti01.text

num = self.tweetnum.text

if len(user01) == 0 or len(num) == 0:
	self.blank_popup.open()
	print '# ERROR: Abnormal parameters'
else:	
	print '# 01:' + self.screenti01.text
	users01 = api.GetUserTimeline(screen_name=user01, count=int(num))
	tweetlist01 = [s.text for s in users01]
	tweetdict01 = {}
	critics = {}
	blacklist = ['i', 'a', 'an', 'the', 'at', 'it', 'in', 'this', 'that', 'of', 'to', 'and', 'is', 'are']
	url = re.compile('(http://[A-Za-z0-9\'~+\-=_.,/%\?!;:@#\*&\(\)]+)')

	for n in range(len(tweetlist01)):
		for word in tweetlist01[n].split(" "):
			wtweet01 = stem(word)
			urlsearch01 = url.search(wtweet01)
			if urlsearch01:
				print '# 01:Remove URL <<' + urlsearch01.group(1) + '>>'

			else:
				if wtweet01 in blacklist:
					print '# 01:Remove word of the blacklist'

				else:
					if wtweet01 in tweetdict01:
						tweetdict01[wtweet01] += 1.0
						print '# 01:Duplicate'

					else:
						tweetdict01.update({wtweet01.lower():1.0})

	critics.update({user01:tweetdict01})


	pearson = recommendations.sim_pearson(critics, user01, user02)

	euclidean = recommendations.sim_distance(critics, user01, user02)

	ep2 = ((euclidean * 1000) + pearson) / 2

	if self.check01.active:
		self.result = str(euclidean)
	elif self.check02.active:
		self.result = str(pearson)
	elif self.check04.active:
		self.result = str(ep2)

	print '# :END'

 

解説


 

結果

CNN*1 AP*2 Reuters*3 Eric Sammer*4 Josh Wills*5 Bill Gates*6 mousesports*7
CNN 1.000000000000 0.524004700100 0.814690178601 0.315645110740 0.490582946799 0.384108273122 0.353820682875
AP 0.524004700100 1.000000000000 0.541047886946 0.449697147993 0.524912288758 0.425655033649 0.469897475204
Reuters 0.814690178601 0.541047886946 1.000000000000 0.318366381375 0.689322402028 0.379282210463 0.329048595238
Eric Sammer 0.315645110740 0.449697147993 0.318366381375 1.000000000000 0.530130334260 0.490736605218 0.331352622561
Josh Wills 0.490582946799 0.524912288758 0.689322402028 0.530130334260 1.000000000000 0.554278641009 0.421179930456
Bill Gates 0.384108273122 0.425655033649 0.379282210463 0.490736605218 0.554278641009 1.000000000000 0.391850883238
mousesports 0.353820682875 0.469897475204 0.329048595238 0.331352622561 0.421179930456 0.391850883238 1.000000000000

 

考察


 

GUI

GUIはこのような感じになりました。
GUIの作成に際しては次の2つのフォントに加え、Kivyのロゴを使用させていただきました。

  • TempoFont
  • UpperWestSideFont

f:id:CORDEA:20140204004738j:plain,h400



 

*1:CNN Breaking News

*2:The Associated Press

*3:Reuters Top News

*4:Principal Solution Architect at Cloudera

*5:Data Scientist at Cloudera

*6:Chairman of Microsoft

*7:Professional gaming organization based out of Germany