2.3.0 • Published 1 year ago

bad-word-detector v2.3.0

Weekly downloads
9
License
MIT
Repository
github
Last release
1 year ago

bad-word-detector

Detect if Japanese/English input string contains blacklisted words, while allowing specified whitelisted words that contain blacklisted words.

Normalizes input string in the following ways:

  • Removes accents from latin characters (ëe)
  • Fixes full-width latin characters to half-width (fruitfruit)
  • Fixes half-width katakana to full-width (ジュースジュース)
  • Converts all hiragana to katakana (じゅーすジュース)
  • Removes spaces & special characters

Optionally, you may specify a mode for each word in the bad word list to further control what gets marked as 'bad':

enum DetectionMode {
	Default = 1,
	ExactMatch = 2,
	UnNormalizedOnly = 3,
	UnNormalizedOnlyExactMatch = 4,
};

Example isBad outputs for the word apples with each detection mode are as follows:

inputDefaultExactMatchUnNormalizedOnlyUnNormalizedOnlyExactMatch
applestruetruetruetrue
APPLEStruetruefalsefalse
iloveapplestruefalsetruefalse
iloveAPPLEStruefalsefalsefalse

Example isBad outputs for the word モモ with each detection mode are as follows:

inputDefaultExactMatchUnNormalizedOnlyUnNormalizedOnlyExactMatch
モモtruetruetruetrue
ももtruetruefalsefalse
モモが食べたいtruefalsetruefalse
ももが食べたいtruefalsefalsefalse

Usage

const badWordList = {
	苺: {
		whitelist: ["苺大福"],
	},
	ジュース: {
		whitelist: [],
	},
	watermelon: {
		mode: 2,
		whitelist: [],
	}
};

const badWordDetector = new BadWordDetector(badWordList);

badWordDetector.isBad("苺が食べたい"); // true
badWordDetector.isBad("ジュース"); // true
badWordDetector.isBad("じゅーす"); // true
badWordDetector.isBad("wat🍉ermëlon"); // true

badWordDetector.isBad("苺大福が食べたい"); // false
badWordDetector.isBad("シューズ"); // false
badWordDetector.isBad("シュース"); // false
badWordDetector.isBad("melon"); // false
badWordDetector.isBad("water"); // false
badWordDetector.isBad("containswatermelonbutok"); // false
badWordDetector.isBad("i lovewat🍉ermëlon"); // false
2.3.0

1 year ago

2.2.0

1 year ago

2.1.1

4 years ago

2.1.0

4 years ago

2.0.0

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.0

4 years ago