--- a/FuzzyMatcher.st Fri Jul 14 11:43:57 2017 +0200
+++ b/FuzzyMatcher.st Fri Jul 14 12:19:21 2017 +0200
@@ -13,7 +13,7 @@
documentation
"
- FuzzyMatcher is an approximate string matching algroithm that can determine if a string includes a given pattern.
+ FuzzyMatcher is an approximate string matching algorithm that can determine if a string includes a given pattern.
For example, the string 'axby' matches both the pattern 'ab' and, 'ay', but not 'ba'.
The algorithm is based on lib_fts[1], and includes an optional scoring algorithm
@@ -93,57 +93,98 @@
!FuzzyMatcher class methodsFor:'utilities api'!
allMatching: aPattern in: aCollection
- "Assumes that the collection is a collection of Strings"
+ "Assumes that the collection is a collection of Strings"
+
+ | matcher |
- | matcher |
-
- matcher := self pattern: aPattern.
+ matcher := self pattern: aPattern.
- ^ aCollection select: [ :each | matcher matches: each ]
+ ^ aCollection select: [ :each | matcher matches: each ]
- "
- self
- allMatching:'clu'
- in:(Smalltalk allClasses collect:#name)
- "
+ "
+ self
+ allMatching:'clu'
+ in:(Smalltalk allClasses collect:#name)
+ "
- "Modified (comment): / 13-07-2017 / 13:29:11 / cg"
+ "Modified (comment): / 14-07-2017 / 12:19:05 / cg"
!
allMatching: aPattern in: aCollection by: aBlockReturningString
- | matcher |
-
- matcher := self pattern: aPattern.
+ | matcher |
+
+ matcher := self pattern: aPattern.
+
+ ^ aCollection select: [ :each | matcher matches: (aBlockReturningString value: each) ]
- ^ aCollection select: [ :each | matcher matches: (aBlockReturningString value: each) ]
+ "
+ self
+ allMatching:'clu'
+ in:(Smalltalk allClasses)
+ by:[:cls | cls name]
+ "
+ "
+ self
+ allMatching:'clu'
+ in:(Smalltalk allClasses)
+ by:#name
+ "
+
+ "Modified (format): / 14-07-2017 / 12:18:59 / cg"
!
allSortedByScoreMatching: aPattern in: aCollection
- "Assumes that the collection is a collection of Strings"
-
- ^ self allSortedByScoreMatching: aPattern in: aCollection by: [ :each | each ]
+ "Assumes that the collection is a collection of Strings"
+
+ ^ self allSortedByScoreMatching: aPattern in: aCollection by: [ :each | each ]
+
+ "
+ self
+ allSortedByScoreMatching:'clu'
+ in:(Smalltalk allClasses collect:#name)
+ "
+ "
+ self
+ allSortedByScoreMatching:'nary'
+ in:(Smalltalk allClasses collect:#name)
+ "
+
+ "Modified (comment): / 14-07-2017 / 12:18:54 / cg"
!
allSortedByScoreMatching: aPattern in: aCollection by: aBlockReturningString
- | matcher matches |
-
- aPattern isEmpty ifTrue: [ ^ aCollection asArray ].
-
- matcher := self pattern: aPattern.
- matches := OrderedCollection new: aCollection size // 2.
-
- aCollection do: [ :each |
- matcher
- match: (aBlockReturningString value: each)
- ifScored: [ :score | matches add: score -> each ]
- ].
-
- matches sort: [ :a :b | a key >= b key ].
-
- ^ matches collect: [ :each | each value ] as: Array
-
+ | matcher matches |
+
+ aPattern isEmpty ifTrue: [ ^ aCollection asArray ].
+
+ matcher := self pattern: aPattern.
+ matches := OrderedCollection new: aCollection size // 2.
+
+ aCollection do: [ :each |
+ matcher
+ match: (aBlockReturningString value: each)
+ ifScored: [ :score | matches add: score -> each ]
+ ].
+
+ matches sort: [ :a :b | a key >= b key].
+ ^ matches collect: [ :each | each value ] as: Array
+
+ "
+ self
+ allSortedByScoreMatching:'nary'
+ in:(Smalltalk allClasses)
+ by:[:cls | cls name]
+ "
+ "
+ self
+ allSortedByScoreMatching:'nary'
+ in:(Smalltalk allClasses)
+ by:#name
+ "
+
+ "Modified (format): / 14-07-2017 / 12:18:48 / cg"
! !
!FuzzyMatcher methodsFor:'accessing'!