# -*-coding:utf-8 -*-
import fileinput
import re
import sys
import keywords_new
import jieba
import jieba.posseg as pseg
from collections import Counter

reload(sys)
sys.setdefaultencoding('utf-8')

file = open("3822047450", "r")

express = {}
a = {}
list1 = []
record = {}
name_set = {}

while 1:
	line = file.readline().strip().decode('utf-8')
	if not line:
		break
	ex_all = re.findall(u"\\[.*?\\]", line)
	if ex_all:
		print u"表情分析完成"
		for ex_item in ex_all:
			express[ex_item] = express.get(ex_item, 0) + 1
	else:
		print u"微博中不存在表情"
	for kw, keywords in keywords_new.keyword_dict.iteritems():
		flag = 0
		for key, keyword in keywords.iteritems():
			if flag == 1:
				break
			for word in keyword:
				match_flag = 1
				for small_word in word:
					match = re.search(re.compile(small_word, re.I), line)
					if not match:
						match_flag = 0
						break
					if match_flag == 1:
						record[kw] = record.get(kw, 0) + 1
						flag = 1
						break
	item = re.sub(u"\\[.*?\\]", '', line)
	list = jieba.cut(item, cut_all=False)
	for ll in list:
		list1.append(ll)
	seg_list = pseg.cut(item)
	for word, flag in seg_list:
		if flag == 'nr':
			name_set[word] = name_set.get(word, 0) + 1
			print word
			print name_set[word]

count = Counter(list1)
# for item in sorted(dict(count).iteritems(), key=lambda d: d[1], reverse=True):
# 	if len(item[0]) >= 2 and item[1] >= 3:
# 		print >> f3, item[0], item[1]
#
# for key, keywords in sorted(record.iteritems(), key=lambda d: d[1], reverse=True):
# 	print >> f1, u'命中了', key, record[key], u'次'
#
# for key, keywords in sorted(name_set.iteritems(), key=lambda d: d[1], reverse=True):
# 	print >> f4, u'使用了名字', key, name_set[key], u'次'

for key, keywords in sorted(express.iteritems(), key=lambda d: d[1], reverse=True):
	print >> f2, u'使用了', key, u'表情', express[key], u'次'








