| 
									
										
										
										
											2010-03-27 06:25:47 -04:00
										 |  |  | USING: arrays ascii assocs combinators combinators.smart fry | 
					
						
							|  |  |  | http.client io.encodings.ascii io.files io.files.temp kernel | 
					
						
							| 
									
										
										
										
											2013-05-22 14:02:36 -04:00
										 |  |  | literals locals math math.ranges math.statistics memoize | 
					
						
							| 
									
										
										
										
											2013-06-25 18:50:42 -04:00
										 |  |  | sequences sequences.private sets sorting splitting strings urls ;
 | 
					
						
							| 
									
										
										
										
											2010-03-27 06:25:47 -04:00
										 |  |  | IN: spelling | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ! http://norvig.com/spell-correct.html | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-25 18:50:42 -04:00
										 |  |  | CONSTANT: ALPHABET "abcdefghijklmnopqrstuvwxyz" | 
					
						
							| 
									
										
										
										
											2010-03-27 06:25:47 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-25 18:50:42 -04:00
										 |  |  | : deletes ( word -- edits )
 | 
					
						
							|  |  |  |     [ length iota ] keep '[ _ remove-nth ] map ;
 | 
					
						
							| 
									
										
										
										
											2010-03-27 06:25:47 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-25 18:50:42 -04:00
										 |  |  | : transposes ( word -- edits )
 | 
					
						
							|  |  |  |     [ length [1,b) ] keep '[ | 
					
						
							|  |  |  |         dup 1 - _ clone [ exchange-unsafe ] keep
 | 
					
						
							|  |  |  |     ] map ;
 | 
					
						
							| 
									
										
										
										
											2010-03-27 06:25:47 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-25 18:50:42 -04:00
										 |  |  | : replaces ( word -- edits )
 | 
					
						
							|  |  |  |     [ length iota ] keep '[ | 
					
						
							|  |  |  |         ALPHABET [ | 
					
						
							|  |  |  |             swap _ clone [ set-nth-unsafe ] keep
 | 
					
						
							|  |  |  |         ] with { } map-as
 | 
					
						
							|  |  |  |     ] map concat ;
 | 
					
						
							| 
									
										
										
										
											2013-05-22 14:02:36 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-25 18:50:42 -04:00
										 |  |  | : inserts ( word -- edits )
 | 
					
						
							|  |  |  |     [ length [0,b] ] keep '[ | 
					
						
							|  |  |  |         CHAR: ? over _ insert-nth ALPHABET swap [ | 
					
						
							|  |  |  |             swapd clone [ set-nth-unsafe ] keep
 | 
					
						
							|  |  |  |         ] curry with { } map-as
 | 
					
						
							|  |  |  |     ] map concat ;
 | 
					
						
							| 
									
										
										
										
											2010-03-27 06:25:47 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : edits1 ( word -- edits )
 | 
					
						
							|  |  |  |     [ | 
					
						
							| 
									
										
										
										
											2013-06-25 18:50:42 -04:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2010-03-27 06:25:47 -04:00
										 |  |  |             [ deletes ] | 
					
						
							|  |  |  |             [ transposes ] | 
					
						
							|  |  |  |             [ replaces ] | 
					
						
							|  |  |  |             [ inserts ] | 
					
						
							|  |  |  |         } cleave
 | 
					
						
							|  |  |  |     ] append-outputs ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : edits2 ( word -- edits )
 | 
					
						
							|  |  |  |     edits1 [ edits1 ] map concat ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-22 14:02:36 -04:00
										 |  |  | : filter-known ( edits dictionary -- words )
 | 
					
						
							| 
									
										
										
										
											2010-03-27 06:25:47 -04:00
										 |  |  |     '[ _ key? ] filter ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | :: corrections ( word dictionary -- words )
 | 
					
						
							|  |  |  |     word 1array dictionary filter-known | 
					
						
							|  |  |  |     [ word edits1 dictionary filter-known ] when-empty
 | 
					
						
							|  |  |  |     [ word edits2 dictionary filter-known ] when-empty
 | 
					
						
							| 
									
										
										
										
											2013-05-22 14:02:36 -04:00
										 |  |  |     [ dictionary at ] sort-with reverse! ;
 | 
					
						
							| 
									
										
										
										
											2010-03-27 06:25:47 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : words ( string -- words )
 | 
					
						
							|  |  |  |     >lower [ letter? not ] split-when harvest ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : load-dictionary ( file -- assoc )
 | 
					
						
							|  |  |  |     ascii file-contents words histogram ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | MEMO: default-dictionary ( -- counts )
 | 
					
						
							| 
									
										
										
										
											2014-06-09 11:58:05 -04:00
										 |  |  |     URL" http://norvig.com/big.txt" "big.txt" temp-file | 
					
						
							|  |  |  |     [ ?download-to ] [ load-dictionary ] bi ;
 | 
					
						
							| 
									
										
										
										
											2010-03-27 06:25:47 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : (correct) ( word dictionary -- word/f )
 | 
					
						
							| 
									
										
										
										
											2011-10-13 15:53:46 -04:00
										 |  |  |     corrections ?first ;
 | 
					
						
							| 
									
										
										
										
											2010-03-27 06:25:47 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : correct ( word -- word/f )
 | 
					
						
							|  |  |  |     default-dictionary (correct) ;
 |