User:MilHistBot/AutoCheck.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using NDesk.Options;
public class AutoCheck
{
private Bot bot;
private int unchanged;
private int upgrade;
private int newgrade;
private int downgrade;
private int max = 1;
private bool debugs = false;
private bool force = false;
private bool help = false;
private bool verbose = false;
private Dictionary<string, int> classes;
private bool compareClass (string oldClass, string botClass)
{
if (null == classes)
{
classes = new Dictionary<string, int>()
{
{ "Stub", 0 },
{ "Start", 1 },
{ "List", 1 },
{ "C", 2 },
{ "CL", 2 },
{ "B", 3 },
{ "BL", 3 },
};
}
if (oldClass.Equals (""))
{
newgrade++;
return true;
}
if (! classes.ContainsKey (oldClass))
{
throw new ApplicationException ("unknown class: '" + oldClass + "'");
}
int diff = classes[botClass] - classes[oldClass];
if (0 > diff)
{
downgrade++;
}
else if (0 == diff)
{
unchanged++;
return false;
}
else if (0 < diff)
{
upgrade++;
}
return true;
}
private void report ()
{
bot.Cred.Showtime (String.Format("{0} articles newly rated, {1} downgraded, {2} upgraded, {3} unchanged - total {4}",
newgrade, downgrade, upgrade, unchanged, newgrade + downgrade + upgrade + unchanged));
}
private void autoCheck (Page article, Page talk)
{
try
{
bot.Cred.Showtime (article.Title);
article.Load ();
talk.Load ();
var template = talk.MilHist.ProjectTemplate;
var oldClass = template.Class;
var rating = template.Rating;
var botClass = rating.Class;
template.RemoveAll (@"^class$|^importance$|^b\d$|^B-Class-\d$");
template.Rating = rating;
Debug.WriteLine ("\told rating = " + oldClass);
Debug.WriteLine ("\tprediction = " + article.Prediction ());
Debug.WriteLine ("\tbot rating = " + botClass);
var changed = compareClass (oldClass, botClass);
if (changed && force)
{
talk.Save ("Automatic MILHIST checklist assessment - " + botClass + " class");
bot.Cred.Showtime ("\tChanged from " + oldClass + " to " + botClass);
}
}
catch (ApplicationException ex)
{
string message = "Error in " + article.Title + ": " + ex.Message;
bot.Cred.Showtime (message);
}
catch (Exception ex)
{
string message = "Error in " + article.Title + ":\n" + ex.Message + "\n" + ex.StackTrace;
if (debugs)
{
Debug.WriteLine (message);
}
else
{
bot.Cred.Warning (message);
bot.Close ();
}
Environment.Exit (1);
}
}
private void autoCheck (List<Page> talkPages)
{
foreach (var talkPage in talkPages)
{
// Could be a subcategory
if (talkPage.Namespace.Equals ("Talk"))
{
autoCheck (talkPage.Article, talkPage);
}
}
report ();
}
private static void showHelp (OptionSet options)
{
Console.WriteLine ("Usage: mono AutoCheck [OPTIONS]+ <article>");
Console.WriteLine ("Assess article and update the MilHist template on the talk page.");
Console.WriteLine ();
Console.WriteLine ("Options:");
options.WriteOptionDescriptions (Console.Out);
Environment.Exit (0);
}
List<string> options (string [] args)
{
var optionSet = new OptionSet () {
{ "d|debug", "debugging", v => debugs = v != null },
{ "f|force", "update page", v => force = v != null },
{ "h|?|help", "display help", v => help = v != null },
{ "n|max=", "number of pages to process", v => int.TryParse (v, out max) },
{ "v|verbose", "vebosity", v => verbose = v != null },
};
List<string> extras = optionSet.Parse (args);
if (help)
{
showHelp (optionSet);
}
return extras;
}
private AutoCheck (string [] args)
{
bot = new Bot ();
var articles = options (args);
Debug.On = debugs;
Debug.Bot = bot;
List<Page> talkPages;
bot.Cred.Showtime ("started");
if (articles.Count > 0)
{
var articlePage = new Page (bot, articles[0]);
autoCheck (articlePage, articlePage.Talk);
}
else
{
var query = new Query ("Military history articles with missing B-Class checklists", max);
talkPages = bot.Category (query);
if (talkPages.Count < max)
{
query = new Query ("Military history articles with incomplete B-Class checklists", max - talkPages.Count);
talkPages.AddRange (bot.Category (query));
}
if (talkPages.Count < max)
{
query = new Query ("Unassessed military history articles", max - talkPages.Count);
talkPages.AddRange (bot.Category (query));
}
autoCheck (talkPages);
}
bot.Cred.Showtime ("done");
}
static public void Main (string [] args)
{
var autoCheck = new AutoCheck (args);
}
}
Content Disclaimer
Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.
- The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
- There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
- It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
- Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
- Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.