Skip to content

Instantly share code, notes, and snippets.

@WahidinAji
Last active January 19, 2024 08:51
Show Gist options
  • Save WahidinAji/cac1bc4324c1e7294cc0b583418c7952 to your computer and use it in GitHub Desktop.
Save WahidinAji/cac1bc4324c1e7294cc0b583418c7952 to your computer and use it in GitHub Desktop.
badWors
package main
import (
"fmt"
"strings"
)
type BadWord struct {
Word string
}
func BadWordList() (badWords []BadWord) {
// "anjing",ajg,anj,anjeng,ANJING,ANJ,ANJENG,Anjing,Anj,Anjeng,4nj1ng,4nj,4nj3ng
anjings := []BadWord{
{Word: "anjing"},
{Word: "ajg"},
{Word: "anj"},
{Word: "anjeng"},
{Word: "ANJING"},
{Word: "ANJ"},
{Word: "AJG"},
{Word: "ANJENG"},
{Word: "Anjing"},
{Word: "Anj"},
{Word: "Anjeng"},
{Word: "4nj1ng"},
{Word: "4nj"},
{Word: "4nj3ng"},
}
// "babi",b4b1,b4bi,B4B1,B4BI,Babi,B4bi
babi := []BadWord{
{Word: "babi"},
{Word: "b4b1"},
{Word: "b4bi"},
{Word: "B4B1"},
{Word: "B4BI"},
{Word: "Babi"},
{Word: "B4bi"},
}
// "bangsat",b4ngs4t,B4NGS4T,Bangs4t,B4ngs4t
bangsats := []BadWord{
{Word: "bangsat"},
{Word: "b4ngs4t"},
{Word: "B4NGS4T"},
{Word: "Bangs4t"},
{Word: "B4ngs4t"},
}
// "kontol",k0nt0l,kont0l,K0NT0L,KONT0L,Kontol,K0ntol
kontols := []BadWord{
{Word: "kontols"},
{Word: "kontol"},
{Word: "k0nt0l"},
{Word: "kont0l"},
{Word: "K0NT0L"},
{Word: "KONT0L"},
{Word: "Kontol"},
{Word: "K0ntol"},
}
// "memek",m3m3k,mem3k,M3M3K,MEM3K,Memek,M3mek
memeks := []BadWord{
{Word: "memeks"},
{Word: "memek"},
{Word: "m3m3k"},
{Word: "mem3k"},
{Word: "M3M3K"},
{Word: "MEM3K"},
{Word: "Memek"},
{Word: "M3mek"},
}
// "ngentot",n93nt0t,ng3nt0t,N93NT0T,NG3NT0T,Ngentot,N93ntot
ngentots := []BadWord{
{Word: "ngentots"},
{Word: "ngentot"},
{Word: "n93nt0t"},
{Word: "ng3nt0t"},
{Word: "N93NT0T"},
{Word: "NG3NT0T"},
{Word: "Ngentot"},
{Word: "N93ntot"},
}
// "pepek",p3p3k,pep3k,P3P3K,PEP3K,Pepek,P3pek
pepeks := []BadWord{
{Word: "pepeks"},
{Word: "pepek"},
{Word: "p3p3k"},
{Word: "pep3k"},
{Word: "P3P3K"},
{Word: "PEP3K"},
{Word: "Pepek"},
{Word: "P3pek"},
}
// "tetek",t3t3k,tet3k,T3T3K,TET3K,Tetek,T3tek
tetek := []BadWord{
{Word: "tetek"},
{Word: "t3t3k"},
{Word: "tet3k"},
{Word: "T3T3K"},
{Word: "TET3K"},
{Word: "Tetek"},
{Word: "T3tek"},
}
// "toket",t0k3t,t0ket,T0K3T,T0KET,Toket,T0ket
toket := []BadWord{
{Word: "toket"},
{Word: "t0k3t"},
{Word: "t0ket"},
{Word: "T0K3T"},
{Word: "T0KET"},
{Word: "Toket"},
{Word: "T0ket"},
}
// "ngewe",n93w3,ng3w3,N93W3,NG3W3,Ngewe,N93we
ngewe := []BadWord{
{Word: "ngewe"},
{Word: "n93w3"},
{Word: "ng3w3"},
{Word: "N93W3"},
{Word: "NG3W3"},
{Word: "Ngewe"},
{Word: "N93we"},
}
// "ngentot",n93nt0t,ng3nt0t,N93NT0T,NG3NT0T,Ngentot,N93ntot
ngentot := []BadWord{
{Word: "ngentot"},
{Word: "n93nt0t"},
{Word: "ng3nt0t"},
{Word: "N93NT0T"},
{Word: "NG3NT0T"},
{Word: "Ngentot"},
{Word: "N93ntot"},
{Word: "ngentots"},
{Word: "n93nt0ts"},
{Word: "ng3nt0ts"},
}
//merge all badwords
badWords = append(anjings, babi...)
badWords = append(badWords, bangsats...)
badWords = append(badWords, kontols...)
badWords = append(badWords, memeks...)
badWords = append(badWords, ngentots...)
badWords = append(badWords, pepeks...)
badWords = append(badWords, tetek...)
badWords = append(badWords, toket...)
badWords = append(badWords, ngewe...)
badWords = append(badWords, ngentot...)
return
}
//solusi pertama, katanya kurang efektif.
func SensorBadWords(in string) (out string) {
//loop every word in str_in and replace with "*", and only return the first and the last swjaracter of the word
//example: "test" -> "t**t"
//example: "test test" -> "t**t t**t"
words := strings.Fields(in)
badwords := BadWordList()
for i, word := range words {
for _, badword := range badwords {
if strings.Contains(word, badword.Word) {
words[i] = word[:1] + strings.Repeat("*", len(word)-2) + word[len(word)-1:]
}
}
}
out = strings.Join(words, " ")
return
}
type TrieNode struct {
starWars [26]*TrieNode
endGame bool
}
func (t *TrieNode) insert(word string) {
node := t
for _, swj := range word {
if swj < 'a' || swj > 'z' {
continue
}
swj -= 'a'
if node.starWars[swj] == nil {
fmt.Println("swj : ", swj)
node.starWars[swj] = &TrieNode{}
}
node = node.starWars[swj]
}
node.endGame = true
}
func (t *TrieNode) search(word string) bool {
node := t
for _, swj := range word {
if swj < 'a' || swj > 'z' {
//fmt.Println("swj : ", swj)
continue
}
swj -= 'a'
if node.starWars[swj] == nil {
return false
}
node = node.starWars[swj]
}
//fmt.Println("node : ", node)
return node != nil && node.endGame
}
func SensorBadWordsTrie(in string) (out string) {
root := &TrieNode{}
badWords := BadWordList()
for _, badWord := range badWords {
//fmt.Println(badWord.Word)
root.insert(strings.ToLower(badWord.Word))
}
words := strings.Fields(in)
for i, word := range words {
if root.search(strings.ToLower(word)) {
//fmt.Println("found badword", word, "at index", i)
words[i] = word[:1] + strings.Repeat("*", len(word)-2) + word[len(word)-1:]
}
}
//fmt.Println("words : ", words)
out = strings.Join(words, " ")
return
}
//
//type NodeTree struct {
// Data string
// Left *NodeTree
// Right *NodeTree
//}
//func (n *NodeTree) Insert(data string) {
// if n.Data == "" {
// n.Data = data
// return
// }
// if data < n.Data {
// if n.Left == nil {
// n.Left = &NodeTree{Data: data}
// return
// }
// n.Left.Insert(data)
// return
// }
// if n.Right == nil {
// n.Right = &NodeTree{Data: data}
// return
// }
// n.Right.Insert(data)
//}
//func PreorderTraversal(node *NodeTree) string {
// if node == nil {
// return ""
// }
// fmt.Println(node.Data)
//
// //badWords := BadWordList()
// //for _, v := range badWords {
// // if strings.Contains(node.Data, v.Word) {
// // fmt.Println("found badword", v.Word, "at index", node.Data)
// // //fmt.Println("\n", node.Data[:1], len(node.Data)-2, node.Data[len(node.Data)-1:])
// // //if node.Data[len(node.Data)-1:] == " " {
// // // node.Data = node.Data[:1] + strings.Repeat("*", len(node.Data)-3) + node.Data[len(node.Data)-2:]
// // //}
// // node.Data = node.Data[:1] + strings.Repeat("*", len(node.Data)-2) + node.Data[len(node.Data)-1:]
// //
// // }
// //}
// //return PreorderTraversal(node.Left) + node.Data + PreorderTraversal(node.Right)
// PreorderTraversal(node.Left)
// PreorderTraversal(node.Right)
// return node.Data
//}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment