#!/usr/bin/env awk -f BEGIN { # Initialize stop words split("the and of to a in is that it on for as was with at by an be this are from or can your layout", words) for (i in words) stopwords[tolower(words[i])] = 1 # Track the query query = tolower(ENVIRON["QUERY"]) patsplit(query, query_tokens, /[a-zA-Z0-9]+/) for (i in query_tokens) { if (!stopwords[query_tokens[i]]) { query_weights[query_tokens[i]]++ } } # Configuration: How many lines per chunk lines_per_chunk = 5 current_chunk = "" } { # Accumulate lines into a chunk current_chunk = current_chunk " " $0 # Process the chunk once we hit our line limit or reach the End-of-File if (NR % lines_per_chunk == 0) { process_chunk(current_chunk) current_chunk = "" } } # Catch any remaining text at the very end of the file END { if (current_chunk != "") { process_chunk(current_chunk) } # Output the absolute best match found if (max_score == 0) { print "Context: (No highly relevant context found in documents.)" } else { print "Context: " best_paragraph } print "Question: " ENVIRON["QUERY"] print "Answer: " } function process_chunk(paragraph) { gsub(/[ \t\r\n]+/, " ", paragraph) # Clean spacing score = 0 patsplit(tolower(paragraph), p_tokens, /[a-zA-Z0-9]+/) delete tf for (i in p_tokens) { if (!stopwords[p_tokens[i]]) tf[p_tokens[i]]++ } for (token in query_weights) { if (token in tf) { score += query_weights[token] * tf[token] } } if (score > max_score) { max_score = score best_paragraph = paragraph } }