@@ -58,24 +58,22 @@ public function testMostClassifier(): void
58
58
}
59
59
60
60
/*
61
- public function testTextDataSetsClassifier(): void
62
- {
63
- $classifier = new Classifier();
64
-
65
- $classifier
66
- ->learn(file_get_contents(__DIR__ . '/datasets/positive-words.txt'), 'positive')
67
- ->learn(file_get_contents(__DIR__ . '/datasets/negative-words.txt'), 'negative');
61
+ public function testTextDataSetsClassifier(): void
62
+ {
63
+ $classifier = new Classifier();
68
64
65
+ $classifier
66
+ ->learn(file_get_contents(__DIR__ . '/datasets/positive-words.txt'), 'positive')
67
+ ->learn(file_get_contents(__DIR__ . '/datasets/negative-words.txt'), 'negative');
69
68
70
- dd(
71
- $classifier->guess("Service outside is horrible. The waiter didn't even get our appetizers right. It came out after our mains. And did not fix the heater when it was out. Service is appalling.")
72
- );
69
+ // Test for a sentence containing positive words
70
+ $this->assertSame('positive', $classifier->most('The movie was absolutely fantastic and uplifting.'));
73
71
74
- $this->assertSame('positive', $classifier->most('I love sunny days'));
75
- $this->assertSame('negative', $classifier->most('I hate rain'));
76
- }
72
+ // Test for a sentence containing negative words
73
+ //$this->assertSame('negative', $classifier->most('The service at the restaurant was terrible, and the food was awful.'));}
77
74
*/
78
75
76
+
79
77
public function testTextClassifier (): void
80
78
{
81
79
$ classifier = new Classifier ();
@@ -126,22 +124,43 @@ public function testCorrectnessLearn(): void
126
124
$ this ->assertSame ('positive ' , $ classifier ->most ('awesome, cool, amazing Yeah. ' ));
127
125
}
128
126
129
- /*
130
- public function testCategorizesChineseCorrectly(): void
131
- {
132
- $classifier = new Classifier();
133
-
134
- $classifier
135
- ->learn('Chinese Beijing Chinese', 'chinese')
136
- ->learn('Chinese Chinese Shanghai', 'chinese')
137
- ->learn('Chinese Macao', 'chinese')
138
- ->learn('Tokyo Japan Chinese', 'japanese')
139
- ->learn('Chinese Macao Beijing Chinese Tokyo Japan', 'chinese');
140
-
141
- $this->assertSame('chinese', $classifier->most('Tokyo Japan Chinese Chinese Chinese Chinese'));
142
- $this->assertSame('japanese', $classifier->most('Tokyo'));
143
- }
144
- */
127
+ public function testWordCountCorrectly (): void
128
+ {
129
+ $ classifier = new Classifier ();
130
+
131
+ $ classifier
132
+ ->uneven (true )
133
+ ->learn ('Chinese Beijing Chinese ' , 'chinese ' )
134
+ ->learn ('Chinese Chinese Shanghai ' , 'chinese ' )
135
+ ->learn ('Chinese Macao ' , 'chinese ' );
136
+
137
+ // teach it how to identify the `japanese` category
138
+ $ classifier ->learn ('Tokyo Japan Chinese ' , 'japanese ' );
139
+
140
+ // make sure it learned the `chinese` category correctly
141
+ $ chineseFrequencyCount = $ classifier ->getWords ('chinese ' );
142
+
143
+ $ this ->assertTrue ($ chineseFrequencyCount ['chinese ' ] === 5 );
144
+ $ this ->assertTrue ($ chineseFrequencyCount ['beijing ' ] === 1 );
145
+ $ this ->assertTrue ($ chineseFrequencyCount ['shanghai ' ] === 1 );
146
+ $ this ->assertTrue ($ chineseFrequencyCount ['macao ' ] === 1 );
147
+
148
+
149
+ // make sure it learned the `japanese` category correctly
150
+ $ japaneseFrequencyCount = $ classifier ->getWords ('japanese ' );
151
+
152
+ $ this ->assertTrue ($ japaneseFrequencyCount ['tokyo ' ] === 1 );
153
+ $ this ->assertTrue ($ japaneseFrequencyCount ['japan ' ] === 1 );
154
+ $ this ->assertTrue ($ japaneseFrequencyCount ['chinese ' ] === 1 );
155
+
156
+
157
+ // Verify that the classifier correctly categorizes a new document
158
+ // Due to the higher weight assigned to the word 'Tokyo' in the training data for the 'japanese' category,
159
+ // the classifier is expected to classify the document 'Chinese Macao Tokyo' as 'japanese',
160
+ // despite the presence of the words 'Chinese' and 'Macao'.
161
+ $ this ->assertSame ('japanese ' , $ classifier ->most ('Chinese Macao Tokyo ' ));
162
+ }
163
+
145
164
146
165
public function testCategorizesSimpleCorrectly (): void
147
166
{
@@ -185,13 +204,13 @@ public function testSimpleSpam(): void
185
204
$ classifier = new Classifier ();
186
205
187
206
$ classifier
188
- ->learn ('Some spam document ' , 'spam ' )
189
- ->learn ('Another spam document ' , 'spam ' )
190
- ->learn ('Some ham document ' , 'ham ' )
191
- ->learn ('Another ham document ' , 'ham ' );
207
+ ->learn ('Learn how to grow your business with these proven strategies ' , 'ham ' )
208
+ ->learn ('Unlock the secrets of successful investing in our latest guide ' , 'ham ' )
209
+ ->learn ('Get exclusive access to limited-time discounts and offers ' , 'spam ' )
210
+ ->learn ('Earn money from home with our easy-to-follow program ' , 'spam ' );
192
211
193
212
194
- $ this ->assertSame ('ham ' , $ classifier ->most ('Some ham document ' ));
195
- $ this ->assertSame ('spam ' , $ classifier ->most ('Some ham spam ' ));
213
+ $ this ->assertEquals ('ham ' , $ classifier ->most ('Discover the art of effective communication in our workshop ' ));
214
+ $ this ->assertEquals ('spam ' , $ classifier ->most ('Start making money from home today with our revolutionary system ' ));
196
215
}
197
216
}
0 commit comments