Skip to content

Commit

Permalink
Review text extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
rodfersou committed Jul 7, 2016
1 parent 0513414 commit bd3d7ea
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ Changelog
1.0b2 (unreleased)
------------------

- Review text extraction.
[rodfersou]

- To avoid displaying the 'Listen' button with an incorrect voice,
the feature is now globally disabled by default at installation time.
[hvelarde]
Expand Down
12 changes: 12 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,15 @@ A viewlet with a 'Listen' button will be displayed on objects with the feature e
The Text-To-Speech feature enabled.

You can pause/resume the reader at any time by selecting 'Pause'/'Resume'.

How does it work
----------------

This package looks for every text into the ``#content`` div, process it and send to `ResponsiveVoice <http://responsivevoice.org/>`_ to play the text.

The text processing is done in this way:

1. Looks text line by line.
2. Remove extra space.
3. Ignore invisible elements and empty lines.
4. Add ending period if it is missing.
25 changes: 23 additions & 2 deletions src/collective/texttospeech/static/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,28 @@ var MainView = (function() {
this.$button.html(this.label_stopped);
this.$button.attr('class', 'stopped');
};
MainView.prototype.extract_text = function() {
var i, len, ref, results, $el, text;
// create an array with the text extracted
results = [];
ref = $('#content > *');
for (i = 0, len = ref.length; i < len; i++) {
$el = $(ref[i]);
// Remove extra spaces
text = $el.text().replace(/\s+/g, ' ').trim();
// ignore invisible elements or empty lines
if (!$el.is(':visible') || text.length === 0) {
continue;
}
// ensure there is a pause after every line adding a period
if (!/[.,;:!?]$/.test(text)) {
text += '.';
}
results.push(text);
}
// join array with texts
return results.join(' ');
};
MainView.prototype.play_pause = function(e) {
e.preventDefault();
if (this.playing) {
Expand All @@ -39,8 +61,7 @@ var MainView = (function() {
}
} else {
responsiveVoice.speak(
// remove spaces to avoid issues with some Firefox versions
$('#content').text().replace(/\s+/g, ' ').trim(),
this.extract_text(),
this.voice, {
onstart: $.proxy(this.onstart, this),
onend: $.proxy(this.onend, this)
Expand Down

0 comments on commit bd3d7ea

Please sign in to comment.