|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import readings
|
|
|
|
from pprint import pprint
|
|
|
|
import re, sys, glob
|
|
|
|
from escpos import escpos
|
|
|
|
from sys import stdin, stderr, stdout
|
|
|
|
from time import sleep
|
|
|
|
|
|
|
|
import nltk
|
|
|
|
from nltk.tokenize import RegexpTokenizer
|
|
|
|
tokenizer = RegexpTokenizer(r'\w+') # initialize tokenizer
|
|
|
|
|
|
|
|
connected = True
|
|
|
|
|
|
|
|
def check_printer_path():
|
|
|
|
printers = glob.glob('/dev/usb/lp*')
|
|
|
|
if printers:
|
|
|
|
return printers[0]
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
def char_swap(some_string):
|
|
|
|
swaps = [('–','-'), ('“','"'),('”','"'),('ù','u'), ("’","'"), ('à','a'), ('â','a'),('é','e'),('è','e'),('î','i')]
|
|
|
|
for swap in swaps:
|
|
|
|
some_string = some_string.replace(swap[0], swap[1])
|
|
|
|
return some_string
|
|
|
|
|
|
|
|
def insert_linebreaks(string, linelength):
|
|
|
|
count = 0
|
|
|
|
tmp = ''
|
|
|
|
new = ''
|
|
|
|
for x, ch in enumerate(string):
|
|
|
|
tmp_ch = []
|
|
|
|
if ch == ' ':
|
|
|
|
count = count + 1
|
|
|
|
if count < linelength:
|
|
|
|
tmp += ' '
|
|
|
|
else:
|
|
|
|
new += tmp + '\n'
|
|
|
|
tmp = ''
|
|
|
|
count = 0
|
|
|
|
elif x+1 == len(string):
|
|
|
|
new += tmp + ch
|
|
|
|
else:
|
|
|
|
tmp += ch
|
|
|
|
count += 1
|
|
|
|
return new
|
|
|
|
|
|
|
|
def printNow(query, results):
|
|
|
|
if connected == True:
|
|
|
|
printerpath = check_printer_path()
|
|
|
|
try:
|
|
|
|
print_pointer = open(printerpath, 'w') if printerpath else sys.__stdout__
|
|
|
|
print(escpos['reset'], file=print_pointer, flush=True)
|
|
|
|
print(escpos['init_printer'], file=print_pointer, flush=True)
|
|
|
|
print(escpos['margin_left'], file=print_pointer, flush=True)
|
|
|
|
print(escpos['justify_center'], file=print_pointer, flush=True)
|
|
|
|
|
|
|
|
printed = False
|
|
|
|
results_count = 0
|
|
|
|
for x, document in results.items():
|
|
|
|
for n, sentence in enumerate(document['matches']):
|
|
|
|
if results_count <= 4:
|
|
|
|
results_count += 1
|
|
|
|
sentence = str(sentence) # Turn Flask Markup object back to normal python string
|
|
|
|
sentence = char_swap(sentence)
|
|
|
|
query = char_swap(query)
|
|
|
|
sentence = sentence.replace('\n', ' ').replace(' ', ' ')
|
|
|
|
pattern = r'[\s\W\_]'+query+r'[\s\W\_\n]|^'+query+'|'+query+'$'
|
|
|
|
match = re.search(pattern, sentence, flags=re.IGNORECASE)
|
|
|
|
if match:
|
|
|
|
match = match.group()
|
|
|
|
sentence_splitted = re.compile(match).split(sentence) # find the query in the sentence, and split the sentence up
|
|
|
|
for i, part in enumerate(sentence_splitted):
|
|
|
|
if n == 0: # first sentence of this document
|
|
|
|
if i == 0: # start of a new sentence
|
|
|
|
print('', file=print_pointer, flush=True)
|
|
|
|
linebreak = ''' **
|
|
|
|
** /** **
|
|
|
|
//** /** **
|
|
|
|
**************
|
|
|
|
///**//**//**/
|
|
|
|
** /** //**
|
|
|
|
// /** //
|
|
|
|
// '''
|
|
|
|
print(linebreak, file=print_pointer, flush=True) # print line break ascii art
|
|
|
|
print('\n', file=print_pointer, flush=True)
|
|
|
|
title = insert_linebreaks(document['name'], 13)
|
|
|
|
print(escpos['largefont'], title, escpos['normalfont'], file=print_pointer, flush=True) # print the document name
|
|
|
|
print('\n', file=print_pointer, flush=True)
|
|
|
|
|
|
|
|
# start of sentence
|
|
|
|
part = insert_linebreaks(part, 35)
|
|
|
|
print(part, file=print_pointer, flush=True) # print current part of sentence
|
|
|
|
|
|
|
|
if i + 1 != len(sentence_splitted): # if this part is not the last one
|
|
|
|
print(escpos['bold'], match, escpos['reset'], file=print_pointer, flush=True) # print query
|
|
|
|
|
|
|
|
# line breaker after each sentence
|
|
|
|
if i + 1 == len(sentence_splitted):
|
|
|
|
print('\n | \n', file=print_pointer, flush=True)
|
|
|
|
sleep(3)
|
|
|
|
|
|
|
|
end_line = insert_linebreaks('These are the first 5 results that the algorithm selected.', 35)
|
|
|
|
print("\n\n{}\n".format(end_line), file=print_pointer, flush=True)
|
|
|
|
print(escpos['paperfeed_1l'], file=print_pointer, flush=True)
|
|
|
|
print(escpos['paperfeed_1l'], file=print_pointer, flush=True)
|
|
|
|
print(escpos['paperfeed_1l'], file=print_pointer, flush=True)
|
|
|
|
sleep(3)
|
|
|
|
print(escpos['papercut'], file=print_pointer, flush=True)
|
|
|
|
print(escpos['reset'], file=print_pointer, flush=True)
|
|
|
|
|
|
|
|
|
|
|
|
except:
|
|
|
|
# print('test')
|
|
|
|
e = sys.exc_info()[0]
|
|
|
|
print("Could not print: {}".format(e))
|
|
|
|
|
|
|
|
# query = 'noise'
|
|
|
|
# results, _, _ = readings.request_results(query)
|
|
|
|
# pprint(results)
|
|
|
|
# printNow(query, results)
|