Ab initio calculation of excitonic effects in the optical spectra of semiconductors