FuzzyMatcher.st
changeset 4468 4da827f70608
parent 4459 cfbde361fe34
child 4469 b6f641dce0ac
--- a/FuzzyMatcher.st	Thu Jul 13 18:09:46 2017 +0200
+++ b/FuzzyMatcher.st	Thu Jul 13 20:48:38 2017 +0200
@@ -25,6 +25,95 @@
         https://github.com/forrestthewoods/lib_fts
 
 "
+!
+
+example
+"
+    |top lv list field patternHolder names|
+
+    patternHolder := '' asValue.
+    list := List new.
+    
+    top := StandardSystemView new.
+    lv := ListView origin:(0.0@30) corner:(1.0@1.0) in:top.
+    lv model:list.
+
+    field := EditField origin:(0.0@0.0) corner:(1.0@30) in:top.
+    field model:patternHolder.
+    field immediateAccept:true.
+
+    names := Smalltalk allClasses collect:#name.
+
+    patternHolder 
+        onChangeEvaluate:[
+            |matcher pattern matches|
+
+            pattern := patternHolder value.
+            pattern notEmpty ifTrue:[
+                matcher := FuzzyMatcher pattern:pattern.
+                
+                matches := OrderedCollection new.
+
+                names do:[:eachClassName | 
+                    matcher 
+                        match:eachClassName
+                        ifScored: [ :score | matches add: eachClassName -> score ] 
+                ].
+                matches sortBySelector:#value.
+
+                list removeAll.
+                list addAllReversed:(matches 
+                                collect:[:nameScoreAssoc | 
+                                    '[%1] %2' bindWith:nameScoreAssoc value with:nameScoreAssoc key])
+            ].    
+        ].    
+    top open.
+    patternHolder value:'mph'.
+"
+!
+
+examples
+""
+    |top lv list field patternHolder names|
+
+    patternHolder := 'mph' asValue.
+    list := List new.
+    
+    top := StandardSystemView new.
+    lv := ListView origin:(0.0@30) corner:(1.0@1.0) in:top.
+    lv model:list.
+
+    field := EditField origin:(0.0@0.0) corner:(1.0@30) in:top.
+    field model:patternHolder.
+    field immediateAccept:true.
+
+    names := Smalltalk allClasses collect:#name.
+
+    patternHolder 
+        onChangeEvaluate:[
+            |matcher pattern matches|
+
+            pattern := patternHolder value.
+            pattern notEmpty ifTrue:[
+                matcher := FuzzyMatcher pattern:pattern.
+                
+                matches := OrderedCollection new.
+
+                names do:[:eachClassName | 
+                    matcher 
+                        match:eachClassName
+                        ifScored: [ :score | matches add: eachClassName -> score ] 
+                ].
+                matches sortBySelector:#value.
+
+                list removeAll.
+                list addAllReversed:(matches 
+                                collect:[:nameScoreAssoc | 
+                                    '[%1] %2' bindWith:nameScoreAssoc value with:nameScoreAssoc key])
+            ].    
+        ].    
+    top open.
+""
 ! !
 
 !FuzzyMatcher class methodsFor:'instance creation'!