Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tool to detect strange characters in texts. #109

Closed
PhilipMay opened this issue Dec 10, 2023 · 1 comment · Fixed by #113
Closed

Add tool to detect strange characters in texts. #109

PhilipMay opened this issue Dec 10, 2023 · 1 comment · Fixed by #113
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@PhilipMay
Copy link
Member

No description provided.

@PhilipMay
Copy link
Member Author

PhilipMay commented Dec 10, 2023

Like this?

from collections import Counter
from dataclasses import dataclass, field
import unicodedata

@dataclass
class CharCounter():

    char_counter: Counter = field(init=False, repr=False)

    def __post_init__(self):
        """Do post init."""
        self.char_counter = Counter()

    def fit(self, text: str):
        """Fit counter with text."""
        self.char_counter.update(text)

    # print top n uncommen characters in char_counter
    def print_top_n(self, n: int = 10):
        """Print top n uncommen characters in char_counter."""
        rare_chars = self.char_counter.most_common()[-n:]

        # print rare chars with their count unicode and hex representation
        for char, count in rare_chars:
            print(f">{char}< {hex(ord(char))} {unicodedata.category(char)}")

@PhilipMay PhilipMay added enhancement New feature or request help wanted Extra attention is needed labels Dec 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant