diff --git a/3b8e5045-42db-4315-a9da-20ba22f7f162.wav b/3b8e5045-42db-4315-a9da-20ba22f7f162.wav new file mode 100644 index 00000000..c3035571 Binary files /dev/null and b/3b8e5045-42db-4315-a9da-20ba22f7f162.wav differ diff --git a/ComSemApp/corpus/views.py b/ComSemApp/corpus/views.py index 289afd45..8d08ca4b 100644 --- a/ComSemApp/corpus/views.py +++ b/ComSemApp/corpus/views.py @@ -19,11 +19,11 @@ def corpus_search(request): tags = Tag.objects.all() template = loader.get_template('ComSemApp/corpus/corpus_search_new.html') - return HttpResponse(template.render({'tags': tags, 'offsetRange':[i for i in range(-8,8)]}, request)) + return HttpResponse(template.render({'tags': tags, 'offsetRange':[i for i in range(-8,8+1)]}, request)) @login_required def populate_word_tag(request): - val = request.POST.get('val', None) + val = request.POST.get('val', None).lstrip(' ').rstrip(' ') search_type = request.POST.get('type', None) output = request.POST.get('output', None) @@ -60,13 +60,17 @@ def search_results(request): sequential_search = request.POST.get('searchType') == '1' search_criteria = request.POST.get('searchCriteria', None) - if not search_criteria: + if not search_criteria or search_criteria == "": return HttpResponse('No search criteria provided', status=401) search_criteria = json.loads(search_criteria) - query = build_query(len(search_criteria) - 1, search_criteria, sequential_search) - print("Query:", query) + for item in search_criteria: + if item['type'] == 'word' and " " in item['val'].rstrip().lstrip(): + return HttpResponse('Invalid input: one word only per entry'); + + query = build_query(search_criteria, sequential_search) + with connection.cursor() as cursor: expression_ids = [] cursor.execute(query) @@ -76,20 +80,6 @@ def search_results(request): # grab the information we want about the expressions expressions = Expression.objects.filter(id__in=expression_ids) - # for each expression, retag in order to show where the matching word / tag is. - # TODO - # for expression in expressions: - # tokens = nltk.word_tokenize(expression.expression) - # tagged = nltk.pos_tag(tokens) - # print (tagged) - # for criterion in search_criteria: - # print (criterion) - # if criterion['type'] == 'tag': - # tag = criterion['val'] - # for word in tagged: - # if word[1] == tag: - # print ("match") - context = { 'expressions': expressions, 'sequential_search': sequential_search, @@ -98,47 +88,49 @@ def search_results(request): template = loader.get_template('ComSemApp/corpus/search_results.html') return HttpResponse(template.render(context, request)) - -# work backwards through the search criteria - we make n - 1 joins (where n = number of search criteria) with n tables that -# select expression ID and position (if sequential search). -def build_query(i, search_criteria, sequential_search): - current_criteria = search_criteria[i] - criteria_type = current_criteria['type'] - val = current_criteria['val'] - id_list = current_criteria['id_list'] - - # if val isnt valid, id_list isn't a list of int ... - - if i < 0: +# This query builder makes the following assumptions about the search criteria: +# there is one word, either a tag or a second word, and there may be an offset. +def build_query(search_criteria, sequential_search): + words = [] + tags = [] + offset = 0 + for item in search_criteria: + if item['type'] == 'word': + words.append(item) + elif item['type'] == 'tag': + tags.append(item) + elif item['type'] == 'offset' and sequential_search == True: + offset = item['val'] + + if len(words) == 0: return "" - else: - if(criteria_type == "offset"): - print ("to do") - - select_position = ", SW.Position" if sequential_search else "" - from_words = ", ComSemApp_word as W " if criteria_type == "tag" else "" - - query = "SELECT SW.expression_id" + select_position + " FROM ComSemApp_sequentialwords AS SW" + from_words - if i > 0: - query += ", (" + build_query(i - 1, search_criteria, sequential_search) + ") as Derived" + str(i) - - query += " WHERE " - - if criteria_type == "tag": - query += " SW.word_id = W.id AND W.tag_id in (" + ','.join([str(id) for id in id_list]) + ") " + query = "SELECT SW.expression_id" + if sequential_search: + query += ", SW.position" + query += " FROM ComSemApp_sequentialwords as SW" + + if len(words) > 1 or len(tags) > 0: + query += ", (SELECT SW2.expression_id" + if sequential_search: + query += ", SW2.position" + query += " from ComSemApp_sequentialwords as SW2" + if len(tags) > 0: + query += ", ComSemApp_word as W where W.tag_id in (" + ','.join([str(id) for id in tags[0]['id_list']]) + query += ") and SW2.word_id = W.id" else: - query += " SW.word_id in (" + ','.join([str(id) for id in id_list]) + ") " - - if i > 0: - if sequential_search: - next_position = 1 + query += " where SW2.word_id in (" + ','.join([str(id) for id in words[1]['id_list']]) + query += ")" + query += ") as derived2" + query += " where SW.word_id in (" + ','.join([str(id) for id in words[0]['id_list']]) + query += ")" - # if the next search criteria is an offset, we'll use it here then skip it in the next call. - if search_criteria[i-1]['type'] == 'offset': - next_position += search_criteria[i-1]['val'] + if len(words) > 1 or len(tags) > 0: + query += " and SW.expression_id = derived2.expression_id" - query += "AND SW.position = (Derived" + str(i) + ".position + " + str(next_position) + ") " + if offset > 0: + query += " and derived2.position <= (SW.position + " + str(offset) + ") and SW.position < derived2.position" + elif offset < 0: + query += " and SW.position <= (derived2.position + " + str(abs(offset)) + ") and derived2.position < SW.position" - query += "AND SW.expression_id = Derived" + str(i) + ".expression_id " - return query + return query diff --git a/ComSemApp/static/ComSemApp/images/google_logo.png b/ComSemApp/static/ComSemApp/images/google_logo.png new file mode 100644 index 00000000..8a4c8a88 Binary files /dev/null and b/ComSemApp/static/ComSemApp/images/google_logo.png differ diff --git a/ComSemApp/student/views.py b/ComSemApp/student/views.py index 58085058..d79d7a5a 100644 --- a/ComSemApp/student/views.py +++ b/ComSemApp/student/views.py @@ -17,6 +17,16 @@ from ComSemApp.models import * from ComSemApp.libs.mixins import RoleViewMixin, CourseViewMixin, WorksheetViewMixin, SubmissionViewMixin +# Imports the Google Cloud client library +from google.cloud import speech +from google.cloud.speech import enums +from google.cloud.speech import types + +# googleClient = speech.SpeechClient() + +def TranscribeAudio(request): + + return class StudentViewMixin(RoleViewMixin): @@ -193,6 +203,7 @@ def get_context_data(self, **kwargs): context = super(AttemptCreateView, self).get_context_data(**kwargs) # TODO - expression mixin rather than grabbing expression twice ? expression_id = self.kwargs.get('expression_id') + expression = get_object_or_404(Expression, id=expression_id, worksheet=self.worksheet) context['expression'] = expression return context diff --git a/ComSemApp/templates/ComSemApp/corpus/corpus_search_new.html b/ComSemApp/templates/ComSemApp/corpus/corpus_search_new.html index 7c6a27e2..30bcd673 100644 --- a/ComSemApp/templates/ComSemApp/corpus/corpus_search_new.html +++ b/ComSemApp/templates/ComSemApp/corpus/corpus_search_new.html @@ -59,40 +59,50 @@
Please enter a word.
+Please enter a word.
"); - return; + if (resultField == "#DynamicField2") { + val = "ALL"; + type = 'tag'; + } + else { + $(resultField).empty(); + $(resultField).html("Please enter a word.
"); + return; + } } $(resultField).empty(); // clear the result field, prep for new results @@ -194,8 +229,9 @@