<commit_before>from __future__ import with_statement

import config
import sprite
from Box2D import *
import pyglet
from pyglet.gl import *
import codecs
from collections import *
from itertools import *
from math import *
from operator import attrgetter
import cPickle as pickle
import random

# Monkey patch for subpixel option.
pyglet.sprite.Sprite = sprite.Sprite

# Translation hook.
def _(phrase):
    return phrase

class Dictionary(object):
    def __init__(self, words):
        self.letter_tree = {}
        self.letter_counts = defaultdict(int)
        for word in words:
            self.add_word(word)

    def add_word(self, word):
        tree = self.letter_tree
        for letter in word:
            self.letter_counts[letter] += 1
            tree = tree.setdefault(letter, {})
        tree[u''] = None

    def complete(self, prefix):
        tree = self.letter_tree
        for letter in prefix:
            if letter in tree:
                tree = tree[letter]
            else:
                return set()
        return set(tree)

    def random_letter(self):
        total_count = sum(self.letter_counts.values())
        random_count = random.randrange(total_count)
        for letter, count in self.letter_counts.items():
            if random_count < count:
                return letter
            random_count -= count
        return None

    @staticmethod
    def parse():
        def read_words():
            alphabet = set(config.alphabet)
            with codecs.open(config.dictionary_file, 'r',
                             config.dictionary_encoding) as file_obj:
                for line in file_obj:
                    word = line.strip().upper()
                    if not set(word) - alphabet:
                        yield word
        return Dictionary(read_words())

    @staticmethod
    def load_cache():
        with open(config.dictionary_cache) as cache_file:
            return pickle.load(cache_file)

    def save_cache(self):
        with open(config.dictionary_cache, 'w') as cache_file:
            pickle.dump(self, cache_file, pickle.HIGHEST_PROTOCOL)

class MyWindow(pyglet.window.Window):
    def __init__(self, dictionary, **kwargs):
        super(MyWindow, self).__init__(**kwargs)        
        if self.fullscreen:
            self.set_exclusive_mouse()

        self.dictionary = dictionary
        self.scale = self.height / config.view_height
        self.font = pyglet.font.load(name=config.font_name,
                                     size=(self.scale * config.font_scale),
                                     bold=config.font_bold)
        self._init_highscores()
        self._init_gl()
        self.my_screen = TitleScreen(self)

    def _init_highscores(self):
        try:
            with open(config.highscore_file) as file_obj:
                self.highscores = pickle.load(file_obj)
        except:
            self.highscores = []
            for score in (200, 400, 600, 800, 1000):
                self.add_highscore(score, 'Nucleus', save=False)

    def add_highscore(self, score, name='Player', save=True):
        self.highscores.append((score, name))
        self.highscores.sort(reverse=True)
        self.highscores = self.highscores[:5]
        if save:
            with open(config.highscore_file, 'w') as file_obj:
                pickle.dump(self.highscores, file_obj, pickle.HIGHEST_PROTOCOL)    

    def _init_gl(self):
        clear_color = [float(c) / 255. for c in config.background_color]
        clear_color.append(0.)
        glClearColor(*clear_color)

    def on_draw(self):
        self.my_screen.on_draw()

    def on_key_press(self, symbol, modifiers):
        self.my_screen.on_key_press(symbol, modifiers)

    def on_text(self, text):
        self.my_screen.on_text(text)

