@sideid/id-profanity-filter 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. package/.eslintrc.js +16 -0
  2. package/.github/workflows/ci.yml +0 -0
  3. package/CONTRIBUTING.md +151 -0
  4. package/LICENSE +21 -0
  5. package/README.md +285 -0
  6. package/dist/constants/categories/insult.d.ts +4 -0
  7. package/dist/constants/categories/sexual.d.ts +4 -0
  8. package/dist/constants/regions/batak.d.ts +4 -0
  9. package/dist/constants/regions/betawi.d.ts +4 -0
  10. package/dist/constants/regions/general.d.ts +4 -0
  11. package/dist/constants/regions/jawa.d.ts +4 -0
  12. package/dist/constants/regions/sunda.d.ts +4 -0
  13. package/dist/constants/wordList.d.ts +34 -0
  14. package/dist/core/analyzer.d.ts +54 -0
  15. package/dist/core/filter.d.ts +17 -0
  16. package/dist/core/matcher.d.ts +31 -0
  17. package/dist/index.d.ts +71 -0
  18. package/dist/index.esm.js +1014 -0
  19. package/dist/index.esm.js.map +1 -0
  20. package/dist/index.js +1031 -0
  21. package/dist/index.js.map +1 -0
  22. package/dist/types/index.d.ts +69 -0
  23. package/examples/basic.ts +52 -0
  24. package/jest.config.js +10 -0
  25. package/package.json +51 -0
  26. package/prettierrc +7 -0
  27. package/rollup.config.js +35 -0
  28. package/src/config/options.ts +0 -0
  29. package/src/constants/categories/blasphemy.ts +0 -0
  30. package/src/constants/categories/disgusting.ts +0 -0
  31. package/src/constants/categories/drugs.ts +0 -0
  32. package/src/constants/categories/index.ts +31 -0
  33. package/src/constants/categories/insult.ts +114 -0
  34. package/src/constants/categories/profanity.ts +0 -0
  35. package/src/constants/categories/sexual.ts +100 -0
  36. package/src/constants/categories/slur.ts +0 -0
  37. package/src/constants/regions/aceh.ts +0 -0
  38. package/src/constants/regions/ambon.ts +0 -0
  39. package/src/constants/regions/bali.ts +0 -0
  40. package/src/constants/regions/banjar.ts +0 -0
  41. package/src/constants/regions/batak.ts +17 -0
  42. package/src/constants/regions/betawi.ts +39 -0
  43. package/src/constants/regions/bugis.ts +0 -0
  44. package/src/constants/regions/general.ts +111 -0
  45. package/src/constants/regions/index.ts +62 -0
  46. package/src/constants/regions/jawa.ts +102 -0
  47. package/src/constants/regions/lampung.ts +0 -0
  48. package/src/constants/regions/madura.ts +0 -0
  49. package/src/constants/regions/manado.ts +0 -0
  50. package/src/constants/regions/minang.ts +0 -0
  51. package/src/constants/regions/ntb.ts +0 -0
  52. package/src/constants/regions/ntt.ts +0 -0
  53. package/src/constants/regions/palembang.ts +0 -0
  54. package/src/constants/regions/papua.ts +0 -0
  55. package/src/constants/regions/sunda.ts +35 -0
  56. package/src/constants/wordList.ts +125 -0
  57. package/src/core/analyzer.ts +204 -0
  58. package/src/core/filter.ts +129 -0
  59. package/src/core/matcher.ts +267 -0
  60. package/src/index.ts +133 -0
  61. package/src/types/index.ts +115 -0
  62. package/src/utils/regexUtils.ts +0 -0
  63. package/src/utils/stringUtils.ts +0 -0
  64. package/tsconfig.json +115 -0
