# naive bayes

from __future__ import division
from fractions import Fraction
import os
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from flask import Flask, request
import pymongo
from pymongo import MongoClient
from flask import Flask, request, jsonify, abort
import json
from flask_pymongo import PyMongo

app = Flask(__name__)
app.config['MONGO_URI'] = 'mongodb://localhost:27017/category'

from googletrans import Translator
translator = Translator(service_urls=[
		'translate.google.com',
		'translate.google.co.kr',
	])

mongo = PyMongo(app)
def get_all_data():
	for i in mongo.db.category.find():
		print(i)

#Client = MongoClient()
#client = MongoClient('localhost', 27017)
#client = MongoClient('mongodb://localhost:27017')
#db = client.category
#posts = db.posts

path1, dirs1, files1 = next(os.walk("/home/redcliff/Naive_Bayes/Auto"))
path2, dirs2, files2 = next(os.walk("/home/redcliff/Naive_Bayes/computer"))
path3, dirs3, files3 = next(os.walk("/home/redcliff/Naive_Bayes/sports"))

f1 = open('/home/redcliff/Naive_Bayes/Auto/Doc1','r')
f2 = open('/home/redcliff/Naive_Bayes/Auto/Doc2','r')
f3 = open('/home/redcliff/Naive_Bayes/computer/Doc1','r')
#f4 = open('/home/redcliff/Naive_Bayes/computer/Doc2','r')
f5 = open('/home/redcliff/Naive_Bayes/sports/Doc1','r')
f6 = open('/home/redcliff/Naive_Bayes/sports/Doc2','r')

for fil in files1:
	with open('/home/redcliff/Naive_Bayes/Auto/{}'.format(fil), 'r') as f:
		j = f.read()                                                       ##   here error
		

file_count1 = len(files1)
file_count2 = len(files2)
file_count3 = len(files3)
total_files = file_count1+file_count2+file_count3
#print(file_count1 / total_files)
#print(file_count2 / total_files)
#print(file_count3 / total_files)

content1 = f1.read()
content2 = f2.read()
content3 = f3.read()
#content4 = f4.read()
content5 = f5.read()
content6 = f6.read()
content = (content6+content1+content2+content3+content5)
content_auto = (content1+content2)
content_computer = content3
content_sports = content5+content6
# for NLTK Stopwords
stop_words = set(stopwords.words('english'))
word_tokens = word_tokenize(content)
filtered_sentence = [w for w in word_tokens if not w in stop_words]

filtered_sentence = []
for w in word_tokens:
    if w not in stop_words:
        filtered_sentence.append(w)
#print(filtered_sentence)
 #for list to string
st = ' '.join(filtered_sentence)

def write(path, content):
	file_name = open(path,'w')
	stop_words = set(stopwords.words('english'))
	word_tokens = word_tokenize(content)
	filtered_sentence = [w for w in word_tokens if not w in stop_words]
	filtered_sentence = []
	for w in word_tokens:
		if w not in stop_words:
			filtered_sentence.append(w)
	#print(filtered_sentence)
	a = '\n'.join(filtered_sentence)
	file_name.write(a)	
	file_name.close()

write('/home/redcliff/Naive_Bayes/another/final',content)

# get all data
#for i in mongo.db.category.find({}):
	#print(i)


myquery = { "_id": "100", "Text":"this is ", "category":"auto" }
newvalues = {"$set": {"_id":"1", "Text":"this is", "category":"auto" }}

#insertt = mongo.db.category.insert_one(myquery)
#insertt.inserted_id


final1 = open('/home/redcliff/Naive_Bayes/another/final','r')
con = final1.read().split()
print("total word in every document %s " % (len(con)))

write('/home/redcliff/Naive_Bayes/another/Merge',content_auto)

word_count = {}
for k in con:
    if k not in word_count:
        word_count[k] = 1
    else:
        word_count[k] += 1
print("unique word from all documents is %s " % (len(word_count)))
#print(word_count)
final1_auto = open('/home/redcliff/Naive_Bayes/another/Merge','r')
auto_content = final1_auto.read().split()

#print("total no of word in auto %s " % (len(auto_content)))
auto_word_count = {}
for word in auto_content:
    if word not in auto_word_count:
        auto_word_count[word] = 1
    else :
        auto_word_count[word] += 1
