<commit_before>#!/usr/bin/python
# -*- coding: utf-8 -*-
"""Create country sub-division redirect pages.

Check if they are in the form Something, State, and if so, create a redirect
from Something, ST.

Specific arguments:
-start:xxx Specify the place in the alphabet to start searching
-force: Don't ask whether to create pages, just create them.

PRE-REQUISITE : Need to install python-pycountry library.
* Follow the instructions at: https://www.versioneye.com/python/pycountry/0.16
* Install with pip: pip install pycountry
"""
#
# (C) <NAME>, 2004
# (C) Pywikibot team, 2004-2018
#
# Distributed under the terms of the MIT license.
#
from __future__ import absolute_import, division, unicode_literals

import re

import pywikibot

from pywikibot.bot import suggest_help
from pywikibot import i18n

try:
    import pycountry
except ImportError as e:
    pycountry = e


class StatesRedirectBot(pywikibot.Bot):

    """Bot class used for implementation of re-direction norms."""

    def __init__(self, start, force):
        """Initializer.

        Parameters:
            @param start:xxx Specify the place in the alphabet to start
            searching.
            @param force: Don't ask whether to create pages, just create
            them.
        """
        site = pywikibot.Site()
        generator = site.allpages(start=start)
        super(StatesRedirectBot, self).__init__(generator=generator)

        self.force = force

        # Created abbrev from pycountry data base
        self.abbrev = {}
        for subd in pycountry.subdivisions:
            # Used subd.code[3:] to extract the exact code for
            # subdivisional states(ignoring the country code).
            self.abbrev[subd.name] = subd.code[3:]

    def treat(self, page):
        """Re-directing process.

        Check if pages are in the given form Something, State, and
        if so, create a redirect from Something, ST..
        """
        for sn in self.abbrev:
            if re.search(r', %s$' % sn, page.title()):
                pl = pywikibot.Page(self.site, page.title().replace(sn,
                                    self.abbrev[sn]))
                # A bit hacking here - the real work is done in the
                # 'except pywikibot.NoPage' part rather than the 'try'.

                try:
                    pl.get(get_redirect=True)
                    goal = pl.getRedirectTarget().title()
                    if pywikibot.Page(self.site, goal).exists():<commit_msg>[IMPR] Use SingleSiteBot with states_redirect.py<commit_after>#!/usr/bin/python
# -*- coding: utf-8 -*-
"""Create country sub-division redirect pages.

Check if they are in the form Something, State, and if so, create a redirect
from Something, ST.

Specific arguments:
-start:xxx Specify the place in the alphabet to start searching
-force: Don't ask whether to create pages, just create them.

PRE-REQUISITE : Need to install python-pycountry library.
* Follow the instructions at: https://www.versioneye.com/python/pycountry/0.16
* Install with pip: pip install pycountry
"""
#
# (C) <NAME>, 2004
# (C) Pywikibot team, 2004-2018
#
# Distributed under the terms of the MIT license.
#
from __future__ import absolute_import, division, unicode_literals

import re

import pywikibot

from pywikibot.bot import SingleSiteBot, suggest_help
from pywikibot import i18n

try:
    import pycountry
except ImportError as e:
    pycountry = e


class StatesRedirectBot(SingleSiteBot):

    """Bot class used for implementation of re-direction norms."""

    def __init__(self, start, force):
        """Initializer.

        @param start:xxx Specify the place in the alphabet to start searching.
        @type start: str
        @param force: Don't ask whether to create pages, just create them.
        @type force: bool
        """
        super(StatesRedirectBot, self).__init__()
        self.start = start
        self.force = force

    def setup(self):
        """Create abbrev from pycountry data base."""
        self.abbrev = {}
        for subd in pycountry.subdivisions:
            # Used subd.code[3:] to extract the exact code for
            # subdivisional states(ignoring the country code).
            self.abbrev[subd.name] = subd.code[3:]

    @property
    def generator(self):
        """Generator used by run() method."""
        return self.site.allpages(start=self.start)

    def treat(self, page):
        """Re-directing process.

        Check if pages are in the given form Something, State, and
        if so, create a redirect from Something, ST..
        """
        for sn in self.abbrev:
            if re.search(r', %s$' % sn, page.title()):
                pl = pywikibot.Page(self.site, page.title().replace(sn,
                                    self.abbrev[sn]))
                # A bit hacking here - the real work is done in the
                # 'except pywikibot.NoPage' part rather than the 'try'.

                try:
                    pl.get(get_redirect=True)
                    goal = pl.getRedirectTarget().title()
                    if pywikibot.Page(self.site, goal).exists():
{'fraction_non_alphanumeric': 0.09624413145539906, 'fraction_numerical': 0.00782472613458529, 'mean_word_length': 2.3660302830809745, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4862146', 'n_tokens_mistral': 1506, 'n_tokens_neox': 1395, 'n_words': 501}