<commit_before>"""
Copyright (c) 2012 <NAME>, <NAME>

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""

import os, sys, pgdb, MySQLdb, re

'''
Application Data Acceess

@author: <NAME>, <NAME>
@date: Aug 3, 2012
'''
PG = 'PG'
MYSQL = 'MYSQL'
DB = PG
class ModelController:
	def __init__(self):
		if(DB==MYSQL):
			self.conn = MySQLdb.connect(host="mysql.abhardwaj.org", 
			user="_mysql_admin", passwd="<PASSWORD>", db="trish")
		elif(DB == PG):
			self.conn = pgdb.connect("localhost:trish:postgres:postgres") 
		self.cursor = self.conn.cursor()
		

	def find_post(self, post_id):
		try:
			stmt = "SELECT phone, post FROM posts WHERE id="+post_id		
			var = self.cursor.execute(stmt)			
			row = self.cursor.fetchone()
			return row	
		except:
			print "find_post: ", sys.exc_info()
			return None



	def insert_post(self, phone_num, post, zipcode):
		try:			
			rowid = -1;
			if(DB == MYSQL):
				self.cursor.execute("INSERT INTO posts (phone, post, zipcode) VALUES (%s, %s, %s)", (phone_num, post, zipcode))	
				self.conn.commit()
				rowid = self.cursor.lastrowid
			elif(DB == PG):
				self.cursor.execute("INSERT INTO posts (phone, post, zipcode) VALUES (%s, %s, %s) RETURNING id", (phone_num, post, zipcode))	
				self.conn.commit()
				rowid = self.cursor.fetchone()[0]
			return rowid
		except:
			self.conn.rollback()
			print "insert_post: ", sys.exc_info()
			return -1

	
	def update_post(self, post_id, zipcode, msg):
		try:
			stmt = "UPDATE posts SET zipcode = '" +zipcode+ "', post = '" +msg+ "' WHERE id = " + post_id		
			self.cursor.execute(stmt)
			self.conn.commit()
			return True
		except:
			self.conn.rollback()
			print "update_post: ", sys.exc_info()
			return False
			
	
			
	def delete_post(self, post_id):
		try:
			self.cursor.execute("DELETE FROM posts WHERE id="+ post_id)
			self.conn.commit();
			return True
		except:
			self.conn.rollback()
			print "delete_post: ", sys.exc_info()
			return False
		



	def search_posts(self, tag):
		try:
			data = None
			if(DB == MYSQL):	
				self.cursor.execute("SELECT post, id FROM posts WHERE MATCH (post) AGAINST('"+ tag+"') LIMIT 3")				
			elif(DB == PG):
				self.cursor.execute("SELECT post, id FROM posts WHERE to_tsvector('english', post) @@ plainto_tsquery('english', '"+ tag+"') LIMIT 3")
			data = self.cursor.fetchall()
			if(not data):
				return None<commit_msg>so, now we rank the search results, it's no more boolean search<commit_after>"""
Copyright (c) 2012 <NAME>, <NAME>

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""

import os, sys, pgdb, MySQLdb, re

'''
Application Data Acceess

@author: <NAME>, <NAME>
@date: Aug 3, 2012
'''
PG = 'PG'
MYSQL = 'MYSQL'
DB = PG
class ModelController:
	def __init__(self):
		if(DB==MYSQL):
			self.conn = MySQLdb.connect(host="mysql.abhardwaj.org", 
			user="_mysql_admin", passwd="<PASSWORD>", db="trish")
		elif(DB == PG):
			self.conn = pgdb.connect("localhost:trish:postgres:postgres") 
		self.cursor = self.conn.cursor()
		

	def find_post(self, post_id):
		try:
			stmt = "SELECT phone, post FROM posts WHERE id="+post_id		
			var = self.cursor.execute(stmt)			
			row = self.cursor.fetchone()
			return row	
		except:
			print "find_post: ", sys.exc_info()
			return None



	def insert_post(self, phone_num, post, zipcode):
		try:			
			rowid = -1;
			if(DB == MYSQL):
				self.cursor.execute("INSERT INTO posts (phone, post, zipcode) VALUES (%s, %s, %s)", (phone_num, post, zipcode))	
				self.conn.commit()
				rowid = self.cursor.lastrowid
			elif(DB == PG):
				self.cursor.execute("INSERT INTO posts (phone, post, zipcode) VALUES (%s, %s, %s) RETURNING id", (phone_num, post, zipcode))	
				self.conn.commit()
				rowid = self.cursor.fetchone()[0]
			return rowid
		except:
			self.conn.rollback()
			print "insert_post: ", sys.exc_info()
			return -1

	
	def update_post(self, post_id, zipcode, msg):
		try:
			stmt = "UPDATE posts SET zipcode = '" +zipcode+ "', post = '" +msg+ "' WHERE id = " + post_id		
			self.cursor.execute(stmt)
			self.conn.commit()
			return True
		except:
			self.conn.rollback()
			print "update_post: ", sys.exc_info()
			return False
			
	
			
	def delete_post(self, post_id):
		try:
			self.cursor.execute("DELETE FROM posts WHERE id="+ post_id)
			self.conn.commit();
			return True
		except:
			self.conn.rollback()
			print "delete_post: ", sys.exc_info()
			return False
		



	def search_posts(self, query):
		try:
			data = None
			if(DB == MYSQL):	
				self.cursor.execute("SELECT post, id FROM posts WHERE MATCH (post) AGAINST('"+ query+"') LIMIT 3")				
			elif(DB == PG):
				q = re.findall('\w+', query)
				q = map(lambda x: x.strip(), q)
				q = filter(lambda x: x!='' and x!=',', q)
				q = map(lambda x: x.lower(), q)
				q = '|'.join(q)				
				self.cursor.execute("SELECT post, id, ts_rank_cd(to_tsvector('english', post), query) as rank FROM posts, to_tsquery('english', '"+q+"') as query WHERE to_tsvector('english', post) @@ query ORDER BY rank DESC LIMIT 3")
			data = self.cursor.fetchall()
			if(not data):
				return None
{'fraction_non_alphanumeric': 0.12204893403920447, 'fraction_numerical': 0.00400629560738303, 'mean_word_length': 3.824016563146998, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4922948', 'n_tokens_mistral': 2574, 'n_tokens_neox': 2230, 'n_words': 790}