@@ -0,0 +1,111 @@
1
+ import { ProfanityWord } from '../../types';
2
+
3
+ export const general: ProfanityWord[] = [
4
+ {
5
+ word: 'anjing',
6
+ category: 'profanity',
7
+ region: 'general',
8
+ severity: 0.7,
9
+ aliases: ['anjay', 'anjir', 'anying', 'njing', 'anj'],
10
+ description: 'Mengacu pada hewan anjing, digunakan sebagai umpatan',
11
+ context: 'Umpatan umum untuk menunjukkan kemarahan atau ketidaksetujuan',
12
+ },
13
+ {
14
+ word: 'babi',
15
+ category: 'profanity',
16
+ region: 'general',
17
+ severity: 0.6,
18
+ aliases: ['bab1', 'b4b1'],
19
+ description: 'Mengacu pada hewan babi, digunakan sebagai umpatan',
20
+ context: 'Umpatan umum untuk menunjukkan kemarahan atau ketidaksetujuan',
21
+ },
22
+ {
23
+ word: 'bangsat',
24
+ category: 'insult',
25
+ region: 'general',
26
+ severity: 0.8,
27
+ aliases: ['bangst', 'bngst', 'bgst'],
28
+ description:
29
+ 'Secara harfiah berarti kutu busuk, digunakan sebagai umpatan untuk menyebut seseorang yang tidak bermoral',
30
+ context:
31
+ 'Umpatan kasar untuk menyebut orang yang dianggap jahat atau merugikan',
32
+ },
33
+ {
34
+ word: 'kontol',
35
+ category: 'sexual',
36
+ region: 'general',
37
+ severity: 0.9,
38
+ aliases: ['kntl', 'k0ntol', 'k0nt0l'],
39
+ description: 'Kata vulgar yang mengacu pada alat kelamin laki-laki',
40
+ context: 'Umpatan kasar atau istilah vulgar untuk alat kelamin',
41
+ },
42
+ {
43
+ word: 'memek',
44
+ category: 'sexual',
45
+ region: 'general',
46
+ severity: 0.9,
47
+ aliases: ['mmk', 'memk'],
48
+ description: 'Kata vulgar yang mengacu pada alat kelamin perempuan',
49
+ context: 'Umpatan kasar atau istilah vulgar untuk alat kelamin',
50
+ },
51
+ {
52
+ word: 'bego',
53
+ category: 'insult',
54
+ region: 'general',
55
+ severity: 0.5,
56
+ aliases: ['bgo', 'begok'],
57
+ description: 'Kata yang mengacu pada kebodohan seseorang',
58
+ context: 'Hinaan untuk menyebut orang yang dianggap tidak pintar',
59
+ },
60
+ {
61
+ word: 'tolol',
62
+ category: 'insult',
63
+ region: 'general',
64
+ severity: 0.6,
65
+ aliases: ['tll', 'tlol'],
66
+ description: 'Kata yang mengacu pada kebodohan seseorang',
67
+ context: 'Hinaan untuk menyebut orang yang dianggap tidak pintar',
68
+ },
69
+ {
70
+ word: 'bajingan',
71
+ category: 'insult',
72
+ region: 'general',
73
+ severity: 0.7,
74
+ aliases: ['bajingn', 'bjgn'],
75
+ description: 'Kata yang mengacu pada orang jahat atau tidak bermoral',
76
+ context:
77
+ 'Hinaan untuk menyebut orang yang dianggap jahat atau tidak bermoral',
78
+ },
79
+ {
80
+ word: 'goblok',
81
+ category: 'insult',
82
+ region: 'general',
83
+ severity: 0.6,
84
+ aliases: ['gblk', 'goblk'],
85
+ description: 'Kata yang mengacu pada kebodohan seseorang',
86
+ context: 'Hinaan untuk menyebut orang yang dianggap tidak pintar',
87
+ },
88
+ {
89
+ word: 'keparat',
90
+ category: 'insult',
91
+ region: 'general',
92
+ severity: 0.7,
93
+ aliases: ['kprt', 'keprat'],
94
+ description: 'Kata yang mengacu pada orang jahat atau tidak bermoral',
95
+ context:
96
+ 'Hinaan untuk menyebut orang yang dianggap jahat atau tidak bermoral',
97
+ },
98
+ {
99
+ word: 'bacot',
100
+ category: 'insult',
101
+ region: 'general',
102
+ severity: 0.5,
103
+ aliases: ['bcot', 'bacot2'],
104
+ description: 'Kata yang mengacu pada orang yang banyak bicara',
105
+ context: 'Hinaan untuk menyebut orang yang banyak bicara atau cerewet',
106
+ },
107
+ ];
108
+
109
+ export const generalWords = general.map((item) => item.word);
110
+
111
+ export default general;
@@ -0,0 +1,62 @@
1
+ // import { general } from './general';
2
+ // import { jawa } from './jawa';
3
+ // import { sunda } from './sunda';
4
+ // import { betawi } from './betawi';
5
+ // import { batak } from './batak';
6
+ // import { minang } from './minang';
7
+ // import { bali } from './bali';
8
+ // import { madura } from './madura';
9
+ // import { bugis } from './bugis';
10
+ // import { aceh } from './aceh';
11
+ // import { ambon } from './ambon';
12
+ // import { papua } from './papua';
13
+ // import { manado } from './manado';
14
+ // import { banjar } from './banjar';
15
+ // import { palembang } from './palembang';
16
+ // import { lampung } from './lampung';
17
+ // import { ntt } from './ntt';
18
+ // import { ntb } from './ntb';
19
+
20
+ // export {
21
+ // general,
22
+ // jawa,
23
+ // sunda,
24
+ // betawi,
25
+ // batak,
26
+ // minang,
27
+ // bali,
28
+ // madura,
29
+ // bugis,
30
+ // aceh,
31
+ // ambon,
32
+ // papua,
33
+ // manado,
34
+ // banjar,
35
+ // palembang,
36
+ // lampung,
37
+ // ntt,
38
+ // ntb,
39
+ // };
40
+
41
+ // export const allRegionWords = [
42
+ // ...general,
43
+ // ...jawa,
44
+ // ...sunda,
45
+ // ...betawi,
46
+ // ...batak,
47
+ // ...minang,
48
+ // ...bali,
49
+ // ...madura,
50
+ // ...bugis,
51
+ // ...aceh,
52
+ // ...ambon,
53
+ // ...papua,
54
+ // ...manado,
55
+ // ...banjar,
56
+ // ...palembang,
57
+ // ...lampung,
58
+ // ...ntt,
59
+ // ...ntb,
60
+ // ];
61
+
62
+ // export default allRegionWords;
@@ -0,0 +1,102 @@
1
+ import { ProfanityWord } from '../../types';
2
+
3
+ export const jawa: ProfanityWord[] = [
4
+ {
5
+ word: 'asu',
6
+ category: 'profanity',
7
+ region: 'jawa',
8
+ severity: 0.7,
9
+ aliases: ['asyu', 'su'],
10
+ description: 'Secara harfiah berarti anjing dalam Bahasa Jawa',
11
+ context: 'Umpatan umum dalam Bahasa Jawa untuk menunjukkan kemarahan',
12
+ },
13
+ {
14
+ word: 'jancok',
15
+ category: 'sexual',
16
+ region: 'jawa',
17
+ severity: 0.8,
18
+ aliases: ['jancuk', 'jncok', 'jancuk', 'jncuk', 'dancok', 'dancuk'],
19
+ description: 'Kata umpatan kasar dalam Bahasa Jawa',
20
+ context: 'Umpatan kasar yang umum digunakan di Jawa Timur',
21
+ },
22
+ {
23
+ word: 'cuk',
24
+ category: 'sexual',
25
+ region: 'jawa',
26
+ severity: 0.7,
27
+ aliases: ['cok', 'cook'],
28
+ description: 'Singkatan dari jancok/jancuk',
29
+ context: 'Umpatan singkat yang umum digunakan di Jawa Timur',
30
+ },
31
+ {
32
+ word: 'diancok',
33
+ category: 'sexual',
34
+ region: 'jawa',
35
+ severity: 0.8,
36
+ aliases: ['diancuk', 'dancok', 'ancok'],
37
+ description: 'Variasi dari jancok/jancuk',
38
+ context: 'Umpatan kasar yang umum digunakan di Jawa Timur',
39
+ },
40
+ {
41
+ word: 'matamu',
42
+ category: 'insult',
43
+ region: 'jawa',
44
+ severity: 0.5,
45
+ aliases: ['mripat mu', 'matane'],
46
+ description:
47
+ 'Secara harfiah berarti matamu, digunakan sebagai umpatan ringan',
48
+ context:
49
+ 'Umpatan ringan untuk menanggapi sesuatu yang dianggap tidak benar',
50
+ },
51
+ {
52
+ word: 'mbokne ancok',
53
+ category: 'insult',
54
+ region: 'jawa',
55
+ severity: 0.8,
56
+ aliases: ['mbokne', 'mbokneancok'],
57
+ description: 'Umpatan yang menyinggung ibu seseorang',
58
+ context: 'Umpatan kasar yang menyinggung orangtua orang lain',
59
+ },
60
+ {
61
+ word: 'pekok',
62
+ category: 'insult',
63
+ region: 'jawa',
64
+ severity: 0.6,
65
+ aliases: ['pekak', 'pekilk'],
66
+ description: 'Kata hinaan yang menunjukkan kebodohan',
67
+ context: 'Hinaan untuk menyebut orang yang dianggap sangat bodoh',
68
+ },
69
+ {
70
+ word: 'sempak',
71
+ category: 'disgusting',
72
+ region: 'jawa',
73
+ severity: 0.5,
74
+ aliases: ['sempok'],
75
+ description: 'Mengacu pada pakaian dalam',
76
+ context: 'Umpatan ringan yang dianggap jorok',
77
+ },
78
+ {
79
+ word: 'taek',
80
+ category: 'disgusting',
81
+ region: 'jawa',
82
+ severity: 0.6,
83
+ aliases: ['tai', 'tahi', 'telek'],
84
+ description: 'Secara harfiah berarti kotoran/tinja',
85
+ context: 'Umpatan untuk menunjukkan sesuatu yang menjijikkan atau buruk',
86
+ },
87
+ {
88
+ word: 'ndhasmu',
89
+ category: 'insult',
90
+ region: 'jawa',
91
+ severity: 0.5,
92
+ aliases: ['ndas mu', 'dhasmu'],
93
+ description:
94
+ 'Secara harfiah berarti kepalamu, digunakan sebagai umpatan ringan',
95
+ context:
96
+ 'Umpatan ringan untuk menanggapi sesuatu yang dianggap tidak benar',
97
+ },
98
+ ];
99
+
100
+ export const jawaWords = jawa.map((item) => item.word);
101
+
102
+ export default jawa;
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
@@ -0,0 +1,35 @@
1
+ import { ProfanityWord } from '../../types';
2
+
3
+ export const sunda: ProfanityWord[] = [
4
+ {
5
+ word: 'bagong',
6
+ category: 'insult',
7
+ region: 'sunda',
8
+ severity: 0.5,
9
+ aliases: ['bagog'],
10
+ description: 'Secara harfiah berarti babi hutan, digunakan sebagai hinaan',
11
+ context: 'Hinaan untuk menyebut orang yang dianggap jorok atau rakus',
12
+ },
13
+ {
14
+ word: 'belegug',
15
+ category: 'insult',
16
+ region: 'sunda',
17
+ severity: 0.5,
18
+ aliases: ['belecuk', 'beledog'],
19
+ description: 'Kata yang mengacu pada kebodohan seseorang',
20
+ context: 'Hinaan untuk menyebut orang yang dianggap tidak pintar',
21
+ },
22
+ {
23
+ word: 'goblog',
24
+ category: 'insult',
25
+ region: 'sunda',
26
+ severity: 0.6,
27
+ aliases: ['goblok', 'golbok'],
28
+ description: 'Kata yang mengacu pada kebodohan seseorang',
29
+ context: 'Hinaan untuk menyebut orang yang dianggap tidak pintar',
30
+ },
31
+ ];
32
+
33
+ export const sundaWords = sunda.map((item) => item.word);
34
+
35
+ export default sunda;
@@ -0,0 +1,125 @@
1
+ import { ProfanityWord } from '../types';
2
+
3
+ import { sexual, sexualWords } from './categories/sexual';
4
+ import { insult, insultWords } from './categories/insult';
5
+ // import { profanity, profanityWords } from './categories/profanity';
6
+ // import { slur, slurWords } from './categories/slur';
7
+ // import { drugs, drugsWords } from './categories/drugs';
8
+ // import { disgusting, disgustingWords } from './categories/disgusting';
9
+ // import { blasphemy, blasphemyWords } from './categories/blasphemy';
10
+
11
+ import { general, generalWords } from './regions/general';
12
+ import { jawa, jawaWords } from './regions/jawa';
13
+ import { sunda, sundaWords } from './regions/sunda';
14
+ import { betawi, betawiWords } from './regions/betawi';
15
+ import { batak, batakWords } from './regions/batak';
16
+ // import { minang, minangWords } from './regions/minang';
17
+ // import { bali, baliWords } from './regions/bali';
18
+ // import { madura, maduraWords } from './regions/madura';
19
+ // import { bugis, bugisWords } from './regions/bugis';
20
+ // import { aceh, acehWords } from './regions/aceh';
21
+ // import { ambon, ambonWords } from './regions/ambon';
22
+ // import { papua, papuaWords } from './regions/papua';
23
+ // import { manado, manadoWords } from './regions/manado';
24
+ // import { banjar, banjarWords } from './regions/banjar';
25
+ // import { palembang, palembangWords } from './regions/palembang';
26
+ // import { lampung, lampungWords } from './regions/lampung';
27
+ // import { ntt, nttWords } from './regions/ntt';
28
+ // import { ntb, ntbWords } from './regions/ntb';
29
+
30
+ export const wordCategories = {
31
+ sexual: sexualWords,
32
+ insult: insultWords,
33
+ // profanity: profanityWords,
34
+ // slur: slurWords,
35
+ // drugs: drugsWords,
36
+ // disgusting: disgustingWords,
37
+ // blasphemy: blasphemyWords,
38
+ };
39
+
40
+ export const wordRegions = {
41
+ general: generalWords,
42
+ jawa: jawaWords,
43
+ sunda: sundaWords,
44
+ betawi: betawiWords,
45
+ batak: batakWords,
46
+ // minang: minangWords,
47
+ // bali: baliWords,
48
+ // madura: maduraWords,
49
+ // bugis: bugisWords,
50
+ // aceh: acehWords,
51
+ // ambon: ambonWords,
52
+ // papua: papuaWords,
53
+ // manado: manadoWords,
54
+ // banjar: banjarWords,
55
+ // palembang: palembangWords,
56
+ // lampung: lampungWords,
57
+ // ntt: nttWords,
58
+ // ntb: ntbWords,
59
+ };
60
+
61
+ export const wordObjects: ProfanityWord[] = [
62
+ ...general,
63
+ ...jawa,
64
+ ...sunda,
65
+ ...betawi,
66
+ ...batak,
67
+ // ...minang,
68
+ // ...bali,
69
+ // ...madura,
70
+ // ...bugis,
71
+ // ...aceh,
72
+ // ...ambon,
73
+ // ...papua,
74
+ // ...manado,
75
+ // ...banjar,
76
+ // ...palembang,
77
+ // ...lampung,
78
+ // ...ntt,
79
+ // ...ntb,
80
+ ];
81
+
82
+ export const allWords: string[] = wordObjects.map((item) => item.word);
83
+
84
+ /**
85
+ * Kata-kata dengan tingkat keparahan tinggi (subset dari semua kata)
86
+ */
87
+ export const severeWords: string[] = wordObjects
88
+ .filter((word) => word.severity >= 0.8)
89
+ .map((item) => item.word);
90
+
91
+ /**
92
+ * Mencari kata berdasarkan kategori dan region
93
+ *
94
+ * @param category Kategori kata kotor
95
+ * @param region Daerah asal kata kotor
96
+ * @returns Array dari kata kotor
97
+ */
98
+ export function getWordsByFilter(category?: string, region?: string): string[] {
99
+ return wordObjects
100
+ .filter((word) => {
101
+ const matchCategory = category ? word.category === category : true;
102
+ const matchRegion = region ? word.region === region : true;
103
+ return matchCategory && matchRegion;
104
+ })
105
+ .map((item) => item.word);
106
+ }
107
+
108
+ /**
109
+ * Mencari metadata lengkap untuk sebuah kata
110
+ *
111
+ * @param word Kata yang dicari
112
+ * @returns Objek ProfanityWord jika ditemukan, undefined jika tidak
113
+ */
114
+ export function getWordMetadata(word: string): ProfanityWord | undefined {
115
+ return wordObjects.find(
116
+ (item) =>
117
+ item.word.toLowerCase() === word.toLowerCase() ||
118
+ (item.aliases &&
119
+ item.aliases.some(
120
+ (alias) => alias.toLowerCase() === word.toLowerCase(),
121
+ )),
122
+ );
123
+ }
124
+
125
+ export default allWords;
@@ -0,0 +1,204 @@
1
+ import {
2
+ FilterOptions,
3
+ AnalysisResult,
4
+ ProfanityWord,
5
+ ProfanityCategory,
6
+ Region,
7
+ } from '../types';
8
+ import {
9
+ findProfanity,
10
+ findProfanityWithMetadata,
11
+ findCategories,
12
+ findRegions,
13
+ calculateSeverity,
14
+ } from './matcher';
15
+
16
+ /**
17
+ * Menganalisis teks untuk kata kotor
18
+ *
19
+ * @param text Teks yang akan dianalisis
20
+ * @param options Opsi untuk analisis
21
+ * @return AnalysisResult dengan hasil analisis
22
+ */
23
+ export function analyze(
24
+ text: string,
25
+ options: FilterOptions = {},
26
+ ): AnalysisResult {
27
+ const matches = findProfanity(text, options);
28
+
29
+ if (matches.length === 0) {
30
+ return {
31
+ hasProfanity: false,
32
+ matches: [],
33
+ matchDetails: [],
34
+ categories: [],
35
+ regions: [],
36
+ severityScore: 0,
37
+ };
38
+ }
39
+
40
+ const matchDetails = findProfanityWithMetadata(text, options);
41
+ const categories = findCategories(matchDetails);
42
+ const regions = findRegions(matchDetails);
43
+ const severityScore = calculateSeverity(matchDetails);
44
+
45
+ return {
46
+ hasProfanity: true,
47
+ matches,
48
+ matchDetails,
49
+ categories,
50
+ regions,
51
+ severityScore,
52
+ };
53
+ }
54
+
55
+ /**
56
+ * menganalisis daftar teks dan ringkasan
57
+ *
58
+ * @param texts Daftar teks yagn dianalisis
59
+ * @param options Opsi untuk analisis
60
+ * @return Objek dengan ringkasan analisis
61
+ */
62
+ export function batchAnalyze(
63
+ texts: string[],
64
+ options: FilterOptions = {},
65
+ ): {
66
+ totalTexts: number;
67
+ profaneTexts: number;
68
+ cleanTexts: number;
69
+ averageSeverity: number;
70
+ topCategories: ProfanityCategory[];
71
+ topRegions: Region[];
72
+ mostFrequentWords: Array<{ word: string; count: number }>;
73
+ } {
74
+ const results = texts.map((text) => analyze(text, options));
75
+ const profaneTexts = results.filter((result) => result.hasProfanity).length;
76
+ const totalSeverity = results.reduce(
77
+ (sum, result) => sum + result.severityScore,
78
+ 0,
79
+ );
80
+ const averageSeverity = profaneTexts > 0 ? totalSeverity / profaneTexts : 0;
81
+ const allCategories = results.flatMap((result) => result.categories);
82
+ const categoryCount: Record<string, number> = {};
83
+
84
+ allCategories.forEach((category) => {
85
+ categoryCount[category] = (categoryCount[category] || 0) + 1;
86
+ });
87
+
88
+ const topCategories = Object.entries(categoryCount)
89
+ .sort((a, b) => b[1] - a[1])
90
+ .map(([category]) => category as ProfanityCategory);
91
+
92
+ const allRegions = results.flatMap((result) => result.regions);
93
+ const regionCount: Record<string, number> = {};
94
+
95
+ allRegions.forEach((region) => {
96
+ regionCount[region] = (regionCount[region] || 0) + 1;
97
+ });
98
+
99
+ const topRegions = Object.entries(regionCount)
100
+ .sort((a, b) => b[1] - a[1])
101
+ .map(([region]) => region as Region);
102
+
103
+ const allWords = results.flatMap((result) => result.matches);
104
+ const wordCount: Record<string, number> = {};
105
+
106
+ allWords.forEach((word) => {
107
+ wordCount[word] = (wordCount[word] || 0) + 1;
108
+ });
109
+
110
+ const mostFrequentWords = Object.entries(wordCount)
111
+ .sort((a, b) => b[1] - a[1])
112
+ .slice(0, 10)
113
+ .map(([word, count]) => ({ word, count }));
114
+
115
+ return {
116
+ totalTexts: texts.length,
117
+ profaneTexts,
118
+ cleanTexts: texts.length - profaneTexts,
119
+ averageSeverity,
120
+ topCategories,
121
+ topRegions,
122
+ mostFrequentWords,
123
+ };
124
+ }
125
+
126
+ /**
127
+ * Menganalisis per-kalimat untuk melokalisasi kata kotor
128
+ *
129
+ * @param text Teks yang akan dianalisis per-kalimat
130
+ * @param options Opsi untuk analisis
131
+ * @returns Array hasil analisis per-kalimat
132
+ */
133
+ export function analyzeBySentence(
134
+ text: string,
135
+ options: FilterOptions = {},
136
+ ): Array<AnalysisResult & { sentence: string }> {
137
+ const sentences = text
138
+ .split(/(?<=[.!?])\s+/)
139
+ .filter((sentence) => sentence.trim().length > 0);
140
+
141
+ return sentences.map((sentence) => {
142
+ const result = analyze(sentence, options);
143
+
144
+ return {
145
+ ...result,
146
+ sentence,
147
+ };
148
+ });
149
+ }
150
+
151
+ /**
152
+ * Menganalisis teks untuk menemukan kata kotor pada konteks tertentu
153
+ *
154
+ * @param text Teks yang akan dianalisis
155
+ * @param context Kata konteks untuk dicari di dekat kata kotor
156
+ * @param options Opsi untuk analisis
157
+ * @returns Konteks di dekat kata kotor
158
+ */
159
+ export function analyzeWithContext(
160
+ text: string,
161
+ contextWindowSize: number = 5,
162
+ options: FilterOptions = {},
163
+ ): Array<{
164
+ word: string;
165
+ context: string;
166
+ position: { start: number; end: number };
167
+ }> {
168
+ const matches = findProfanity(text, options);
169
+
170
+ if (matches.length === 0) {
171
+ return [];
172
+ }
173
+
174
+ const result = [];
175
+
176
+ for (const word of matches) {
177
+ const regex = new RegExp(
178
+ `((?:\\S+\\s+){0,${contextWindowSize}})(\\b${escapeRegExp(word)}\\b)((?:\\s+\\S+){0,${contextWindowSize}})`,
179
+ 'gi',
180
+ );
181
+
182
+ let match;
183
+ while ((match = regex.exec(text)) !== null) {
184
+ const beforeContext = match[1] || '';
185
+ const wordMatch = match[2];
186
+ const afterContext = match[3] || '';
187
+
188
+ result.push({
189
+ word: wordMatch,
190
+ context: beforeContext + wordMatch + afterContext,
191
+ position: {
192
+ start: match.index,
193
+ end: match.index + match[0].length,
194
+ },
195
+ });
196
+ }
197
+ }
198
+
199
+ return result;
200
+ }
201
+
202
+ function escapeRegExp(string: string): string {
203
+ return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
204
+ }