<commit_before>#!/usr/local/bin/python

import sys
import codecs
import chardet


def read_then_write(input_file, guess_encoding, output_encoding):
    ipos = input_file.rfind('.')
    output_file = input_file[0:ipos] + '(%s)' % output_encoding + input_file[ipos:]
    writer = open(output_file, 'wb')
    reader = open(input_file, 'rw')
    for line in reader:
        line = codecs.decode(line, guess_encoding)
        encoded_line = codecs.encode(line, output_encoding)
        writer.write(encoded_line)
    reader.close()
    writer.close()


def convert(input_file, output_encoding):
    origin_fobj = open(input_file, 'rb')
    result = chardet.detect(origin_fobj.read())
    confidence = float(result['confidence'])
    if confidence > 0.5:
        guess_encoding = result['encoding']
        read_then_write(input_file, guess_encoding, output_encoding)
        print 'Convert from %s to %s Complete! (with a confidence of %d %%)' \
              % (guess_encoding, output_encoding, int(confidence*100))
    else:
        print 'Convert failed!'

def print_usage():
    print 'python cuecow.py input_file [output_encoding(default: utf-8)]'

if __name__ == '__main__':
    arg_num = len(sys.argv)
    if arg_num > 1:
        input_file = sys.argv[1]
        output_encoding = 'utf-8'
        if arg_num > 2:
            output_encoding = sys.argv[2]
        convert(input_file, output_encoding)
    else:
        print_usage()
<commit_msg>Lower the confidence threshold from 0.5 to 0.4.<commit_after>#!/usr/local/bin/python

import sys
import codecs
import chardet


def read_then_write(input_file, guess_encoding, output_encoding):
    ipos = input_file.rfind('.')
    output_file = input_file[0:ipos] + '(%s)' % output_encoding + input_file[ipos:]
    writer = open(output_file, 'wb')
    reader = open(input_file, 'rw')
    for line in reader:
        line = codecs.decode(line, guess_encoding)
        encoded_line = codecs.encode(line, output_encoding)
        writer.write(encoded_line)
    reader.close()
    writer.close()


def convert(input_file, output_encoding):
    origin_fobj = open(input_file, 'rb')
    result = chardet.detect(origin_fobj.read())
    confidence = float(result['confidence'])
    guess_encoding = None
    if confidence > 0.4:
        guess_encoding = result['encoding']
        read_then_write(input_file, guess_encoding, output_encoding)
        print 'Convert from %s to %s Completed! (with a confidence of %d%%)' \
              % (guess_encoding, output_encoding, int(confidence*100))
    else:
        print 'Convert failed! [Debug info] confidence->%s, guess_encoding->%s' % (confidence, guess_encoding)

def print_usage():
    print 'python cuecow.py input_file [output_encoding(default: utf-8)]'

if __name__ == '__main__':
    arg_num = len(sys.argv)
    if arg_num > 1:
        input_file = sys.argv[1]
        output_encoding = 'utf-8'
        if arg_num > 2:
            output_encoding = sys.argv[2]
        convert(input_file, output_encoding)
    else:
        print_usage()

{'fraction_non_alphanumeric': 0.11778290993071594, 'fraction_numerical': 0.009237875288683603, 'mean_word_length': 3.2824858757062145, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '6755732', 'n_tokens_mistral': 996, 'n_tokens_neox': 976, 'n_words': 221}