<commit_before># -*- coding: utf-8 -*-
"""
Views.py
Created on Fri Jun 13 13:42:00 2014

@author: ahna
"""
###################################################################
# set up
import urllib
import pickle
import wikipedia
import pandas.io.sql as psql
import pymysql
from flask import render_template, request, json, Response
import app
from app import app, host, port, user, passwd, db
from app.database import *
from app import scrapeWikipedia as sw
from os import chdir, getcwd
import numpy as np

###################################################################
#configFileName = '/home/ubuntu/wikiphilia/app/settings/development.cfg'
#configFileName = '/Users/ahna/Documents/Work/insightdatascience/project/wikiphilia/webapp/app/settings/development.cfg'
#debug, host, port, user, passwd, dbname, localpath = grabDatabaseSettingsFromCfgFile(configFileName)
configFileName = 'app/settings/development.cfg'

###################################################################
# MAIN PAGE WITH SEARCH BOX
@app.route('/')
def index():
    return render_template('index.html',text='')     # Renders index.html
   
###################################################################
# SEARCH RESULTS
@app.route('/out', methods=['POST','GET'])
def out():
    searchPhrase = request.form['searchPhrase']
    wikiscore, searchPhraseDF = getWikiScore(searchPhrase)
    print "wikiscore is " + str(wikiscore)
    if wikiscore is None:
        "here"
        return render_template('index.html',text='Please try again')
        
    svgtxt = genSvg(searchPhrase, searchPhraseDF)
    return render_template('vis.html', searchPhrase=searchPhraseDF['title'][0], wikiscore=wikiscore, svgtxt=svgtxt, url=searchPhraseDF['url'][0])


###################################################################
# GET WIKISCORE FOR THE SEARCH PHRASE
def getWikiScore(searchPhrase):
    
    # get wikipedia search results ####################################
    print searchPhrase    
    searchRes = wikipedia.search(searchPhrase, results=3, suggestion=False)
    print searchRes
    if len(searchRes) < 1:
        print "Sorry. We didn't find any results for you. Please try a new search."
        return None, searchRes
  
    # connect to database ###################################################
    con = conDB(host,db,passwd=<PASSWORD>,port=port, user=user)

    # first check to see if search phrase results are already in database ############
    bInDB = False 
    for i in range(len(searchRes)):
#        print i, searchRes[i]
        sql = '''SELECT * FROM testing2 WHERE title="%s"'''%searchRes[i]
        searchPhraseDF = psql.frame_query(sql, con)
        if len(searchPhraseDF['id']) > 0:
            bInDB = True
            searchResultUse = searchRes[i]
            print("Found " + searchResultUse +" testing2 database")<commit_msg>commented out troublesome print statement<commit_after># -*- coding: utf-8 -*-
"""
Views.py
Created on Fri Jun 13 13:42:00 2014

@author: ahna
"""
###################################################################
# set up
import urllib
import pickle
import wikipedia
import pandas.io.sql as psql
import pymysql
from flask import render_template, request, json, Response
import app
from app import app, host, port, user, passwd, db
from app.database import *
from app import scrapeWikipedia as sw
from os import chdir, getcwd
import numpy as np

###################################################################
#configFileName = '/home/ubuntu/wikiphilia/app/settings/development.cfg'
#configFileName = '/Users/ahna/Documents/Work/insightdatascience/project/wikiphilia/webapp/app/settings/development.cfg'
#debug, host, port, user, passwd, dbname, localpath = grabDatabaseSettingsFromCfgFile(configFileName)
configFileName = 'app/settings/development.cfg'

###################################################################
# MAIN PAGE WITH SEARCH BOX
@app.route('/')
def index():
    return render_template('index.html',text='')     # Renders index.html
   
###################################################################
# SEARCH RESULTS
@app.route('/out', methods=['POST','GET'])
def out():
    searchPhrase = request.form['searchPhrase']
    wikiscore, searchPhraseDF = getWikiScore(searchPhrase)
    print "wikiscore is " + str(wikiscore)
    if wikiscore is None:
        "here"
        return render_template('index.html',text='Please try again')
        
    svgtxt = genSvg(searchPhrase, searchPhraseDF)
    return render_template('vis.html', searchPhrase=searchPhraseDF['title'][0], wikiscore=wikiscore, svgtxt=svgtxt, url=searchPhraseDF['url'][0])


###################################################################
# GET WIKISCORE FOR THE SEARCH PHRASE
def getWikiScore(searchPhrase):
    
    # get wikipedia search results ####################################
    print searchPhrase    
    searchRes = wikipedia.search(searchPhrase, results=3, suggestion=False)
    print searchRes
    if len(searchRes) < 1:
        print "Sorry. We didn't find any results for you. Please try a new search."
        return None, searchRes
  
    # connect to database ###################################################
    con = conDB(host,db,passwd=<PASSWORD>,port=port, user=user)

    # first check to see if search phrase results are already in database ############
    bInDB = False 
    for i in range(len(searchRes)):
#        print i, searchRes[i]
        sql = '''SELECT * FROM testing2 WHERE title="%s"'''%searchRes[i]
        searchPhraseDF = psql.frame_query(sql, con)
        if len(searchPhraseDF['id']) > 0:
            bInDB = True
            searchResultUse = searchRes[i]
            #print("Found " + searchResultUse +" testing2 database")
{'fraction_non_alphanumeric': 0.2452301767897777, 'fraction_numerical': 0.007001575354454752, 'mean_word_length': 4.685572139303482, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '47473', 'n_tokens_mistral': 1674, 'n_tokens_neox': 1564, 'n_words': 421}