Jump to content

User:Lightmouse/AWB/scripts/units

From Wikipedia, the free encyclopedia

//add metric units

//

        WikiFunctions.Parse.HideText ht = new WikiFunctions.Parse.HideText(true, false, true);
        public string ProcessArticle(string ArticleText, string ArticleTitle, int wikiNamespace, out string Summary, out bool Skip)
        {
            Skip = false;
            Summary = "";
//Protect images, quotes etc by removing them
//ArticleText = ht.Hide(ArticleText);
ArticleText = ht.HideMore(ArticleText);
//aviation specifications: protect 'main=' and 'alt=' - undone below
ArticleText=Regex.Replace(ArticleText, @"(?i)((?:main|alt)\s?=\s?)(\d)", "$1xx$2");

//protect 'per unit' - undone below
ArticleText=Regex.Replace(ArticleText, @"(?i)(feet|foot|ft|miles?)(\Wper\W)", "$1xx$2");



//foot
ArticleText=Regex.Replace(ArticleText, @"(?i)([^\d\)\/\\]\s)(\d[\d,\.]*)(?:\s|-| )foot([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1{{convert|$2|ft|m|adj=on}}$3");
ArticleText=Regex.Replace(ArticleText, @"(?i)([^\d\)\/\\]\s)(\d[\d,\.]*)(?:\s|-| )ft([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1{{convert|$2|ft|m|abbr=on}}$3");
ArticleText=Regex.Replace(ArticleText, @"(?i)([^\d\)\/\\]\s)(\d[\d,\.]*)(?:\s|-| )feet([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1{{convert|$2|ft|m}}$3");


//sq ft
ArticleText=Regex.Replace(ArticleText, @"(?i)([^\d\)\/\\]\s)(\d[\d,\.]*)(?:\s|-| )(?:sq\.?|square)(?:\s|-| )foot([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1{{convert|$2|sqft|m2|adj=on}}$3");
ArticleText=Regex.Replace(ArticleText, @"(?i)([^\d\)\/\\]\s)(\d[\d,\.]*)(?:\s|-| )(?:sq\.?|square)(?:\s|-| |)ft([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1{{convert|$2|sqft|m2|abbr=on}}$3");
ArticleText=Regex.Replace(ArticleText, @"(?i)([^\d\)\/\\]\s)(\d[\d,\.]*)(?:\s|-| )(?:sq\.?|square)(?:\s|-| )feet([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1{{convert|$2|sqft|m2}}$3");

ArticleText=Regex.Replace(ArticleText, @"(?i)([^\d\)\/\\]\s)(\d[\d,\.]*)(?:\s|-| )\[\[square\sfoot\]\]([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1{{convert|$2|sqft|m2|adj=on}}$3");
ArticleText=Regex.Replace(ArticleText, @"(?i)([^\d\)\/\\]\s)(\d[\d,\.]*)(?:\s|-| )\[\[square\sfoot\|sq\sft\]\]([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1{{convert|$2|sqft|m2|abbr=on}}$3");
ArticleText=Regex.Replace(ArticleText, @"(?i)([^\d\)\/\\]\s)(\d[\d,\.]*)(?:\s|-| )\[\[square\sfeet\]\]([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1{{convert|$2|sqft|m2}}$3");

ArticleText=Regex.Replace(ArticleText, @"(?i)([^\d\)\/\\]\s)(\d[\d,]*)(?:\s|-| )thousand(?:\s|-| )(?:sq|square)\.?(?:\s|-| )ft([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1{{convert|$2,000|sqft|m2|abbr=on}}$3");
ArticleText=Regex.Replace(ArticleText, @"(?i)([^\d\)\/\\]\s)(\d[\d,]*)(?:\s|-| )thousand(?:\s|-| )(?:sq|square)\.?(?:\s|-| )feet([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1{{convert|$2,000|sqft|m2}}$3");
ArticleText=Regex.Replace(ArticleText, @"(?i)([^\d\)\/\\]\s)(\d[\d,]*)(?:\s|-| )thousand(?:\s|-| )(?:sq|square)\.?(?:\s|-| )foot([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1{{convert|$2,000|sqft|m2|adj=on}}$3");
ArticleText=Regex.Replace(ArticleText, @"(?i)([^\d\)\/\\]\s)(\d[\d,]*)(?:\s|-| )million(?:\s|-| )(?:sq|square)\.?(?:\s|-| |)ft([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1{{convert|$2,000000|sqft|m2|abbr=on}}$3");
ArticleText=Regex.Replace(ArticleText, @"(?i)([^\d\)\/\\]\s)(\d[\d,]*)(?:\s|-| )million(?:\s|-| )(?:sq|square)\.?(?:\s|-| )feet([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1{{convert|$2,000000|sqft|m2}}$3");
ArticleText=Regex.Replace(ArticleText, @"(?i)([^\d\)\/\\]\s)(\d[\d,]*)(?:\s|-| )million(?:\s|-| )(?:sq|square)\.?(?:\s|-| )foot([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1{{convert|$2,000000|sqft|m2|adj=on}}$3");
ArticleText=Regex.Replace(ArticleText, @"(?i)([^\d\)\/\\]\s)(\d[\d,]*)\.(\d)(?:\s|-| )million(?:\s|-| )(?:sq|square)\.?(?:\s|-| |)ft([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1{{convert|$2$3,00000|sqft|m2|abbr=on}}$4");
ArticleText=Regex.Replace(ArticleText, @"(?i)([^\d\)\/\\]\s)(\d[\d,]*)\.(\d)(?:\s|-| )million(?:\s|-| )(?:sq|square)\.?(?:\s|-| )feet([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1{{convert|$2$3,00000|sqft|m2}}$4");
ArticleText=Regex.Replace(ArticleText, @"(?i)([^\d\)\/\\]\s)(\d[\d,]*)\.(\d)(?:\s|-| )million(?:\s|-| )(?:sq|square)\.?(?:\s|-| )foot([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1{{convert|$2$3,00000|sqft|m2|adj=on}}$4");
ArticleText=Regex.Replace(ArticleText, @"(?i)([^\d\)\/\\]\s)(\d[\d,]*)\.(\d\d)(?:\s|-| )million(?:\s|-| )(?:sq|square)\.?(?:\s|-| |)ft([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1{{convert|$2$3,0000|sqft|m2|abbr=on}}$4");
ArticleText=Regex.Replace(ArticleText, @"(?i)([^\d\)\/\\]\s)(\d[\d,]*)\.(\d\d)(?:\s|-| )million(?:\s|-| )(?:sq|square)\.?(?:\s|-| )feet([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1{{convert|$2$3,0000|sqft|m2}}$4");
ArticleText=Regex.Replace(ArticleText, @"(?i)([^\d\)\/\\]\s)(\d[\d,]*)\.(\d\d)(?:\s|-| )million(?:\s|-| )(?:sq|square)\.?(?:\s|-| )foot([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1{{convert|$2$3,0000|sqft|m2|adj=on}}$4");

//cubic foot
ArticleText=Regex.Replace(ArticleText, @"(?i)([^\d\)\/\\]\s)(\d[\d,\.]*)(?:\s|-| )(?:cu\.?|cubic)(?:\s|-| )foot([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1{{convert|$2|cuft|m3|adj=on}}$3");
ArticleText=Regex.Replace(ArticleText, @"(?i)([^\d\)\/\\]\s)(\d[\d,\.]*)(?:\s|-| )(?:cu\.?|cubic)(?:\s|-| |)ft([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1{{convert|$2|cuft|m3|abbr=on}}$3");
ArticleText=Regex.Replace(ArticleText, @"(?i)([^\d\)\/\\]\s)(\d[\d,\.]*)(?:\s|-| )(?:cu\.?|cubic)(?:\s|-| )feet([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1{{convert|$2|cuft|m3}}$3");



//mile - lower case to avoid proper names like 'Five Mile Road'
ArticleText=Regex.Replace(ArticleText, @"([^\d\)\/\\]\s)(\d[\d,\.]*)(?:\s|-| )mile([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1{{convert|$2|mi|km|adj=on}}$3");
ArticleText=Regex.Replace(ArticleText, @"([^\d\)\/\\]\s)(\d[\d,\.]*)(?:\s|-| |)mi([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1{{convert|$2|mi|km|abbr=on}}$3");
ArticleText=Regex.Replace(ArticleText, @"([^\d\)\/\\]\s)(\d[\d,\.]*)(?:\s|-| )miles([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1{{convert|$2|mi|km}}$3");

ArticleText=Regex.Replace(ArticleText, @"([^y]\sone(?:\s|-| )miles?)([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1 (1.6 km)$2");
ArticleText=Regex.Replace(ArticleText, @"([^y]\stwo(?:\s|-| )miles?)([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1 (3 km)$2");
ArticleText=Regex.Replace(ArticleText, @"([^y]\sthree(?:\s|-| )miles?)([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1 (5 km)$2");
ArticleText=Regex.Replace(ArticleText, @"([^y]\sfour(?:\s|-| )miles?)([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1 (6 km)$2");
ArticleText=Regex.Replace(ArticleText, @"([^y]\sfive(?:\s|-| )miles?)([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1 (8 km)$2");
ArticleText=Regex.Replace(ArticleText, @"([^y]\ssix(?:\s|-| )miles?)([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1 (10 km)$2");
ArticleText=Regex.Replace(ArticleText, @"([^y]\sseven(?:\s|-| )miles?)([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1 (11 km)$2");
ArticleText=Regex.Replace(ArticleText, @"([^y]\seight(?:\s|-| )miles?)([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1 (13 km)$2");
ArticleText=Regex.Replace(ArticleText, @"([^y]\snine(?:\s|-| )miles?)([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1 (14 km)$2");
ArticleText=Regex.Replace(ArticleText, @"([^y]\sten(?:\s|-| )miles?)([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1 (16 km)$2");
ArticleText=Regex.Replace(ArticleText, @"([^y]\seleven(?:\s|-| )miles?)([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1 (18 km)$2");
ArticleText=Regex.Replace(ArticleText, @"([^y]\stwelve(?:\s|-| )miles?)([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1 (19 km)$2");
ArticleText=Regex.Replace(ArticleText, @"([^y]\sthirteen(?:\s|-| )miles?)([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1 (19 km)$2");
ArticleText=Regex.Replace(ArticleText, @"([^y]\sfourteen(?:\s|-| )miles?)([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1 (21 km)$2");
ArticleText=Regex.Replace(ArticleText, @"([^y]\sfifteen(?:\s|-| )miles?)([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1 (24 km)$2");
ArticleText=Regex.Replace(ArticleText, @"([^y]\sixteen(?:\s|-| )miles?)([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1 (26 km)$2");
ArticleText=Regex.Replace(ArticleText, @"([^y]\sseventeen(?:\s|-| )miles?)([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1 (27 km)$2");
ArticleText=Regex.Replace(ArticleText, @"([^y]\seighteen(?:\s|-| )miles?)([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1 (29 km)$2");
ArticleText=Regex.Replace(ArticleText, @"([^y]\snineteen(?:\s|-| )miles?)([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1 (30 km)$2");
ArticleText=Regex.Replace(ArticleText, @"([^y]\stwenty(?:\s|-| )miles?)([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1 (32 km)$2");


//sq mi
ArticleText=Regex.Replace(ArticleText, @"(?i)([^\d\)\/\\]\s)(\d[\d,\.]*)(?:\s|-| )(?:sq\.?|square)(?:\s|-| )mile([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1{{convert|$2|sqmi|km2|adj=on}}$3");
ArticleText=Regex.Replace(ArticleText, @"(?i)([^\d\)\/\\]\s)(\d[\d,\.]*)(?:\s|-| )(?:sq\.?|square)(?:\s|-| |)mi([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1{{convert|$2|sqmi|km2|abbr=on}}$3");
ArticleText=Regex.Replace(ArticleText, @"(?i)([^\d\)\/\\]\s)(\d[\d,\.]*)(?:\s|-| )(?:sq\.?|square)(?:\s|-| )miles([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1{{convert|$2|sqmi|km2}}$3");

ArticleText=Regex.Replace(ArticleText, @"(?i)([^\d\)\/\\]\s)(\d[\d,]*)(?:\s|-| )thousand(?:\s|-| )(?:sq|square)\.?(?:\s|-| )mi([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1{{convert|$2,000|sqmi|km2|abbr=on}}$3");
ArticleText=Regex.Replace(ArticleText, @"(?i)([^\d\)\/\\]\s)(\d[\d,]*)(?:\s|-| )thousand(?:\s|-| )(?:sq|square)\.?(?:\s|-| )miles([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1{{convert|$2,000|sqmi|km2}}$3");
ArticleText=Regex.Replace(ArticleText, @"(?i)([^\d\)\/\\]\s)(\d[\d,]*)(?:\s|-| )thousand(?:\s|-| )(?:sq|square)\.?(?:\s|-| )mile([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1{{convert|$2,000|sqmi|km2|adj=on}}$3");
ArticleText=Regex.Replace(ArticleText, @"(?i)([^\d\)\/\\]\s)(\d[\d,]*)(?:\s|-| )million(?:\s|-| )(?:sq|square)\.?(?:\s|-| |)mi([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1{{convert|$2,000000|sqmi|km2|abbr=on}}$3");
ArticleText=Regex.Replace(ArticleText, @"(?i)([^\d\)\/\\]\s)(\d[\d,]*)(?:\s|-| )million(?:\s|-| )(?:sq|square)\.?(?:\s|-| )miles([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1{{convert|$2,000000|sqmi|km2}}$3");
ArticleText=Regex.Replace(ArticleText, @"(?i)([^\d\)\/\\]\s)(\d[\d,]*)(?:\s|-| )million(?:\s|-| )(?:sq|square)\.?(?:\s|-| )mile([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1{{convert|$2,000000|sqmi|km2|adj=on}}$3");
ArticleText=Regex.Replace(ArticleText, @"(?i)([^\d\)\/\\]\s)(\d[\d,]*)\.(\d)(?:\s|-| )million(?:\s|-| )(?:sq|square)\.?(?:\s|-| |)mi([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1{{convert|$2$3,00000|sqmi|km2|abbr=on}}$4");
ArticleText=Regex.Replace(ArticleText, @"(?i)([^\d\)\/\\]\s)(\d[\d,]*)\.(\d)(?:\s|-| )million(?:\s|-| )(?:sq|square)\.?(?:\s|-| )miles([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1{{convert|$2$3,00000|sqmi|km2}}$4");
ArticleText=Regex.Replace(ArticleText, @"(?i)([^\d\)\/\\]\s)(\d[\d,]*)\.(\d)(?:\s|-| )million(?:\s|-| )(?:sq|square)\.?(?:\s|-| )mile([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1{{convert|$2$3,00000|sqmi|km2|adj=on}}$4");
ArticleText=Regex.Replace(ArticleText, @"(?i)([^\d\)\/\\]\s)(\d[\d,]*)\.(\d\d)(?:\s|-| )million(?:\s|-| )(?:sq|square)\.?(?:\s|-| |)mi([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1{{convert|$2$3,0000|sqmi|km2|abbr=on}}$4");
ArticleText=Regex.Replace(ArticleText, @"(?i)([^\d\)\/\\]\s)(\d[\d,]*)\.(\d\d)(?:\s|-| )million(?:\s|-| )(?:sq|square)\.?(?:\s|-| )miles([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1{{convert|$2$3,0000|sqmi|km2}}$4");
ArticleText=Regex.Replace(ArticleText, @"(?i)([^\d\)\/\\]\s)(\d[\d,]*)\.(\d\d)(?:\s|-| )million(?:\s|-| )(?:sq|square)\.?(?:\s|-| )mile([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1{{convert|$2$3,0000|sqmi|km2|adj=on}}$4");


//cu mi
ArticleText=Regex.Replace(ArticleText, @"(?i)([^\d\)\/\\]\s)(\d[\d,\.]*)(?:\s|-| )(?:cu\.?|cubic)(?:\s|-| )mile([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1{{convert|$2|cumi|km3|adj=on}}$3");
ArticleText=Regex.Replace(ArticleText, @"(?i)([^\d\)\/\\]\s)(\d[\d,\.]*)(?:\s|-| )(?:cu\.?|cubic)(?:\s|-| |)mi([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1{{convert|$2|cumi|km3|abbr=on}}$3");
ArticleText=Regex.Replace(ArticleText, @"(?i)([^\d\)\/\\]\s)(\d[\d,\.]*)(?:\s|-| )(?:cu\.?|cubic)(?:\s|-| )miles([^\d\w\(\)\/\\]?(?:\s|-)[^\d\(\/\\])", "$1{{convert|$2|cumi|km3}}$3");



//aviation specifications: unprotect 'main=' and 'alt='
ArticleText=Regex.Replace(ArticleText, @"(?i)((?:main|alt)\s?=\s?)xx(\d)", "$1$2");

//unprotect 'per unit'
ArticleText=Regex.Replace(ArticleText, @"(?i)(feet|foot|ft|miles?)xx(\Wper\W)", "$1 $2");


//fix superscript after convert template
ArticleText=Regex.Replace(ArticleText, @"({{convert\|\d+\|)ft\|m([^}]*}})<sup>2<\/sup>", "$1sqft|m2$2");
ArticleText=Regex.Replace(ArticleText, @"({{convert\|\d+\|)ft\|m([^}]*}})<sup>3<\/sup>", "$1cuft|m3$2");
ArticleText=Regex.Replace(ArticleText, @"({{convert\|\d+\|)m\|ft([^}]*}})<sup>2<\/sup>", "$1m2|sqft$2");
ArticleText=Regex.Replace(ArticleText, @"({{convert\|\d+\|)m\|ft([^}]*}})<sup>3<\/sup>", "$1m3|cuft$2");
ArticleText=Regex.Replace(ArticleText, @"({{convert\|\d+\|)mi\|km([^}]*}})<sup>2<\/sup>", "$1sqmi|km2$2");
ArticleText=Regex.Replace(ArticleText, @"({{convert\|\d+\|)km\|mi([^}]*}})<sup>2<\/sup>", "$1km2|sqmi$2");
//remove commas from numerical values in convert template
ArticleText=Regex.Replace(ArticleText, @"({convert\|[^}]*),", "$1");
ArticleText=Regex.Replace(ArticleText, @"({convert\|[^}]*),", "$1");
ArticleText=Regex.Replace(ArticleText, @"({convert\|[^}]*),", "$1");
ArticleText=Regex.Replace(ArticleText, @"({convert\|[^}]*),", "$1");
ArticleText=Regex.Replace(ArticleText, @"({convert\|[^}]*),", "$1");
//remove leading zeros from convert template
ArticleText=Regex.Replace(ArticleText, @"({convert\|)0(\d)", "$1$2");
ArticleText=Regex.Replace(ArticleText, @"({convert\|)0(\d)", "$1$2");
ArticleText=Regex.Replace(ArticleText, @"({convert\|)0(\d)", "$1$2");
//Unprotect images, quotes etc (i.e. put them back)
return ht.AddBackMore(ArticleText);
//return ht.AddBack(ArticleText);
return ArticleText;
}
//