Structure and electrical activity of rare-earth dopants in semiconductors