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 6615777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:30:15+00:00 2026-05-25T20:30:15+00:00

I’m trying to set up some sample data for a Naive Bayesian Classifier for

  • 0

I’m trying to set up some sample data for a Naive Bayesian Classifier for Twitter.

One of the post-processing of the tweets I’d like to do is to remove unnecessary repeat characters.

For example, one of the tweets reads: Twizzlers. mmmmm goooooooooooood!

I’d like to reduce the number of w’s down to just two. Why two? That’s what the article I’m following did. Any individual word that is less than 2 characters is discarded (see mmmmm above). And as far as gooooooood, I would imagine double letters are the most common to be uber repeated.

So, that said, what’s the fastest way (in terms of execution time) to reduce words such as gooooooooood to simply good?

[Edit]
I’ll be processing 800,000 tweets in this app, hence the requirement for fastest execution
[/Edit]

[Edit2]
I just ran some simple benchmarking based on elapsed time to iterate through 1000 records & save to a text file. I repeated this iteration 100 times on each method. The average results are here:

Method 1: 386 ms [LINQ – answer was deleted]
Method 2: 407 ms [Regex]
Method 3: 303 ms [StringBuilder]
Method 4: 301 ms [StringBuilder part 2]

Method 1: LINQ (answer was apparently deleted)

static string doIt(string a)
    {
        var l = a.Select((p, i) => new { ch = p, index = i }).
            Where(p => (p.index < a.Length - 2) && (a[p.index + 1] == p.ch) && (a[p.index + 2] == p.ch))
            .Select(p => p.index).ToList();
        l.Sort();
        l.Reverse();

        l.ForEach(i => a = a.Remove(i, 1));

        return a;
    }

METHOD 2:

Regex.Replace(tweet,@"(\S)\1{2,}","$1$1");

Method 3:

static string StringB(string s)
    {
        string input = s;
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < input.Length; i++)
        {
            if (i < 2 || input[i] != input[i - 1] || input[i] != input[i - 2])
                sb.Append(input[i]);
        }
        string output = sb.ToString();
        return output;

    }

Method 4:

static string sb2(string s)
    {
        string input = s;

        var sb = new StringBuilder(input);

        char p2 = '\0';
        char p1 = '\0';

        int pos = 0, len = sb.Length;
        while (pos < len)
        {
            if (p2 == p1) for (; pos < len && (sb[pos] == p2); len--)
                    sb.Remove(pos, 1);

            if (pos < len)
            {
                p2 = p1;
                p1 = sb[pos];
                pos++;
            }
        }
        return sb.ToString();

    }
  • 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-05-25T20:30:15+00:00Added an answer on May 25, 2026 at 8:30 pm

    Regexen look to be the simplest. Simple proof of concept in the REPL:

    using System.Text.RegularExpressions;  
    var regex = new Regex(@"(\S)\1{2,}"); // or @"([aeiouy])\1{2,}" etc?
    regex.Replace("mmmmm gooood griieeeeefff", "$1$1");
    

    –>

    "mm good griieeff"
    

    For raw performance, use something more like this: see it live on https://ideone.com/uWG68

    using System;
    using System.Text;
    
    class Program
    {
        public static void Main(string[] args)
        {
            string input = "mmmm gooood griiiiiiiiiieeeeeeefffff";
    
            var sb = new StringBuilder(input);
    
            char p2 = '\0';
            char p1 = '\0';
    
            int pos = 0, len=sb.Length;
            while (pos < len)
            {
                if (p2==p1) for (; pos<len && (sb[pos]==p2); len--)
                    sb.Remove(pos, 1);
    
                if (pos<len)
                {
                    p2=p1;
                    p1=sb[pos];
                    pos++;
                }
            }
    
            Console.WriteLine(sb);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some data like this: 1 2 3 4 5 9 2 6
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I've got a string that has curly quotes in it. I'd like to replace
I am trying to loop through a bunch of documents I have to put
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.