Debian Codegraph @

NLP/Minimorph/English.hs

raw
1 {-# LANGUAGE ViewPatterns #-}
2 -- TODO : learn how to use Functional Morphology instead
3 -- |Simple default rules for English morphology
4 module NLP.Minimorph.English where
5
6 #if !(MIN_VERSION_base(4,11,0))
7 -- this is redundant starting with base-4.11 / GHC 8.4
8 import Data.Semigroup
9 #endif
10
11 import Data.Char (isSpace, isUpper, toLower)
12 import Data.Text (Text)
13 import qualified Data.Text as T
14
15 import NLP.Minimorph.Util
16
17 -- ---------------------------------------------------------------------
18 -- ** Punctuation
19 -- ---------------------------------------------------------------------
20
21 -- | No Oxford commas, alas.
22 --
23 -- > commas "and" "foo bar" == "foo and bar"
24 -- > commas "and" "foo, bar, baz" == "foo, bar and baz"
25 commas :: Text -> [Text] -> Text
26 commas _ [] = ""
27 commas _ [x] = x
28 commas et xs = T.intercalate ", " (init xs) <+> et <+> last xs
29
30 -- ---------------------------------------------------------------------
31 -- ** Numbers
32 -- ---------------------------------------------------------------------
33
34 -- | > cardinal 0 == "zero"
35 -- > cardinal 1 == "one"
36 -- > cardinal 2 == "two"
37 -- > cardinal 10 == "ten"
38 -- > cardinal 11 == "11"
39 cardinal :: Int -> Text
40 cardinal n = case n of
41 0 -> "zero"
42 1 -> "one"
43 2 -> "two"
44 3 -> "three"
45 4 -> "four"
46 5 -> "five"
47 6 -> "six"
48 7 -> "seven"
49 8 -> "eight"
50 9 -> "nine"
51 10 -> "ten"
52 _ -> tshow n
53
54 -- | > ordinalNotSpelled 1 == "1st"
55 -- > ordinalNotSpelled 2 == "2nd"
56 -- > ordinalNotSpelled 11 == "11th"
57 ordinalNotSpelled :: Int -> Text
58 ordinalNotSpelled k = case abs k `rem` 100 of
59 n | n > 3 && n < 21 -> k `suf` "th"
60 | n `rem` 10 == 1 -> k `suf` "st"
61 | n `rem` 10 == 2 -> k `suf` "nd"
62 | n `rem` 10 == 3 -> k `suf` "rd"
63 | otherwise -> k `suf` "th"
64 where
65 num `suf` s = tshow num <> s
66
67 -- | > ordinal 1 == "first"
68 -- > ordinal 2 == "second"
69 -- > ordinal 3 == "third"
70 -- > ordinal 11 == "11th"
71 -- > ordinal 42 == "42nd"
72 ordinal :: Int -> Text
73 ordinal n = case n of
74 0 -> "zeroth"
75 1 -> "first"
76 2 -> "second"
77 3 -> "third"
78 4 -> "fourth"
79 5 -> "fifth"
80 6 -> "sixth"
81 7 -> "seventh"
82 8 -> "eighth"
83 9 -> "ninth"
84 10 -> "tenth"
85 k -> ordinalNotSpelled k
86
87 -- ---------------------------------------------------------------------
88 -- ** Nouns and verbs
89 -- ---------------------------------------------------------------------
90
91 -- | Heuristics for English plural for an unknown noun.
92 --
93 -- > defaultNounPlural "egg" == "eggs"
94 -- > defaultNounPlural "patch" == "patches"
95 -- > defaultNounPlural "boy" == "boys"
96 -- > defaultNounPlural "spy" == "spies"
97 -- > defaultNounPlural "thesis" == "theses"
98 --
99 -- http://www.paulnoll.com/Books/Clear-English/English-plurals-1.html
100 --
101 -- http://en.wikipedia.org/wiki/English_plural
102 defaultNounPlural :: Text -> Text
103 defaultNounPlural x
104 | "is" `T.isSuffixOf` x = thesis
105 | hasSibilantSuffix x = sibilant_o
106 | hasCoSuffix x = sibilant_o
107 | hasCySuffix x = y_final
108 | "ff" `T.isSuffixOf` x = ff_final -- quite often not the case
109 | "f" `T.isSuffixOf` x = f_final -- but this one as well, so both needed
110 | otherwise = plain
111 where
112 plain = x <> "s"
113 sibilant_o = x <> "es"
114 y_final = T.init x <> "ies"
115 f_final = T.init x <> "ves"
116 ff_final = T.dropEnd 2 x <> "ves"
117 thesis = T.dropEnd 2 x <> "es"
118
119 -- | Heuristics for 3rd person singular and past participle
120 -- for an unknown regular verb. Doubling of final consonants
121 -- can be handled via a table of (partially) irregular verbs.
122 --
123 -- > defaultVerbStuff "walk" == ("walks", "walked")
124 -- > defaultVerbStuff "push" == ("pushes", "pushed")
125 -- > defaultVerbStuff "play" == ("plays", "played")
126 -- > defaultVerbStuff "cry" == ("cries", "cried")
127 defaultVerbStuff :: Text -> (Text, Text)
128 defaultVerbStuff x
129 | hasSibilantSuffix x = sibilant_o
130 | hasCoSuffix x = sibilant_o
131 | hasCySuffix x = y_final
132 | "e" `T.isSuffixOf` x = e_final
133 | otherwise = plain
134 where
135 plain = (x <> "s" , x <> "ed")
136 sibilant_o = (x <> "es" , x <> "ed")
137 e_final = (x <> "s" , x <> "d")
138 y_final = (T.init x <> "ies", T.init x <> "ied")
139
140 -- | Heuristics for a possesive form for an unknown noun.
141 --
142 -- > defaultPossesive "pass" == "pass'"
143 -- > defaultPossesive "SOS" == "SOS'"
144 -- > defaultPossesive "Mr Blinkin'" == "Mr Blinkin's"
145 -- > defaultPossesive "cry" == "cry's"
146 defaultPossesive :: Text -> Text
147 defaultPossesive t =
148 case T.last t of
149 's' -> t <> "'"
150 'S' -> t <> "'"
151 '\'' -> t <> "s"
152 _ -> t <> "'s"
153
154 -- ---------------------------------------------------------------------
155 -- ** Determiners
156 -- ---------------------------------------------------------------------
157
158 anNumerals :: [Text]
159 anNumerals = [ "11", "11th", "18", "18th" ]
160
161 -- | > indefiniteDet "dog" == "a"
162 -- > indefiniteDet "egg" == "an"
163 -- > indefiniteDet "ewe" == "a"
164 -- > indefiniteDet "ewok" == "an"
165 -- > indefiniteDet "8th" == "an"
166 indefiniteDet :: Text -> Text
167 indefiniteDet t = if wantsAn t then "an" else "a"
168
169 -- | True if the indefinite determiner for a word would normally be
170 -- \'an\' as opposed to \'a\'.
171 wantsAn :: Text -> Bool
172 wantsAn t_ =
173 if startsWithAcronym t_
174 then acronymWantsAn t_
175 else useAn0 || useAn1
176 where
177 t = fst $ T.break isSep $ T.toLower t_
178 useAn0 = t `elem` anNumerals
179 useAn1 = case T.uncons t of
180 Just (h, "") -> isLetterWithInitialVowelSound h
181 Just ('8',_) -> True
182 Just ('u',_) -> hasVowel_U_Prefix t
183 Just (h, _) -> isVowel h `butNot` hasSemivowelPrefix t
184 Nothing -> False
185 x `butNot` y = x && not y
186 isSep c = isSpace c || c `elem` ("-" :: String)
187
188 -- | Variant of 'wantsAn' that assumes the input string is pronounced
189 -- one letter at a time.
190 --
191 -- > wantsAn "x-ray" == False
192 -- > acronymWantsAn "x-ray" == True
193 --
194 -- Note that this won't do the right thing for words like \"SCUBA\".
195 -- You really have to reserve it for those separate-letter acronyms.
196 acronymWantsAn :: Text -> Bool
197 acronymWantsAn (T.toLower -> t) =
198 useAn0 || useAn1
199 where
200 useAn0 = t `elem` anNumerals
201 useAn1 = case T.uncons t of
202 Just ('8',_) -> True
203 Just (h,_) -> isLetterWithInitialVowelSound h
204 Nothing -> False
205
206 -- ---------------------------------------------------------------------
207 -- ** Acronyms
208 -- ---------------------------------------------------------------------
209
210 -- | True if all upper case from second letter and up.
211 --
212 -- > looksLikeAcronym "DNA" == True
213 -- > looksLikeAcronym "tRNA" == True
214 -- > looksLikeAcronym "x" == False
215 -- > looksLikeAcronym "DnA" == False
216 looksLikeAcronym :: Text -> Bool
217 looksLikeAcronym "" = False
218 looksLikeAcronym x = T.all isUpper (if T.length x > 1 then T.drop 1 x else x)
219
220 -- | True if the first word (separating on either hyphen or space)
221 -- looks like an acronym.
222 startsWithAcronym :: Text -> Bool
223 startsWithAcronym =
224 looksLikeAcronym . firstWord
225 where
226 firstWord = fst . T.break isSep
227 isSep c = isSpace c || c `elem` ("-" :: String)
228
229 -- ---------------------------------------------------------------------
230 -- ** Sounds
231 -- ---------------------------------------------------------------------
232
233 -- | Ends with a \'sh\' sound.
234 hasSibilantSuffix :: Text -> Bool
235 hasSibilantSuffix x = any (`T.isSuffixOf` x) ["x","s","ch","sh","z","j"]
236
237 -- | Starts with a semivowel.
238 hasSemivowelPrefix :: Text -> Bool
239 hasSemivowelPrefix ls = any (`T.isPrefixOf` ls) ["y","w","eu","ewe"]
240
241 -- | Starts with a vowel-y \'U\' sound
242 hasVowel_U_Prefix :: Text -> Bool
243 hasVowel_U_Prefix t =
244 case T.unpack t of
245 ['u'] -> False
246 ['u',_] -> True
247 ('u':c:v:_) -> not (isConsonant c && isVowel v)
248 _ -> False
249
250 -- | Last two letters are a consonant and \'y\'.
251 hasCySuffix :: Text -> Bool
252 hasCySuffix (T.unpack . T.takeEnd 2 -> [x, 'y']) = isConsonant x
253 hasCySuffix _ = False
254
255 -- | Last two letters are a consonant and \'o\'.
256 hasCoSuffix :: Text -> Bool
257 hasCoSuffix (T.unpack . T.takeEnd 2 -> [x, 'o']) = isConsonant x
258 hasCoSuffix _ = False
259
260 -- | Is a vowel.
261 isVowel :: Char -> Bool
262 isVowel = (`elem` ("aeiou" :: String)) . toLower
263
264 -- | Letters that when pronounced independently in English sound like they
265 -- begin with vowels.
266 --
267 -- > isLetterWithInitialVowelSound 'r' == True
268 -- > isLetterWithInitialVowelSound 'k' == False
269 --
270 -- (In the above, \'r\' is pronounced \"are\", but \'k\' is pronounced
271 -- \"kay\".)
272 isLetterWithInitialVowelSound :: Char -> Bool
273 isLetterWithInitialVowelSound = (`elem` ("aeiofhlmnrsx" :: String)) . toLower
274
275 -- | Is a consonant.
276 --
277 -- Note that not every `Char` is either a vowel or a consonant.
278 -- We consider numbers, spaces and symbols to be neither vowel or consonants
279 isConsonant :: Char -> Bool
280 isConsonant = (`elem` ("bcdfghjklmnpqrstvwxyz" :: String)) . toLower