To calculate keyword density, divide the number of times a keyword appears in a text by the total number of words in the text. The result is the keyword density expressed as a percentage.