This repository has been archived by the owner on Jun 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sentence.hs
130 lines (111 loc) · 4.28 KB
/
Sentence.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
-- Homework 3 template
module Sentence where
-- Grammar for the animal sentence language:
--
-- <sentence> -> <noun> <verb> [<noun>]
-- | <sentence> `and` <sentence>
--
-- <noun> -> <adj> <noun> | <noun> `and` <noun>
-- | `cats` | `dogs` | `bears` | `goats`
-- <verb> -> `chase` | `cuddle` | `hug` | `scare`
-- <adj> -> `sad` | `small` | `big` | `happy`
-- Define Sentence data type
data Sentence
= NVN Noun Verb Noun --Noun, Verb, Noun
| NV Noun Verb --Noun, Verb
| And Sentence Sentence --Sentence conjunction
| End --End of Sentence
deriving (Eq,Show)
-- Define Adjective data type
data Adj = Sad | Small | Big | Happy
deriving (Eq,Show)
-- Define Noun data type
data Noun
= Cats
| Dogs
| Bears
| Goats
| NP Adj Noun --Noun phrase with an adjective
| NAnd Noun Noun --Noun conjunction
deriving (Eq, Show)
-- Define Verb data type
data Verb = Chase | Cuddle | Hug | Scare
deriving (Eq, Show)
-- | Build a sentence from a noun verb noun.
-- | buildS2 Cats Hug Cats
-- | NVN Cats Hug Cats
buildS2 :: Noun -> Verb ->Noun-> Sentence
buildS2 s v o = NVN s v o
-- | Build a sentence from a noun verb
-- | buildS1 Cats Hug
-- | NV Cats Hug
buildS1 :: Noun -> Verb ->Sentence
buildS1 s v = NV s v
-- | Build a noun phrase from an adjective and noun
-- | buildNP Happy Dogs
-- | NP Happy Dogs
buildNP :: Adj -> Noun -> Noun
buildNP a n = NP a n
-- | Build a noun conjunction from two nouns
-- | buildNAnd Dogs Cats
-- | NAnd Dogs Cats
buildNAnd :: Noun -> Noun -> Noun
buildNAnd n1 n2 = NAnd n1 n2
-- | Build a sentence that is a conjunction of a list of other sentences.
-- | conjunction [ex1, ex2]
-- | And (NVN Cats Hug Dogs) (NVN (NP Small Cats) Hug Dogs)
-- | The End is used if no sentences are given
conjunction :: [Sentence] -> Sentence
conjunction [] = End -- If the list is empty, return the End constructor
conjunction [x] = x -- If the list has only one element, return that element
conjunction (x:xs) = And x (conjunction xs) -- Recursively join head/tail conj
-- | Pretty print a sentence.
pretty :: Sentence -> String
pretty (NVN s v o) = prettyNoun s ++ " " ++ prettyVerb v ++ " " ++ prettyNoun o
pretty (And l r) = pretty l ++ " and " ++ pretty r
pretty (NV s v) = prettyNoun s ++ " " ++ prettyVerb v
pretty (End) = "." -- If it's the End constructor, return a period
-- | Pretty print a noun.
prettyNoun :: Noun -> String
prettyNoun Cats = "cats"
prettyNoun Dogs = "dogs"
prettyNoun Bears = "bears"
prettyNoun Goats = "goats"
prettyNoun (NP a n) = prettyAdj a ++ " " ++ prettyNoun n
prettyNoun (NAnd m n) = prettyNoun m ++ " and " ++prettyNoun n
-- | Pretty print a verb.
prettyVerb :: Verb -> String
prettyVerb Chase = "chase"
prettyVerb Cuddle = "cuddle"
prettyVerb Hug = "hug"
prettyVerb Scare = "scare"
-- | Pretty print an adjective.
prettyAdj :: Adj -> String
prettyAdj Sad = "sad"
prettyAdj Small = "small"
prettyAdj Big = "big"
prettyAdj Happy = "happy"
-- | Determine if a sentence only contains the verbs chase and scare
-- | isMean ex2 => False
-- | isMean ex3 => True
isMean :: Sentence -> Bool
isMean (NVN _ Chase _) = True --If verb is Chase, True
isMean (NVN _ Scare _) = True --If verb is Scare, True
isMean (NVN _ _ _) = False --If verb is not Chase or Scare, False
isMean (And l r) = isMean l && isMean r --Recurse on left & right, return conj
isMean (NV _ Chase) = True --If verb is Chase, True
isMean (NV _ Scare) = True --If verb is Scare, True
isMean (NV _ _) = False --If verb is not Chase or Scare, False
isMean End = True --If it's the End constructor, True
-- | Counts the number of words in a sentence
-- | wordCount ex4
-- | 6
wordCount :: Sentence -> Int
wordCount (NVN s _ o) = nounWordCount s + 1 + nounWordCount o --Add 1 for verb
wordCount (NV s _) = nounWordCount s + 1 --Add 1 for verb
wordCount (And l r) = wordCount l + 1 + wordCount r --Add 1 for conj
wordCount End = 0 --If End constructor, return 0
nounWordCount :: Noun -> Int
nounWordCount (NP _ n) = 1 + nounWordCount n --Count words in adj-noun phrase
nounWordCount (NAnd m n) = nounWordCount m + 1 + nounWordCount n --Add 1 for con
nounWordCount _ = 1 --If it's a simple noun, return 1