class TitleScreen(object):
    def __init__(self, window):
        self.window = window
        self.batch = pyglet.graphics.Batch()
        self._init_labels()

    def _init_labels(self):
        pyglet.text.Label('Nucleus',
                          font_size=(self.window.scale * 1.5),
                          bold=True,
                          x=(self.window.width // 2),
                          y=(self.window.height * 0.75),
                          anchor_x='center',
                          anchor_y='center',
                          batch=self.batch)

        for i, entry in enumerate(self.window.highscores):
            score, name = entry
            y = self.window.height * 0.5 - i * 1.5 * self.window.scale
            pyglet.text.Label(str(score),
                              font_size=self.window.scale,<commit_msg>Press ALT+ENTER to save a screenshot.<commit_after>from __future__ import with_statement

import config
import sprite
from Box2D import *
import pyglet
from pyglet.gl import *
import codecs
from collections import *
from itertools import *
from math import *
from operator import attrgetter
import cPickle as pickle
import random

# Monkey patch for subpixel option.
pyglet.sprite.Sprite = sprite.Sprite

# Translation hook.
def _(phrase):
    return phrase

class Dictionary(object):
    def __init__(self, words):
        self.letter_tree = {}
        self.letter_counts = defaultdict(int)
        for word in words:
            self.add_word(word)

    def add_word(self, word):
        tree = self.letter_tree
        for letter in word:
            self.letter_counts[letter] += 1
            tree = tree.setdefault(letter, {})
        tree[u''] = None

    def complete(self, prefix):
        tree = self.letter_tree
        for letter in prefix:
            if letter in tree:
                tree = tree[letter]
            else:
                return set()
        return set(tree)

    def random_letter(self):
        total_count = sum(self.letter_counts.values())
        random_count = random.randrange(total_count)
        for letter, count in self.letter_counts.items():
            if random_count < count:
                return letter
            random_count -= count
        return None

    @staticmethod
    def parse():
        def read_words():
            alphabet = set(config.alphabet)
            with codecs.open(config.dictionary_file, 'r',
                             config.dictionary_encoding) as file_obj:
                for line in file_obj:
                    word = line.strip().upper()
                    if not set(word) - alphabet:
                        yield word
        return Dictionary(read_words())

    @staticmethod
    def load_cache():
        with open(config.dictionary_cache) as cache_file:
            return pickle.load(cache_file)

    def save_cache(self):
        with open(config.dictionary_cache, 'w') as cache_file:
            pickle.dump(self, cache_file, pickle.HIGHEST_PROTOCOL)

class MyWindow(pyglet.window.Window):
    def __init__(self, dictionary, **kwargs):
        super(MyWindow, self).__init__(**kwargs)        
        if self.fullscreen:
            self.set_exclusive_mouse()

        self.dictionary = dictionary
        self.scale = self.height / config.view_height
        self.font = pyglet.font.load(name=config.font_name,
                                     size=(self.scale * config.font_scale),
                                     bold=config.font_bold)
        self._init_highscores()
        self._init_gl()
        self.my_screen = TitleScreen(self)

    def _init_highscores(self):
        try:
            with open(config.highscore_file) as file_obj:
                self.highscores = pickle.load(file_obj)
        except:
            self.highscores = []
            for score in (200, 400, 600, 800, 1000):
                self.add_highscore(score, 'Nucleus', save=False)

    def add_highscore(self, score, name='Player', save=True):
        self.highscores.append((score, name))
        self.highscores.sort(reverse=True)
        self.highscores = self.highscores[:5]
        if save:
            with open(config.highscore_file, 'w') as file_obj:
                pickle.dump(self.highscores, file_obj, pickle.HIGHEST_PROTOCOL)    

    def _init_gl(self):
        clear_color = [float(c) / 255. for c in config.background_color]
        clear_color.append(1.)
        glClearColor(*clear_color)

    def on_draw(self):
        self.my_screen.on_draw()

    def on_key_press(self, symbol, modifiers):
        if (symbol == pyglet.window.key.ENTER and
            modifiers & pyglet.window.key.MOD_ALT):
            color_buffer = pyglet.image.get_buffer_manager().get_color_buffer()
            color_buffer.save('nucleus-screenshot.png')
        else:
            self.my_screen.on_key_press(symbol, modifiers)

    def on_text(self, text):
        self.my_screen.on_text(text)

class TitleScreen(object):
    def __init__(self, window):
        self.window = window
        self.batch = pyglet.graphics.Batch()
        self._init_labels()

    def _init_labels(self):
        pyglet.text.Label('Nucleus',
                          font_size=(self.window.scale * 1.5),
                          bold=True,
                          x=(self.window.width // 2),
                          y=(self.window.height * 0.75),
                          anchor_x='center',
                          anchor_y='center',
                          batch=self.batch)

        for i, entry in enumerate(self.window.highscores):
            score, name = entry
            y = self.window.height * 0.5 - i * 1.5 * self.window.scale
            pyglet.text.Label(str(score),
                              font_size=self.window.scale,
{'fraction_non_alphanumeric': 0.0949989537560159, 'fraction_numerical': 0.006905210295040804, 'mean_word_length': 1.884429692214846, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '724724', 'n_tokens_mistral': 2843, 'n_tokens_neox': 2699, 'n_words': 602}