print("unique word in auto is %s " % (len(auto_word_count))) #unique word from all Doc in Auto

write('/home/redcliff/Naive_Bayes/another/Merge2',content_computer)

final2_auto = open('/home/redcliff/Naive_Bayes/another/Merge2','r')
computer_content = final2_auto.read().split()
#print("total no of word in auto %s " % (len(auto_content)))
computer_word_count = {}
for word in computer_content:
    if word not in computer_word_count:
        computer_word_count[word] = 1
    else :
        computer_word_count[word] += 1

write('/home/redcliff/Naive_Bayes/another/Merge3',content_sports)


final3_auto = open('/home/redcliff/Naive_Bayes/another/Merge3','r')
sports_content = final3_auto.read().split()
#print("total no of word in auto %s " % (len(auto_content)))
sports_word_count = {}
for word in sports_content:
    if word not in sports_word_count:
        sports_word_count[word] = 1
    else :
        sports_word_count[word] += 1

print("........word count %s" % len(word_count))
#print(sports_word_count)

def main(w,name_word_count,name_content,lis):

	if w in name_word_count.keys():
		for k4,v4 in name_word_count.items():
			if k4 == w:
				#print("v4 is %s" % v4)
				#print(len(name_content),len(word_count),v4)
				#print("occurance of %s is %s" % (w,v4))
				b = ((v4+1) / ((len(name_content))+(len(word_count))))
				#print("b is %s " % b)
				lis.append(b)
	else:
		v4 = 0
		#print("v4 is %s " % v4)
		#print("occurance of %s is %s" % (w,v4))
		b = ((v4+1) / ((len(name_content))+(len(word_count))))
		#print("b is %s " % b)
		lis.append(b)

#######
dic = {}
total2 = []
total3 = []
total1 = []
total = []
#from file_test import b
@app.route('/home', methods=['POST'])
def home():
	payload = request.get_json()
	a = jsonify(payload)
	b = json.dumps(payload)   ## unhashble error : thus why .items() 
	dic.update(payload.items())

	mongo.db.category.insert(dic)
	c = dic['Text']
	string = (c.encode("utf-8")).split()
	
	total1 = []
	total2 = []
	total3 = []
	#string = 'car auto gear '

	total1 = []
	for k in range(0,len(string)):
		main(string[k],sports_word_count,sports_content,total1) 

	total2 = []
	for j in range(0,len(string)):
		main(string[j],computer_word_count,computer_content,total2) 

	total3 = []
	for i in range(0,len(string)):
		main(string[i],auto_word_count,auto_content,total3)


	r3 = 0.2
	# for multiplication from list
	for i in range(0,len(total3)): # auto
		r3 = r3 * total3[i]
	print("auto %s" % r3)
	r2 = 0.4
	for i in range(0,len(total2)): # computer
		r2 = r2 * total2[i]
	print("computer %s " % r2)
	r1 = 0.2
	for i in range(0,len(total1)): # sport
		r1 = r1 * total1[i]
	print("sport %s " % r1)
	

	maximum = (max(r1,r2,r3))
	print(maximum)
	
	
	
	if [r1,r2,r3].index(max(r1,r2,r3)) == 0:
		#result_word = "sports"
		return 'sports'
	elif [r1,r2,r3].index(max(r1,r2,r3)) == 1:
	#	result_word = "computer"
		return 'computer'
	else:
	#	result_word = "auto"
		return 'auto'

	#translations = translator.translate([result_word], dest='hi')
	#for translation in translations:
	#	print(translation.text)

	print("...............................................................")

	#print(mongo.db.category.find().count())
	#print(b)
	#return jsonify(translation.text)
	#return jsonify([sport,computer,auto].index(maximum))
	return 'ok'

#posts.insert_one(s)



# for the accuracy : ( predicted label's ans / total of all of 3's ans ) it will give in % form so bigest one is the category
# multinomial naive bayes 
# we can get comment from twitter/facebook and get to know that what is it about ?

f1.close()
f2.close()
f3.close()
#f4.close()
f5.close()
f6.close()

if __name__=="__main__":
   app.run()




