Dogs are often quite empathetic. The dog probably picked up on the woman’s cries and ditress and the assailant’s violent emotions, and figured out who the victim was and who the bad guy was. Similarly, when the cops came, the dog sensed that they were not violent, and that the victim was happy to see them. So, the dog knew that the cops were good guys.