123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- '''
- description: testing prediction and speller matrix by writing freely on the console
- author: Alessandro Tonin
- date: 06.02.2020
- Copyright (c) 2020 Alessandro Tonin.
- All rights reserved.'''
- import importlib
- import logging
- import sys
- import time
- from random import randint
- import tkinter as tk
- import yaml
- # import pyaudio
- from colorlog import ColoredFormatter
- import aux
- from aux import log
- from paradigms import colorSpeller
- import multiprocessing as mp
- from multiprocessing import Array, Pipe
- import readchar
- import csv
- import re
- importlib.reload(colorSpeller)
- importlib.reload(aux)
- file_log = '/kiap/src/kiap_bci/tests/records_for_annotation_yml.yaml'
- file_in = '/kiap/src/kiap_bci/tests/user_test.txt'
- file_out = '/kiap/src/kiap_bci/tests/user_test_out_update_spcorpus_sp_us_gen.txt'
- sp = colorSpeller.ColorSpeller()
- update_corpus = True
- # # process yaml file to correctly format the spelled sentences
- # with open(file_log, 'r') as flog, open(file_in,'w') as fin:
- # entries = yaml.load(flog,Loader=yaml.Loader)
- # for k in entries.keys():
- # phrase = entries[k]['phrase']
- # init_phrase = entries[k]['phrase_start']
- # phrase = phrase.strip() #just in case...
- # if init_phrase and phrase.find(init_phrase) == 0:
- # phrase = '['+phrase[0:len(init_phrase)]+']'+phrase[len(init_phrase):]
- # fin.write(phrase+'\n')
- # txt file with one speller selection in each line
- # Read txt file line by line
- with open(file_in,'r+') as fin, open(file_out, 'w') as fout:
- results_writer = csv.writer(fout, delimiter=',')
- results_writer.writerow(['num', 'target', 'string', 'vocab1', 'vocab2', 'vocab3', 'vocab4', 'vocab5', 'pred corr','pred wrong','saved char'])
- line = fin.readline()
- cnt = 1
- while line:
- # Check initial string
- init_str_start = line.find('[')
- init_str_end = line.find(']')
- if init_str_start != init_str_end:
- curr_str = line[init_str_start+1:init_str_end]
- else:
- curr_str = ''
- # Delete initial string identifiers
- for r in (('[',''),(']',''),('\n','')):
- line = line.replace(*r)
- log.warning(f'target: {line}')
- sp.set_current_string(curr_str)
- sp.external_updates()
- vocab = sp.get_vocabulary(5)
- # fill empty vocabulary with dash
- while len(vocab) < 5:
- vocab.append('#')
- log.info(f'string: {curr_str}')
- log.info(f'vocab: {vocab}')
- results_writer.writerow([cnt,line,curr_str]+vocab)
-
- # use while loop instead of for in order to skip iterations when prediction is correct
- idx_letter = 0
- pred_correct = 0
- pred_wrong = 0
- saved_char = 0
- while idx_letter < len(line):
- # find the actual word
- word_start = line.rfind(' ',0,idx_letter)
- word_end = line.find(' ',idx_letter)
- if word_end == -1: word_end = len(line) # because if not find result is -1
- curr_word = line[word_start+1:word_end]
- # Check if there is a probable word and if it is correct
- if len(vocab)>0 and vocab[0].startswith('*') and vocab[0][1:-1] == curr_word:
- # in case add the probable word to the string
- curr_str = curr_str[:word_start+1] + curr_word
- pred_correct += 1
- saved_char += word_end-idx_letter
- if word_end < len(line):
- curr_str += ' '
- saved_char += 1
- elif len(vocab)>0 and vocab[0].startswith('*'):
- curr_str += line[idx_letter]
- pred_wrong += 1
- else:
- # Add to speller the selection letter by letter
- curr_str += line[idx_letter]
- # Keep trace of predicted words
-
- sp.set_current_string(curr_str)
- sp.external_updates()
- # Check prediction after every new letter
- vocab = sp.get_vocabulary(5)
- # fill empty vocabulary with dash
- while len(vocab) < 5:
- vocab.append('#')
- log.info(f'string: {curr_str}')
- log.info(f'vocab: {vocab}')
-
- idx_letter = len(curr_str)
- results_writer.writerow([cnt,line,curr_str]+vocab+[pred_correct,pred_wrong,saved_char])
- if update_corpus:
- sp._save_string()
- sp.speller_user_cfd = sp._createCFD(root=sp.corpora_path,corpus_name=sp.speller_user_corpus_name)
- line = fin.readline()
- cnt += 1
|