<commit_before>import MySQLdb
import logging

class MySQLBuildView:
    """The MySQLBuildView class allows database access optimized to
       build the disambiguation database
    """

    def __init__(self, db_connection):
        """constructor

           @param db_connector the database connector used to access the database
        """
        self._db_connection = db_connection
        self._cursor = db_connection.cursor()
        self.reset_cache()

    def __del__(self):
        """destructor
           closes the database connection
        """
        self._db_connection.close()

    def insert_article(self, id, title):
        """saves an article in the database

           @param id the id of the article
           @param title the title of the article 
        """
        try:
            self._cursor.execute('INSERT INTO articles(id, title) VALUES(%s, %s);', 
                id, title)
        except MySQLdb.Error, e:
            logging.error('error saving article "%s" to database: %s (%d)' 
                % (title.encode('ascii', 'ignore'), e.args[1], e.args[0]))

    def insert_redirect(self, source_name, target_name):
        """saves a redirect in the database

        @param source_name the name of the source article
        @param target_name the name of the target article
        """
        try:
            self._cursor.execute('INSERT INTO redirects(source_article_name, target_article_name) VALUES(%s, %s);',
                source_name, target_name)
        except MySQLdb.Error, e:
            logging.error('error saving redirect "%s" --> "%s" to database: %s (%d)' 
                % (source_name.encode('ascii', 'ignore'), target_name.encode('ascii', 'ignore'), e.args[1], e.args[0]))

    def insert_link(self, source_article_id, target_article_name):
        """saves a link to the database and updates the article record it points to

           @param source_article_id the id of the article which links to the target
           @param target_article_name the name of the target article
        """
        target_article_id = self._resolve_title(target_article_name)
        if target_article_id == None:
            logging.error('Could not resolve target article "%s" for link from source article %d' 
                % (target_article_name.encode('ascii', 'ignore'), source_article_id))
        else:
            try:
                self._cursor.execute('INSERT INTO links(source_article_id, target_article_id) VALUES(%s, %s);',
                    source_article_id, target_article_id)
            except MySQLdb.Error, e:
                logging.error('error saving link (%d) --> (%d) to database: %s (%d)' 
                % (source_article_id, target_article_id, e.args[1], e.args[0]))

    def insert_disambiguation(self, string, target_article_name):
        """saves a disambiguation to the database

           @param string the disambiguation string used for the linked entity
           @param target_article_name the name of the article the disambiguation stands for
        """
        target_article_id = self._resolve_title(target_article_name)
        if target_article_id == None:
            logging.error('Could not resolve target article "%s" for link from source article %d' 
                % (target_article_name.encode('ascii', 'ignore'), source_article_id))
        else:
            try:
                self._cursor.execute('INSERT INTO disambiguations(string, target_article_id, occurrences) VALUES(%s, %s, 1) ON DUPLICATE KEY UPDATE occurrences=occurrences+1;',
                    string, target_article_id)
            except MySQLdb.Error, e:
                logging.error('error saving disambiguation "%s" --> %s (%d): %s (%d)'
                    % (string.encode('ascii', 'ignore'), target_article_name.encode('ascii', 'ignore'), target_article_id, e.args[1], e.args[0]))

    def insert_ngrams(self, ngrams):
        """inserts ngrams into the database

           @param ngrams a list of ngrams where each ngram is a tuple containing the string,
                         and a zero or one indicating whether it was used as a link
        """
        try:<commit_msg>fixed inserts in mysqlbuildview<commit_after>import MySQLdb
import logging

class MySQLBuildView:
    """The MySQLBuildView class allows database access optimized to
       build the disambiguation database
    """

    def __init__(self, db_connection):
        """constructor

           @param db_connector the database connector used to access the database
        """
        self._db_connection = db_connection
        self._cursor = db_connection.cursor()
        self.reset_cache()

    def __del__(self):
        """destructor
           closes the database connection
        """
        self._db_connection.close()

    def insert_article(self, id, title):
        """saves an article in the database

           @param id the id of the article
           @param title the title of the article 
        """
        try:
            self._cursor.execute('INSERT INTO articles(id, title) VALUES(%s, %s);', 
                (id, title))
        except MySQLdb.Error, e:
            logging.error('error saving article "%s" to database: %s (%d)' 
                % (title.encode('ascii', 'ignore'), e.args[1], e.args[0]))

    def insert_redirect(self, source_name, target_name):
        """saves a redirect in the database

        @param source_name the name of the source article
        @param target_name the name of the target article
        """
        try:
            self._cursor.execute('INSERT INTO redirects(source_article_name, target_article_name) VALUES(%s, %s);',
                (source_name, target_name))
        except MySQLdb.Error, e:
            logging.error('error saving redirect "%s" --> "%s" to database: %s (%d)' 
                % (source_name.encode('ascii', 'ignore'), target_name.encode('ascii', 'ignore'), e.args[1], e.args[0]))

    def insert_link(self, source_article_id, target_article_name):
        """saves a link to the database and updates the article record it points to

           @param source_article_id the id of the article which links to the target
           @param target_article_name the name of the target article
        """
        target_article_id = self._resolve_title(target_article_name)
        if target_article_id == None:
            logging.error('Could not resolve target article "%s" for link from source article %d' 
                % (target_article_name.encode('ascii', 'ignore'), source_article_id))
        else:
            try:
                self._cursor.execute('INSERT INTO links(source_article_id, target_article_id) VALUES(%s, %s);',
                    (source_article_id, target_article_id))
            except MySQLdb.Error, e:
                logging.error('error saving link (%d) --> (%d) to database: %s (%d)' 
                % (source_article_id, target_article_id, e.args[1], e.args[0]))

    def insert_disambiguation(self, string, target_article_name):
        """saves a disambiguation to the database

           @param string the disambiguation string used for the linked entity
           @param target_article_name the name of the article the disambiguation stands for
        """
        target_article_id = self._resolve_title(target_article_name)
        if target_article_id == None:
            logging.error('Could not resolve target article "%s" for link from source article %d' 
                % (target_article_name.encode('ascii', 'ignore'), source_article_id))
        else:
            try:
                self._cursor.execute('INSERT INTO disambiguations(string, target_article_id, occurrences) VALUES(%s, %s, 1) ON DUPLICATE KEY UPDATE occurrences=occurrences+1;',
                    (string, target_article_id))
            except MySQLdb.Error, e:
                logging.error('error saving disambiguation "%s" --> %s (%d): %s (%d)'
                    % (string.encode('ascii', 'ignore'), target_article_name.encode('ascii', 'ignore'), target_article_id, e.args[1], e.args[0]))

    def insert_ngrams(self, ngrams):
        """inserts ngrams into the database

           @param ngrams a list of ngrams where each ngram is a tuple containing the string,
                         and a zero or one indicating whether it was used as a link
        """
        try:
{'fraction_non_alphanumeric': 0.1000718218817333, 'fraction_numerical': 0.0023940627244433805, 'mean_word_length': 2.4354440789473686, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1760018', 'n_tokens_mistral': 2315, 'n_tokens_neox': 2151, 'n_words': 708}