Skip to content

Commit

Permalink
Fixing maxLength and added more tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
cardilloscreations committed Dec 17, 2019
1 parent 440d069 commit e8a4eaf
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 6 deletions.
14 changes: 11 additions & 3 deletions lib/src/refined_soundex.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ class RefinedSoundex implements PhoneticEncoder {
/// ignore the input character and do not encode it (e.g., vowels).
final Map<int, int> soundexMapping;

/// Maximum length of the encoding (and how much to pad if [paddingEnabled]).
final int maxLength;

/// This is a default mapping of the 26 letters used in US English.
static const Map<int, int> defaultMapping = {
$A: $0,
Expand Down Expand Up @@ -69,12 +72,13 @@ class RefinedSoundex implements PhoneticEncoder {
//#region Constructors

/// Private constructor for initializing an instance.
RefinedSoundex._internal(this.soundexMapping);
RefinedSoundex._internal(this.soundexMapping, this.maxLength);

/// Creates a custom Soundex instance. This constructor can be used to
/// provide custom mappings for non-Western character sets, etc.
factory RefinedSoundex.fromMapping(final Map<int, int> soundexMapping) =>
RefinedSoundex._internal(Map.unmodifiable(soundexMapping));
factory RefinedSoundex.fromMapping(final Map<int, int> soundexMapping,
{int maxLength}) =>
RefinedSoundex._internal(Map.unmodifiable(soundexMapping), maxLength);

/// Gets the [defaultEncoder] instance of a RefinedSoundex encoder.
factory RefinedSoundex() => defaultEncoder;
Expand Down Expand Up @@ -109,6 +113,10 @@ class RefinedSoundex implements PhoneticEncoder {
soundex.writeCharCode(current);
}

if (maxLength != null && soundex.length >= maxLength) {
break;
}

last = current;
}

Expand Down
4 changes: 2 additions & 2 deletions lib/src/soundex.dart
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,13 @@ class Soundex implements PhoneticEncoder {
}
}

if (soundex.length >= maxLength) {
if (maxLength != null && soundex.length >= maxLength) {
break;
}
}

// pad the encoding if required
if (paddingEnabled) {
if (paddingEnabled && maxLength != null) {
while (soundex.length < maxLength) {
soundex.writeCharCode(paddingChar);
}
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: dart_phonetics
description: A collection of phonetic algorithms. These algorithms help find words or names that sound similar by generating an encoding that can be compared or indexed for fuzzy searching.
version: 0.1.0-dev.3
version: 0.1.0-dev.4
homepage: https://github.com/raycardillo/dart_phonetics
repository: https://github.com/raycardillo/dart_phonetics

Expand Down
34 changes: 34 additions & 0 deletions test/refined_soundex_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,40 @@ void main() {
expectEncoding(soundex, 'dogs', 'D6043');
});

test('test max length', () {
final soundex4 = RefinedSoundex.fromMapping(RefinedSoundex.defaultMapping,
maxLength: 4);
expectEncoding(soundex4, 'testing', 'T603');
expectEncoding(soundex4, 'The', 'T60');
expectEncoding(soundex4, 'quick', 'Q503');
expectEncoding(soundex4, 'brown', 'B190');
expectEncoding(soundex4, 'fox', 'F205');
expectEncoding(soundex4, 'jumped', 'J408');
expectEncoding(soundex4, 'over', 'O020');
expectEncoding(soundex4, 'the', 'T60');
expectEncoding(soundex4, 'lazy', 'L705');
expectEncoding(soundex4, 'dogs', 'D604');

final soundex5 = RefinedSoundex.fromMapping(RefinedSoundex.defaultMapping,
maxLength: 5);
expectEncoding(soundex5, 'testing', 'T6036');
expectEncoding(soundex5, 'The', 'T60');
expectEncoding(soundex5, 'quick', 'Q503');
expectEncoding(soundex5, 'brown', 'B1908');
expectEncoding(soundex5, 'fox', 'F205');
expectEncoding(soundex5, 'jumped', 'J4081');
expectEncoding(soundex5, 'over', 'O0209');
expectEncoding(soundex5, 'the', 'T60');
expectEncoding(soundex5, 'lazy', 'L7050');
expectEncoding(soundex5, 'dogs', 'D6043');

final soundex20 = RefinedSoundex.fromMapping(
RefinedSoundex.defaultMapping,
maxLength: 20);
expectEncoding(soundex20, 'testing', 'T6036084');
expectEncoding(soundex20, 'supercalifragilistic', 'S3010930702904070360');
});

test('test irregular characters', () {
final soundex = RefinedSoundex();

Expand Down
43 changes: 43 additions & 0 deletions test/soundex_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,49 @@ void main() {
expectEncoding(soundex, 'dogs', 'D200');
});

test('test max length', () {
final soundex3 = Soundex.fromMapping(Soundex.americanMapping,
maxLength: 3);
expectEncoding(soundex3, 'testing', 'T23');
expectEncoding(soundex3, 'The', 'T00');
expectEncoding(soundex3, 'quick', 'Q20');
expectEncoding(soundex3, 'brown', 'B65');
expectEncoding(soundex3, 'fox', 'F20');
expectEncoding(soundex3, 'jumped', 'J51');
expectEncoding(soundex3, 'over', 'O16');
expectEncoding(soundex3, 'the', 'T00');
expectEncoding(soundex3, 'lazy', 'L20');
expectEncoding(soundex3, 'dogs', 'D20');

final soundex5 = Soundex.fromMapping(Soundex.americanMapping,
maxLength: 5);
expectEncoding(soundex5, 'testing', 'T2352');
expectEncoding(soundex5, 'The', 'T0000');
expectEncoding(soundex5, 'quick', 'Q2000');
expectEncoding(soundex5, 'brown', 'B6500');
expectEncoding(soundex5, 'fox', 'F2000');
expectEncoding(soundex5, 'jumped', 'J5130');
expectEncoding(soundex5, 'over', 'O1600');
expectEncoding(soundex5, 'the', 'T0000');
expectEncoding(soundex5, 'lazy', 'L2000');
expectEncoding(soundex5, 'dogs', 'D2000');

final soundex20 = Soundex.fromMapping(Soundex.americanMapping,
maxLength: 20);
expectEncoding(soundex20, 'testing', 'T2352000000000000000');
expectEncoding(soundex20, 'supercalifragilistic', 'S1624162423200000000');

final soundex10 = Soundex.fromMapping(Soundex.americanMapping,
maxLength: 10, paddingEnabled: false);
expectEncoding(soundex10, 'testing', 'T2352');
expectEncoding(soundex10, 'supercalifragilistic', 'S162416242');

final soundexNoMax = Soundex.fromMapping(Soundex.americanMapping,
maxLength: null);
expectEncoding(soundexNoMax, 'testing', 'T2352');
expectEncoding(soundexNoMax, 'supercalifragilistic', 'S16241624232');
});

test('test irregular characters', () {
final soundex = Soundex();

Expand Down

0 comments on commit e8a4eaf

Please sign in to comment.