<commit_before>package alexwilton.handwritingAssistant;


import alexwilton.handwritingAssistant.exercises.Exercise;

import com.myscript.cloud.sample.ws.api.*;
import com.myscript.cloud.sample.ws.api.Stroke;

import java.util.*;
import java.util.concurrent.ConcurrentLinkedQueue;

public class StrokeAnalyser {
    private Exercise exercise;
    private MyScriptConnection myScriptConnection;
    private ConcurrentLinkedQueue<Stroke> incomingStrokes = new ConcurrentLinkedQueue<>();
    private Canvas canvas;
    private int wordSeperatingDistance = 30;
    private List<Word> recognisedWords = new ArrayList<>();

    public StrokeAnalyser(Exercise exercise, MyScriptConnection myScriptConnection) {
        this.exercise = exercise;
        this.myScriptConnection = myScriptConnection;
    }

    public void addStroke(Stroke stroke){
        incomingStrokes.add(stroke);
    }


    private void highlightExerciseMistakes(){
        String target = exercise.getTextToCopy();
        String[] targetWords = target.split(" ");
        int targetCount = targetWords.length;
        HashSet<Word> wordsToHighLight = new HashSet<>();
        for(int i=0; i<recognisedWords.size() && i<targetCount; i++){
            Word writtenWord = recognisedWords.get(i);
            String targetWord = targetWords[i].replaceAll("[^a-zA-Z]", "").toLowerCase(); //remove not letters from comparison.
            if(!targetWord.equals(writtenWord.getText())) wordsToHighLight.add(writtenWord);
        }
        exercise.setHighlightedWords(wordsToHighLight);
        canvas.repaint();
    }

    public void analyseStrokes(){
//        Stroke[] strokes = incomingStrokes.toArray(new Stroke[]{});
//        for(int startIndex=0; startIndex<strokes.length; startIndex++){
//            for(int endIndex=startIndex; endIndex<strokes.length; endIndex++){
//                int numOfStrokes = endIndex - startIndex + 1;
//                Stroke[] strokesToAnalyse = new Stroke[numOfStrokes];
//                System.arraycopy(strokes, startIndex, strokesToAnalyse, 0, numOfStrokes);
//                analyseStroke(strokesToAnalyse);
//            }
//        }
        ArrayList<ArrayList<Stroke>> strokeSetList = divideStrokesIntoWords();
        for(ArrayList<Stroke> wordStrokes : strokeSetList){
            final Stroke[] sArray = wordStrokes.toArray(new Stroke[wordStrokes.size()]);
            try {
                myScriptConnection.recognizeStrokes(sArray, new MyScriptConnection.MessageHandler() {
                    @Override
                    public void handleMessage(String json) {
                        String text = myScriptConnection.getTextOutputResult(json);
                        System.out.println("Text: " + text);
                        synchronized (this) {
                            recognisedWords.add(extractWordFromStrokes(sArray, text));
                            highlightExerciseMistakes();
                        }
                    }<commit_msg>ignore non letters from comparsion<commit_after>package alexwilton.handwritingAssistant;


import alexwilton.handwritingAssistant.exercises.Exercise;

import com.myscript.cloud.sample.ws.api.*;
import com.myscript.cloud.sample.ws.api.Stroke;

import java.util.*;
import java.util.concurrent.ConcurrentLinkedQueue;

public class StrokeAnalyser {
    private Exercise exercise;
    private MyScriptConnection myScriptConnection;
    private ConcurrentLinkedQueue<Stroke> incomingStrokes = new ConcurrentLinkedQueue<>();
    private Canvas canvas;
    private int wordSeperatingDistance = 30;
    private List<Word> recognisedWords = new ArrayList<>();

    public StrokeAnalyser(Exercise exercise, MyScriptConnection myScriptConnection) {
        this.exercise = exercise;
        this.myScriptConnection = myScriptConnection;
    }

    public void addStroke(Stroke stroke){
        incomingStrokes.add(stroke);
    }


    private void highlightExerciseMistakes(){
        String target = exercise.getTextToCopy();
        String[] targetWords = target.split(" ");
        int targetCount = targetWords.length;
        HashSet<Word> wordsToHighLight = new HashSet<>();
        for(int i=0; i<recognisedWords.size() && i<targetCount; i++){
            String writtenWord = recognisedWords.get(i).getText().replaceAll("[^a-zA-Z]", "").toLowerCase();
            String targetWord = targetWords[i].replaceAll("[^a-zA-Z]", "").toLowerCase(); //remove not letters from comparison.
            if(!targetWord.equals(writtenWord)) wordsToHighLight.add(recognisedWords.get(i));
        }
        exercise.setHighlightedWords(wordsToHighLight);
        canvas.repaint();
    }

    public void analyseStrokes(){
//        Stroke[] strokes = incomingStrokes.toArray(new Stroke[]{});
//        for(int startIndex=0; startIndex<strokes.length; startIndex++){
//            for(int endIndex=startIndex; endIndex<strokes.length; endIndex++){
//                int numOfStrokes = endIndex - startIndex + 1;
//                Stroke[] strokesToAnalyse = new Stroke[numOfStrokes];
//                System.arraycopy(strokes, startIndex, strokesToAnalyse, 0, numOfStrokes);
//                analyseStroke(strokesToAnalyse);
//            }
//        }
        ArrayList<ArrayList<Stroke>> strokeSetList = divideStrokesIntoWords();
        for(ArrayList<Stroke> wordStrokes : strokeSetList){
            final Stroke[] sArray = wordStrokes.toArray(new Stroke[wordStrokes.size()]);
            try {
                myScriptConnection.recognizeStrokes(sArray, new MyScriptConnection.MessageHandler() {
                    @Override
                    public void handleMessage(String json) {
                        String text = myScriptConnection.getTextOutputResult(json);
                        System.out.println("Text: " + text);
                        synchronized (this) {
                            recognisedWords.add(extractWordFromStrokes(sArray, text));
                            highlightExerciseMistakes();
                        }
                    }
{'fraction_non_alphanumeric': 0.10553346587143804, 'fraction_numerical': 0.0019880715705765406, 'mean_word_length': 2.638939119951778, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4551586', 'n_tokens_mistral': 1650, 'n_tokens_neox': 1580, 'n_words': 303}