Skip to content

Add Thai profanity detection utilities with custom words support - #1183

Merged
bact merged 14 commits into
devfrom
copilot/add-profanity-detection-thai
Jan 15, 2026
Merged

Add Thai profanity detection utilities with custom words support#1183
bact merged 14 commits into
devfrom
copilot/add-profanity-detection-thai

Conversation

Copilot AI commented Jan 9, 2026

Copy link
Copy Markdown
Contributor

What does this changes

Adds profanity detection and filtering capabilities for Thai text with three utility functions: contains_profanity(), find_profanity(), and censor_profanity(). Users can optionally provide custom profanity words in addition to the default list.

What was wrong

PyThaiNLP lacked profanity detection functionality for Thai language content moderation and filtering use cases.

How this fixes it

New corpus data:

  • profanity_th.txt with 23 Thai profanity words
  • thai_profanity_words() loader in corpus.common

New utility functions in pythainlp.util.profanity:

  • contains_profanity(text, custom_words=None, engine="newmm") - Returns bool if profanity detected
  • find_profanity(text, custom_words=None, engine="newmm") - Returns list of profanity words found
  • censor_profanity(text, replacement="*", custom_words=None, engine="newmm") - Replaces profanity with censoring characters

Key features:

  • Custom words support: Users can add their own profanity words via the custom_words parameter (accepts an optional set of strings, type: set[str] | None)
  • Improved tokenization: Uses a custom dictionary that merges thai_words() and profanity words for better word segmentation
  • Flexible censoring: Supports custom replacement characters

Usage:

from pythainlp.util import contains_profanity, find_profanity, censor_profanity

# Basic usage with default profanity list
contains_profanity("สวัสดีครับ")  # False
find_profanity("สวัสดี ควย ครับ")  # ['ควย']
censor_profanity("สวัสดี ควย ครับ")  # 'สวัสดี *** ครับ'
censor_profanity("ควย สัส", replacement="#")  # '### ###'

# Add custom profanity words
contains_profanity("คำใหม่", custom_words={"คำใหม่"})  # True
find_profanity("ควย และ คำใหม่", custom_words={"คำใหม่"})  # ['ควย', 'คำใหม่']
censor_profanity("ควย และ คำใหม่", custom_words={"คำใหม่"})  # '*** และ ******'

All functions use word tokenization with a merged custom dictionary (thai_words + profanity words) for accurate Thai text segmentation and support configurable tokenization engines.

Documentation:

  • Added comprehensive API documentation for all three functions in docs/api/util.rst
  • Functions documented in alphabetical order following project conventions

Compatibility updates:

  • Updated type hints to use Python 3.9+ style (lowercase built-in types: frozenset[str], list[str], set[str] | None)
  • Proper union types for optional parameters (set[str] | None instead of set[str] with None default)
  • Merged with latest dev branch to ensure compatibility with current codebase
  • Aligned with dev branch docstring formatting conventions
  • Updated SPDX headers to 2026 for new files

Your checklist for this pull request

  • Passed code styles and structures
  • Passed code linting checks and unit test
Original prompt

This section details on the original issue you should resolve

<issue_title>Profanity detection</issue_title>
<issue_description>Detect profanity in Thai language****</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits January 9, 2026 06:25
Co-authored-by: wannaphong <8536487+wannaphong@users.noreply.github.com>
Co-authored-by: wannaphong <8536487+wannaphong@users.noreply.github.com>
Copilot AI changed the title [WIP] Add profanity detection for Thai language Add Thai profanity detection utilities Jan 9, 2026
Copilot AI requested a review from wannaphong January 9, 2026 06:30
@coveralls

coveralls commented Jan 9, 2026

Copy link
Copy Markdown

Coverage Status

coverage: 52.629% (-0.2%) from 52.858%
when pulling 5d82c22 on copilot/add-profanity-detection-thai
into f59b908 on dev.

@wannaphong wannaphong left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot I'm reviewed and comment about the task.

Comment thread pythainlp/util/profanity.py Outdated
Comment thread pythainlp/util/profanity.py Outdated
Comment thread pythainlp/util/profanity.py Outdated
- All three functions (contains_profanity, find_profanity, censor_profanity) now accept custom_words parameter
- Implemented custom dictionary that merges thai_words and profanity_set for better tokenization
- Added comprehensive tests for custom words functionality
- All 11 tests pass

Co-authored-by: wannaphong <8536487+wannaphong@users.noreply.github.com>
Copilot AI changed the title Add Thai profanity detection utilities Add Thai profanity detection utilities with custom words support Jan 9, 2026
Copilot AI requested a review from wannaphong January 9, 2026 15:02
@wannaphong
wannaphong marked this pull request as ready for review January 10, 2026 15:33
@bact bact added the enhancement enhance functionalities label Jan 10, 2026
@wannaphong

Copy link
Copy Markdown
Member

@copilot Fix This branch has conflicts that must be resolved.

Copilot AI and others added 2 commits January 11, 2026 16:40
- Changed FrozenSet[str] to frozenset[str]
- Changed List[str] to list[str]
- Changed Set[str] to set[str]
- Removed unnecessary typing imports
- Aligns with dev branch type hint style

Co-authored-by: wannaphong <8536487+wannaphong@users.noreply.github.com>
- Resolved merge conflicts in pythainlp/corpus/common.py
- Added profanity detection feature to merged codebase
- Updated to use Python 3.9+ type hints (lowercase built-in types)
- All tests pass (11 profanity tests)

Co-authored-by: wannaphong <8536487+wannaphong@users.noreply.github.com>

Copilot AI commented Jan 11, 2026

Copy link
Copy Markdown
Contributor Author

@copilot Fix This branch has conflicts that must be resolved.

Fixed in commit a8b17f7. Successfully merged with dev branch and resolved all conflicts. The branch now uses Python 3.9+ style type hints (lowercase built-in types) and includes all latest changes from dev. All 11 tests pass.

@wannaphong

Copy link
Copy Markdown
Member

@copilot Add docs to docs/api/util.rst

- Added censor_profanity documentation
- Added contains_profanity documentation
- Added find_profanity documentation
- Functions documented in alphabetical order with descriptions

Co-authored-by: wannaphong <8536487+wannaphong@users.noreply.github.com>

Copilot AI commented Jan 12, 2026

Copy link
Copy Markdown
Contributor Author

@copilot Add docs to docs/api/util.rst

Added documentation in commit 7dd940d. All three profanity detection functions (contains_profanity, find_profanity, censor_profanity) are now documented in docs/api/util.rst in alphabetical order.

Comment thread pythainlp/corpus/profanity_th.txt Outdated
Comment thread pythainlp/util/profanity.py Outdated
Comment thread tests/core/test_profanity.py Outdated
Comment thread pythainlp/util/profanity.py Outdated
…] | None

- Fixed type hint for custom_words parameter in contains_profanity()
- Fixed type hint for custom_words parameter in find_profanity()
- Fixed type hint for custom_words parameter in censor_profanity()
- All 11 tests pass

Co-authored-by: bact <128572+bact@users.noreply.github.com>
Copilot AI requested a review from bact January 12, 2026 13:03
Comment thread pythainlp/util/profanity.py
@sonarqubecloud

Copy link
Copy Markdown

@bact
bact self-requested a review January 12, 2026 15:27
@bact
bact merged commit 28fe327 into dev Jan 15, 2026
42 of 44 checks passed
@bact bact added this to the 5.3 milestone Jan 30, 2026
@bact
bact deleted the copilot/add-profanity-detection-thai branch January 30, 2026 14:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement enhance functionalities

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Profanity detection

4 participants