function parseBookFile (filePath) {
// Read text file
const book = fs.readFileSync(filePath, 'utf8')

// Find book title and author
const title = book.match(/^Title:\s(.+)$/m)[1]
const authorMatch = book.match(/^Author:\s(.+)$/m)
const author = (!authorMatch || authorMatch[1].trim() === '') ? 'Unknown Author' : authorMatch[1]

console.log(`Reading Book - ${title} By ${author}`)

// Find Guttenberg metadata header and footer
const startOfBookMatch = book.match(/^\*{3}\s*START OF (THIS|THE) PROJECT GUTENBERG EBOOK.+\*{3}$/m)
const startOfBookIndex = startOfBookMatch.index + startOfBookMatch[0].length
const endOfBookIndex = book.match(/^\*{3}\s*END OF (THIS|THE) PROJECT GUTENBERG EBOOK.+\*{3}$/m).index

// Clean book text and split into array of paragraphs
const paragraphs = book
.slice(startOfBookIndex, endOfBookIndex) // Remove Guttenberg header and footer
.split(/\n\s+\n/g) // Split each paragraph into it's own array entry
.map(line => line.replace(/\r\n/g, ' ').trim()) // Remove paragraph line breaks and whitespace
.map(line => line.replace(/_/g, '')) // Guttenberg uses "_" to signify italics.  We'll remove it, since it makes the raw text look messy.
.filter((line) => (line && line.length !== '')) // Remove empty lines

console.log(`Parsed ${paragraphs.length} Paragraphs\n`)
return { title, author, paragraphs }
}
/** Read an individual book text file, and extract the title, author, and paragraphs */
Read an individual book text file, and extract the title, author, and paragraphs
Read an individual book text file and extract the title author and paragraphs
function parseBookFile (filePath) {
const book = fs.readFileSync(filePath, 'utf8')
const title = book.match(/^Title:\s(.+)$/m)[1]
const authorMatch = book.match(/^Author:\s(.+)$/m)
const author = (!authorMatch || authorMatch[1].trim() === '') ? 'Unknown Author' : authorMatch[1]
console.log(`Reading Book - ${title} By ${author}`)
const startOfBookMatch = book.match(/^\*{3}\s*START OF (THIS|THE) PROJECT GUTENBERG EBOOK.+\*{3}$/m)
const startOfBookIndex = startOfBookMatch.index + startOfBookMatch[0].length
const endOfBookIndex = book.match(/^\*{3}\s*END OF (THIS|THE) PROJECT GUTENBERG EBOOK.+\*{3}$/m).index
const paragraphs = book
.slice(startOfBookIndex, endOfBookIndex) 
.split(/\n\s+\n/g) 
.map(line => line.replace(/\r\n/g, ' ').trim()) 
.map(line => line.replace(/_/g, '')) 
.filter((line) => (line && line.length !== '')) 
console.log(`Parsed ${paragraphs.length} Paragraphs\n`)
return { title, author, paragraphs }
}
function parseBookFile ( filePath ) { const book = fs . readFileSync ( filePath , 'utf8' ) const title = book . match ( / ^Title:\s(.+)$ / m ) [ 1 ] const authorMatch = book . match ( / ^Author:\s(.+)$ / m ) const author = ( ! authorMatch || authorMatch [ 1 ] . trim ( ) === '' ) ? 'Unknown Author' : authorMatch [ 1 ] console . log ( ` ${ title } ${ author } ` ) const startOfBookMatch = book . match ( / ^\*{3}\s*START OF (THIS|THE) PROJECT GUTENBERG EBOOK.+\*{3}$ / m ) const startOfBookIndex = startOfBookMatch . index + startOfBookMatch [ 0 ] . length const endOfBookIndex = book . match ( / ^\*{3}\s*END OF (THIS|THE) PROJECT GUTENBERG EBOOK.+\*{3}$ / m ) . index const paragraphs = book . slice ( startOfBookIndex , endOfBookIndex ) . split ( / \n\s+\n / g ) . map ( line => line . replace ( / \r\n / g , ' ' ) . trim ( ) ) . map ( line => line . replace ( / _ / g , '' ) ) . filter ( ( line ) => ( line && line . length !== '' ) ) console . log ( ` ${ paragraphs . length } \n ` ) return { title , author , paragraphs } }
Read an individual book text file, and extract the title, author, and paragraphs
Read an individual book text file and extract the title author and paragraphs
// Read text file // Find book title and author // Find Guttenberg metadata header and footer // Clean book text and split into array of paragraphs // Remove Guttenberg header and footer // Split each paragraph into it's own array entry // Remove paragraph line breaks and whitespace // Guttenberg uses "_" to signify italics.  We'll remove it, since it makes the raw text look messy. // Remove empty lines