0

currently I'm working on an app that uses the TTS to play some strings and while the TTS works, I have a method to highlight the word being spoken, but I have problems with Spannable to works with the question mark character.

If it has many question marks my app crashes. If it has only a question mark, Spannable highlight the sentence but not the question mark.

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void speakText(String text) {
    String regex = "[^.!?\\s][^.!?]*(?:[.!?](?!['\"]?\\s|$)[^.!?]*)*[.!?]?['\"]?(?=\\s|$)";
    Matcher matcher = Pattern.compile(regex, Pattern.MULTILINE | Pattern.COMMENTS).matcher(text);

    auxEnd = 0;
    if (!split.isEmpty()) split.clear();

    while (matcher.find()) {

        split.add(matcher.group()); // add split sentences by ". ? !"

        String utteranceId = this.hashCode() + "";
        tts.speak(matcher.group(), TextToSpeech.QUEUE_ADD, null, utteranceId);
    }
}

@Override
public void onInit(int i) {
    //...
    tts.setOnUtteranceProgressListener(new UtteranceProgressListener() {
        @Override
        public void onStart(String utteranceId) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    // highlight sentences while tts is working
                    highlight(textViewWordSentence, textView.getText().toString(), split.get(auxEnd));
                    auxEnd++;
                }
            });
        }

        @Override
        public void onDone(String utteranceId) {
            //...
        }

        @Override
        public void onError(String utteranceId) {
            //...
        }
    });
}

private void highlight(TextView textView, String fullText, String textToSearch) {
    SpannableString spannableString = new SpannableString(fullText);
    Matcher matcher = Pattern.compile(textToSearch).matcher(spannableString);

    while (matcher.find()) {
        spannableString.setSpan(new BackgroundColorSpan(Color.CYAN), matcher.start(), matcher.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    textView.setText(spannableString); // <-- if it has many question marks my app crashes. if it has only a question mark, the Spannable highlight the sentence less the question mark.
}

highlight method output
Hello are you there? <-- Spannable cannot highlight the question mark
Hello are you there? <-- I wish this result

Hello are you there??? <-- force close

I'm a beginner programmer and I really appreciate any help you can provide.

F4bioo
  • 179
  • 1
  • 5
  • 11
  • I think the question mark is a reserved symbol that you need to escape in your regex with "//?". You might look into Pattern.quote as well – ziondreamt Jul 30 '16 at 21:46
  • this regex is here [link](http://stackoverflow.com/a/5553924/5392773) regex is very hard to me yet :/ – F4bioo Jul 30 '16 at 22:44
  • I have very little experience with regex, but it looks like your link had "normal" sentences expected. according to the java doc http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html a single and double question mark are quantifiers, so I have to think that it's related. – ziondreamt Jul 31 '16 at 02:55

0 Answers0