One problem on a website like this is that you often don't know if you are talking to a male or female. However, you have come up with a simple NLP technique you can use to determine the gender of the writer of a piece of text.
Theory
About 38.1% of letters used in English are vowels [a,e,i,o,u] (see References below, y
is NOT a vowel in this case). Therefore, we will define any word that is at least 40% vowels as a feminine word, and any word that is less than 40% vowels as a masculine word.
Beyond this definition we can also find the masculinity or femininity of a word. Let C be the number of consonants in the word, and V be the number of vowels:
- If a word is feminine, it's femininity is
1.5*V/(C+1)
. - If a word is masculine, it's masculinity is
C/(1.5*V+1)
.
For example, the word catch
is masculine. Its masculinity is 4/(1.5*1+1) = 1.6
. The word phone
is feminine. Its femininity is 1.5*2/(3+1) = .75
.
Algorithm
To figure out the gender of the writer of a piece of text, we take the sum of the masculinity of all the masculine words (ΣM), and the sum of the femininity of all the feminine words (ΣF). If ΣM > ΣF, we have determined that the writer is a male. Otherwise, we have determined that the writer is a female.
Confidence Level
Finally, we need a confidence level. If you have determined that the writer is female, your confidence level is 2*ΣF/(ΣF+ΣM)-1
. If you have determined that the writer is male, the confidence level is 2*ΣM/(ΣF+ΣM)-1
.
Input
Input is a piece of English text including punctuation. Words are all separated by spaces (You don't have to worry about new-lines or extra spaces). Some words have non-letter characters in them, which you need to ignore (such as "You're"). If you encounter a word that is all non-letters (like "5" or "!!!") just ignore it. Every input will contain at least one usable word.
Output
You need to output an M or F depending on which gender you think the writer is, followed by your confidence level.
Examples
There's a snake in my boot.
- Gender + masculinity/femininity of each word:
[M1.0,F1.5,F.75,F.75,M2.0,F1.0]
- ΣM = 3.0, ΣF = 4.0
- CL:
2*4.0/(4.0+3.0)-1
= .143 - Output:
F .143
- Gender + masculinity/femininity of each word:
Frankly, I don't give a ^$*.
[M2.4,F1.5,M1.2,F1.0,F1.5]
, ΣM = 3.6, ΣF = 4.0, CL:2*4.0/(4.0+3.6)-1
= .053, Output:F .053
I'm 50 dollars from my goal!
[F.75,M1.25,M1.2,M2.0,F1.0]
, ΣM = 4.45, ΣF = 1.75, CL:2*4.45/(4.45+1.75)-1
= .435, Output:M .435