Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9172637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:23:48+00:00 2026-06-17T16:23:48+00:00

Here’s a simple scanner, that tokenizes text according to certain rules, and labels the

  • 0

Here’s a simple scanner, that tokenizes text according to certain rules, and labels the tokens.

  1. What is the best way to handle unknown characters, and label them as
    unknown?
  2. Is there a recommended way/library to speed things up while
    accomplishing similar results and remaining relatively simple.

Example:

import re

def alpha(scanner,token):
    return token, 'a'

def numeric(scanner,token):
    return token,'rn'

def punctuation(scanner,token):
    return token, 'p'

def superscript(scanner,token):
    return token, 'sn'

scanner = re.Scanner([
    (u"[a-zA-Z]+", alpha),
    (u"[.,:;!?]", punctuation),
    (u"[0-9]+", numeric),
    (u"[\xb9\u2070\xb3\xb2\u2075\u2074\u2077\u2076\u2079\u2078]", superscript),
    (r"[\s\n]+", None), # whitespace, newline
    ])

tokens, _ = scanner.scan("This is a little test? With 7,9 and 6.")
print tokens

out:

[('This', 'a'), ('is', 'a'), ('a', 'a'), ('little', 'a'), ('test', 'a'),
 ('?', 'p'), ('With', 'a'), ('7', 'rn'), (',', 'p'), ('9', 'rn'), 
 ('and', 'a'), ('6', 'rn'), ('.', 'p')]

ps! Defined functions will probably try to categorize the tokens further.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T16:23:49+00:00Added an answer on June 17, 2026 at 4:23 pm

    The re.Scanner matches patterns in the order provided. So you can provide a very general pattern at the end to catch “unknown” characters:

    (r".", unknown)
    

    import re
    
    def alpha(scanner,token):
        return token, 'a'
    
    def numeric(scanner,token):
        return token,'rn'
    
    def punctuation(scanner,token):
        return token, 'p'
    
    def superscript(scanner,token):
        return token, 'sn'
    
    def unknown(scanner,token):
        return token, 'uk'
    
    scanner = re.Scanner([
        (r"[a-zA-Z]+", alpha),
        (r"[.,:;!?]", punctuation),
        (r"[0-9]+", numeric),
        (r"[\xb9\u2070\xb3\xb2\u2075\u2074\u2077\u2076\u2079\u2078]", superscript),
        (r"[\s\n]+", None), # whitespace, newline
        (r".", unknown)
        ])
    
    tokens, _ = scanner.scan("This is a little test? With 7,9 and 6. \xa0-\xaf")
    print tokens
    

    yields

    [('This', 'a'), ('is', 'a'), ('a', 'a'), ('little', 'a'), 
    ('test', 'a'), ('?', 'p'), ('With', 'a'), ('7', 'rn'), (',', 'p'), 
    ('9', 'rn'), ('and', 'a'), ('6', 'rn'), ('.', 'p'), ('\xa0', 'uk'), 
    ('-', 'uk'), ('\xaf', 'uk')]
    

    Some of your patterns are unicode, and one is a str. It is true that in Python2 the pattern and the strings to be matched can be either unicode or str.

    However, in Python3:

    Unicode strings and 8-bit strings cannot be mixed: that is, you cannot
    match an Unicode string with a byte pattern or vice-versa

    It is good practice, therefore, not to mix them, even in Python2.


    I think your code is wonderfully simple (except for superscript regex. Eek!). I don’t know of a library which would make it any simpler.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the Javascript I currently have <script type=text/javascript> $(function() { $('.slideshow').hover( function() {
Here is the script I'm using, copied directly from Google: <script type=text/javascript> var _gaq
Here's the setup - I have a view that lists products. On that same
Here is a scenario: User installs .NET application that you have made. After some
Here's a sample of a SpinBox that writes its changes to underlying variables. The
Here is my scenario. I have a website running under AppPool1 and that works
Here's the code I have. It works. The only problem is that the first
Here's the scenario: I have a local git repository that mirrors the contents of
Here's the problem....I have three components...A Page that contains a User Control and a
Here's my situation. I've noticed that code gets harder to maintain when you keep

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.