2022-06-17 12:06:51 +00:00
|
|
|
from django.shortcuts import render
|
|
|
|
from .forms import searchForm
|
|
|
|
from pathlib import Path
|
|
|
|
import os
|
|
|
|
import pandas as pd
|
|
|
|
from .similarity import phrase_preprocessing, similarityCheck
|
|
|
|
from .tfidfSimilarity import cosine_similarity
|
2022-06-27 13:48:56 +00:00
|
|
|
import json
|
2022-06-17 12:06:51 +00:00
|
|
|
from django.shortcuts import redirect
|
|
|
|
|
|
|
|
from django.core.paginator import Paginator
|
|
|
|
|
|
|
|
from.models import Standards, Level, SearchResults
|
|
|
|
|
|
|
|
from .filters import StandardsFilter
|
|
|
|
|
|
|
|
import uuid
|
|
|
|
from datetime import date, timedelta
|
2022-06-27 13:48:56 +00:00
|
|
|
|
|
|
|
from django.core import serializers
|
2022-06-17 12:06:51 +00:00
|
|
|
# Create your views here.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def home(request):
|
|
|
|
|
|
|
|
levels = list(range(1, 7))
|
|
|
|
BASE_DIR = Path(__file__).resolve().parent.parent
|
|
|
|
path = os.path.join(BASE_DIR, 'App/static/IDDRSStandards/')
|
|
|
|
df = pd.DataFrame(columns=["Level","FileNumber","FileName","Summary","FilePath"])
|
|
|
|
all_standards = Standards.objects.all()
|
|
|
|
i = 0
|
|
|
|
# list_of_standardas = []
|
|
|
|
for level in os.listdir(path):
|
|
|
|
for file in os.listdir(path+'/'+level):
|
|
|
|
df.loc[i, 'Level'] = level
|
|
|
|
df.loc[i, 'FileName'] = file
|
|
|
|
i+= 1
|
|
|
|
|
|
|
|
args = {'all_standards': all_standards,'navbar': 'home','levels':levels}
|
|
|
|
return render(request, 'home.html', args)
|
|
|
|
|
|
|
|
def how_to(request):
|
|
|
|
return render(request, 'how_to.html', {'navbar': 'how_to'})
|
|
|
|
|
|
|
|
def search(request):
|
|
|
|
BASE_DIR = Path(__file__).resolve().parent.parent
|
|
|
|
|
|
|
|
preprocessedData = pd.read_json(os.path.join(BASE_DIR, 'App/static/json/processedIDDRS.json'))
|
|
|
|
phrase = ''
|
|
|
|
results = []
|
|
|
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
form = searchForm(request.POST)
|
|
|
|
if form.is_valid():
|
|
|
|
phrase = form.cleaned_data["phrase"]
|
|
|
|
preprocessedPhrase = phrase_preprocessing(phrase)
|
|
|
|
results = similarityCheck(preprocessedData, preprocessedPhrase)
|
|
|
|
|
|
|
|
else:
|
|
|
|
form = searchForm()
|
|
|
|
|
|
|
|
args = {'form': form, 'phrase': phrase, 'navbar': 'search', 'results':results}
|
|
|
|
return render(request, 'search.html', args)
|
|
|
|
|
|
|
|
def faq(request):
|
|
|
|
return render(request, 'faq.html', {'navbar': 'faq'})
|
|
|
|
|
|
|
|
|
|
|
|
def tfidf(request):
|
|
|
|
SearchResults.objects.filter(session_expiry_date=date.today()).delete()
|
|
|
|
|
|
|
|
searchResults = []
|
|
|
|
results = []
|
|
|
|
categories = Level.objects.all()
|
|
|
|
standards = Standards.objects.all()
|
|
|
|
myFilter = StandardsFilter()
|
|
|
|
selectedChoices = request.GET.getlist('title','')
|
|
|
|
phrase = request.GET.get('phrase','')
|
|
|
|
|
|
|
|
if request.method == 'GET':
|
|
|
|
|
|
|
|
form = searchForm(request.GET)
|
|
|
|
if form.is_valid() or 'searchedPhrase' in request.GET:
|
|
|
|
if 'searchedPhrase' in request.GET:
|
|
|
|
phrase = request.GET.get('searchedPhrase')
|
|
|
|
else:
|
|
|
|
phrase = form.cleaned_data["phrase"]
|
|
|
|
|
2022-06-27 13:48:56 +00:00
|
|
|
searchResults = cosine_similarity(30, phrase)
|
2022-06-17 12:06:51 +00:00
|
|
|
uid = str(uuid.uuid1())[:8]+phrase
|
|
|
|
for result in searchResults:
|
|
|
|
searchedData = SearchResults(
|
|
|
|
session_key = request.session.session_key,
|
|
|
|
uniqueID = uid,
|
|
|
|
session_expiry_date = date.today() + timedelta(days=1),
|
|
|
|
index = result['Index'],
|
|
|
|
level = result['Level'],
|
|
|
|
levelName = result['LevelName'],
|
|
|
|
title = result['Title'],
|
|
|
|
paragraph = result['Paragraph'],
|
|
|
|
color = result['Color'],
|
|
|
|
module = result['Module'],
|
|
|
|
heading1 = result['Heading1'],
|
|
|
|
heading2 = result['Heading2'],
|
|
|
|
heading3 = result['Heading3'],
|
|
|
|
heading4 = result['Heading4'],
|
|
|
|
pageNumber = result['PageNum'],
|
|
|
|
sentence = result['Sentence']
|
|
|
|
)
|
|
|
|
searchedData.save()
|
|
|
|
|
|
|
|
|
|
|
|
results = SearchResults.objects.filter(uniqueID=uid)
|
|
|
|
|
2022-06-27 13:48:56 +00:00
|
|
|
|
2022-06-17 12:06:51 +00:00
|
|
|
if selectedChoices:
|
|
|
|
results = SearchResults.objects.filter(uniqueID=uid)
|
|
|
|
myFilter = StandardsFilter(request.GET, queryset=results)
|
|
|
|
results = myFilter.qs
|
|
|
|
|
|
|
|
else:
|
|
|
|
form = searchForm()
|
|
|
|
|
|
|
|
# Create pagination for results
|
2022-06-27 13:48:56 +00:00
|
|
|
paginator = Paginator(results, per_page=10)
|
2022-06-17 12:06:51 +00:00
|
|
|
pageNumber = request.GET.get('page', 1)
|
|
|
|
page_obj = paginator.get_page(pageNumber)
|
|
|
|
|
|
|
|
args = {'form': form, 'phrase': phrase, 'navbar': 'tfidf',
|
|
|
|
'results':page_obj.object_list, 'levels':categories, 'myFilter':myFilter,
|
|
|
|
'standards':standards, 'selectedChoices':selectedChoices,
|
|
|
|
'pages':paginator, 'pNumber':int(pageNumber)}
|
|
|
|
|
|
|
|
return render(request, 'tfidf.html', args)
|
|
|
|
|
|
|
|
|
|
|
|
# def tfidf(request):
|
|
|
|
|
|
|
|
# searchResults = []
|
|
|
|
# results = []
|
|
|
|
# phrase = request.session['phrase']
|
|
|
|
# categories = Level.objects.all()
|
|
|
|
# standards = Standards.objects.all()
|
|
|
|
# myFilter = StandardsFilter()
|
|
|
|
# selectedChoices = request.GET.getlist('title','')
|
|
|
|
|
|
|
|
# if request.method == 'POST':
|
|
|
|
# # SearchResults.objects.filter(session_key=request.session.session_key).delete()
|
|
|
|
# selectedChoices = ''
|
|
|
|
# form = searchForm(request.POST)
|
|
|
|
# if form.is_valid():
|
|
|
|
# phrase = form.cleaned_data["phrase"]
|
|
|
|
# searchResults = cosine_similarity(20, phrase)
|
|
|
|
# request.session['phrase'] = phrase
|
|
|
|
# # uid = str(uuid.uuid1())
|
|
|
|
# uid = str(uuid.uuid1())[:8]+phrase
|
|
|
|
# for result in searchResults:
|
|
|
|
# searchedData = SearchResults(
|
|
|
|
# session_key = request.session.session_key,
|
|
|
|
# uniqueID = uid,
|
|
|
|
# session_expiry_date = date.today() + timedelta(days=1),
|
|
|
|
# index = result['Index'],
|
|
|
|
# level = result['Level'],
|
|
|
|
# levelName = result['LevelName'],
|
|
|
|
# title = result['Title'],
|
|
|
|
# paragraph = result['Paragraph'],
|
|
|
|
# color = result['Color'],
|
|
|
|
# module = result['Module'],
|
|
|
|
# heading1 = result['Heading1'],
|
|
|
|
# heading2 = result['Heading2'],
|
|
|
|
# heading3 = result['Heading3'],
|
|
|
|
# heading4 = result['Heading4'],
|
|
|
|
# pageNumber = result['PageNum'],
|
|
|
|
# sentence = result['Sentence']
|
|
|
|
# )
|
|
|
|
# searchedData.save()
|
|
|
|
# # update session
|
|
|
|
# if 'phrase' in request.session:
|
|
|
|
# phrase = request.session['phrase']
|
|
|
|
# else:
|
|
|
|
# request.session['phrase'] = phrase
|
|
|
|
|
|
|
|
# request.session['session_id'] = uid
|
|
|
|
# request.session.modified = True
|
|
|
|
|
|
|
|
# results = SearchResults.objects.filter(uniqueID=request.session['session_id'])
|
|
|
|
|
|
|
|
# else:
|
|
|
|
# form = searchForm()
|
|
|
|
|
|
|
|
# # Filter results
|
|
|
|
# if request.method == 'GET' and selectedChoices:
|
|
|
|
# results = SearchResults.objects.filter(uniqueID=request.session['session_id'])
|
|
|
|
# myFilter = StandardsFilter(request.GET, queryset=results)
|
|
|
|
# results = myFilter.qs
|
|
|
|
# else:
|
|
|
|
# results = SearchResults.objects.filter(uniqueID=request.session['session_id'])
|
|
|
|
|
|
|
|
|
|
|
|
# # Create pagination for results
|
|
|
|
# paginator = Paginator(results, per_page=5)
|
|
|
|
# pageNumber = request.GET.get('page', 1)
|
|
|
|
# page_obj = paginator.get_page(pageNumber)
|
|
|
|
|
|
|
|
# args = {'form': form, 'phrase': phrase, 'navbar': 'tfidf',
|
|
|
|
# 'results':page_obj.object_list, 'levels':categories, 'myFilter':myFilter,
|
|
|
|
# 'standards':standards, 'selectedChoices':selectedChoices,
|
|
|
|
# 'pages':paginator, 'pNumber':int(pageNumber)}
|
|
|
|
|
|
|
|
# return render(request, 'tfidf.html', args)
|
|
|
|
|
|
|
|
|
|
|
|
|