Menu

Capitalization and underscore problems

Help
Kbdank71
2008-04-09
2013-05-13
  • Kbdank71

    Kbdank71 - 2008-04-09

    My bot does one of two things: either removes a category from an article, or moves an article from one category to another.

    The code is different in each situation:
    I use RemoveFromCategory() for the removes, and
    pl3[i].text = pl3[i].text.Replace("[[Category:" + pl[0].title,"[[Category:" + pl[1].title);
    for the moves (to keep the sort keys intact).

    This all works fine unless the category has been added to the article using an initial lowercase or underscores.

    For example, if
    [[Category:foo bar]]
    or
    [[Category:Foo_bar]]
    are added to an article, it will show up in [[Category:Foo bar]], but my code won't remove or move either, because of the initial lowercase or the underscore.  When this happens, I have to make the edits manually.

    Is there any way to fix this?  I'm stumped.

     
    • CodeDriller

      CodeDriller - 2008-04-09

      Only manually for now, using regular expressions, e.g.:

          string categoryName = "Foo bar";
          string regexCategoryName = Regex.Escape(categoryName);
          regexCategoryName = regexCategoryName.Replace(" ", "[_ ]");
          regexCategoryName = "[" + regexCategoryName[0].ToLower() +
              regexCategoryName[0].ToUpper() + "]";

          pl[i].text = Regex.Replace(pl[i].text, @"\[\[(" + site.namespaces["14"] + "|" +
              Site.wikiNSpaces["14"] + "):" + regexCategoryName +
              @"(\|.*?)?]]\r?\n?", "");    // Removing category from page text

          pl[i].text = Regex.Replace(pl[i].text, @"\[\[(" + site.namespaces["14"] + "|" +
              Site.wikiNSpaces["14"] + "):" + regexCategoryName,
              "[[Category:New foo bar");    // Replacing category in page text

       
      • CodeDriller

        CodeDriller - 2008-04-09

        Two last statements do the trick. Firstly, you prepare the regular expression pattern (regexCategoryName) and then you modify the page text with it (text = Regex.Replace(text, pattern, replacement)). Try it.

         
        • Kbdank71

          Kbdank71 - 2008-04-09

          Ok, I get the premise.  Unfortunately, I'm getting error CS1501: No overload for method 'ToLower' takes '0' arguments (also with ToUpper).  I'll see if I can figure it out.

           
          • CodeDriller

            CodeDriller - 2008-04-10

            Yes, sorry, I forgot that it's a static function. That line must be:

                regexCategoryName = "[" + char.ToLower(regexCategoryName[0]) +
                    char.ToUpper(regexCategoryName[0]) + "]";

             
            • CodeDriller

              CodeDriller - 2008-04-10

              No, sorry again, it must be:

              string categoryName = "Foo bar";
              string regexCategoryName = Regex.Escape(categoryName);
              regexCategoryName = regexCategoryName.Replace("_", "\\ ").Replace("\\ ", "[_\\ ]");
              regexCategoryName = "[" + regexCategoryName[0].ToLower() +
                  regexCategoryName[0].ToUpper() + "]" + regexCategoryName.Substring(1);

              Don't bother. It seems to be a hard thing, I'd better include it in next release by myself.

               
              • Kbdank71

                Kbdank71 - 2008-04-10

                You're the greatest, thanks for all the help.

                Sorry for this one, I didn't think it was going to be such a hassle.

                 
    • Kbdank71

      Kbdank71 - 2008-04-09

      Will that work?  The way I read that (and apologies if I'm wrong), the code will take what I give it (string categoryName = "Foo bar";) and change it (regexCategoryName = ...), not make the change in what is in the article.

      For example, with the code above I can say "remove [[Category:foo bar]]" or "remove [[Category:Foo_bar]]" and it will remove [[Category:Foo bar]] from the article.

      Whereas I need,
      I will say "remove [[Category:Foo bar]]" and it will remove [[Category:Foo bar]] or [[Category:foo bar]] or [[Category:Foo_bar]] from the article.

      I hope I'm explaining this right. 

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.