Text this: The hidden mathematical logic of stories