The work could transform the way people interact with literature and spark new ways to visualize information, such as audiovisual e-books that generate music according to the emotions on the page or novel music apps, researchers said.

"Given a novel in an electronically readable form, our system - called TransProse - generates simple piano pieces whose notes are dependent on the emotion words in the text," said Saif Mohammad, a computer scientist at the National Research Council Canada.

Along with Hannah Davis, who created TransProse as her master's thesis at New York University, Mohammad used the software to count the density of words associated with the eight basic emotions: anticipation, anger, joy, fear, disgust, sadness, surprise and trust, 'LiveScience' reported.

"It is the first system that automatically generates musical pieces based on the emotions in the text, and uses a novel mechanism to determine sequences of notes that capture the emotional activity in text," said Mohammad, who holds a bachelor's degree in Electronics Engineering from Vishwakarma Institute of Technology, University of Pune.

The algorithm uses databases to rate words according to their emotional value, thereby analysing sentiment in the text and gauging its "emotional temperature".

The algorithm works by splitting the book into four sections: the beginning, early middle, late middle and ending. Then, it generates an emotion profile for each part — a collection of various statistics about the presence of emotional words in the text, Mohammad said.

The researchers have used TransProse to measure the sentiment changes throughout a book, automatically generating music that reflects specific moods.

So far, the scientists have created musical soundtracks for several famous novels, including "Peter Pan" by JM Barrie, "A Clockwork Orange" by Anthony Burgess and "The Road" by Cormac McCarthy, among others.