コード例 #1
0
ファイル: optimal_move.py プロジェクト: bchanx/scrabble
def setup(words_in_play, letters_in_hand, word_list):
	# setup words_in_play and tiles_in_hand
	config.set('init', 'words_in_play', words_in_play)
	config.set('letters_in_hand', 'player0', letters_in_hand)
	with open(CONFIG_FILE, 'w') as config_file:
		config.write(config_file)
	# setup dictionary
	with open(WORD_DICT, 'w') as word_dict:
		for word in word_list:
			word_dict.write(word+'\n')
	scrabble = Scrabble(1, CONFIG_FILE, WORD_DICT)
	return scrabble.getOptimalMove()
コード例 #2
0
ファイル: game.py プロジェクト: gkotas/Scrabble
    def __init__(self):
        SceneBase.__init__(self)
        self.scrabble = Scrabble(True)
        self.board = Board('imgs/board.jpg', [0, 0])
        self.letter_ss = Spritesheet('imgs/letters.jpg')
        self.player_tiles = []
        self.game_tiles = []
        self.selected_tile = None
        self.offset_x = 0
        self.offset_y = 0

        for i, letter in enumerate(self.scrabble.get_rack()):
            self.player_tiles.append(
                Tile(letter, self.letter_ss, PLAYER_TILE_POSITIONS[i]))
コード例 #3
0
    def _get_names(self):
        """Initializes the game by asking for the player's names"""
        i = 0
        while True:
            if i == 0:
                names = PLAYER_NAMES
            else:
                names = []

            if len(names) == 0:
                while True:
                    num = len(names) + 1
                    name = simpledialog.askstring(
                        "Player {}".format(num),
                        "Please enter the name of player {} (or leave blank to finish)"
                        .format(num))

                    if name is None:
                        exit()
                    name = name.strip()

                    if not name:
                        break

                    if name in names:
                        messagebox.showerror(
                            "Bad Name",
                            f"{name} is already registered as a player")
                        continue
                    names.append(name)

            try:
                self._game = Scrabble(names)
                return
            except support.ScrabbleError as error:
                messagebox.showerror("Error Starting Game", str(error))

            i += 1
コード例 #4
0
ファイル: main.py プロジェクト: jlehuen/coviscrabble
def main():
    banner('S C R A B B L E')

    # Checking the Python version
    if sys.version_info < (3, 0):
        print('Python 3.x requis')
        sys.exit(1)

    # Reading the command line args
    nbargs = len(sys.argv)
    if nbargs == 3 and sys.argv[1] == '-serv':
        flag = True
        addr = 'localhost'
        port = int(sys.argv[2])
        start_server(port)  # Launch server
    elif nbargs == 3:
        flag = False
        addr = sys.argv[1]
        port = int(sys.argv[2])
    else:
        print('Usage for server mode: python scrabble.py -serv <port>')
        print('Usage for client mode: python scrabble.py <address> <port>')
        sys.exit(2)

    # Splash screen and identification dialog
    splash = Tk()
    splash.title('S C R A B B L E  %d.%d' % VERSION)
    splash.resizable(False, False)
    image = PhotoImage(file='data/splash.png')
    Label(splash, image=image).pack()
    center(splash)
    Identification(splash)
    splash.destroy()

    # Opening the main window
    dico = os.environ['DICO']
    root = Scrabble(VERSION, addr, port, splash.login, dico, flag)
    root.after(1000, root.pool)  # Start pooling for asynchronous messages
    root.mainloop()
コード例 #5
0
ファイル: my_first_app.py プロジェクト: itraa/dev-sprint5
from music import Music
from scrabble import Scrabble

app = flask.Flask(__name__)
app.secret_key = settings.secret_key
main_view_func = Main.as_view('main')
# Routes
app.add_url_rule('/', view_func=main_view_func, methods=["GET"])
app.add_url_rule('/<page>/', view_func=main_view_func, methods=["GET"])
app.add_url_rule('/login/',
                 view_func=Login.as_view('login'),
                 methods=["GET", "POST"])

app.add_url_rule('/remote/',
                 view_func=Remote.as_view('remote'),
                 methods=['GET', 'POST'])

app.add_url_rule('/music/', view_func=Music.as_view('music'), methods=['GET'])

app.add_url_rule('/scrabble/',
                 view_func=Scrabble.as_view('scrabble'),
                 methods=['GET', 'POST'])


@app.errorhandler(404)
def page_not_found(error):
    return flask.render_template('404.html'), 404


app.debug = True
app.run()
コード例 #6
0
class ScrabbleGame:
    """Top-level game class for Scrabble GUI"""

    LABELS = [
        PlayerActions.PLAY, PlayerActions.SWAP, PlayerActions.PASS,
        PlayerActions.RESET
    ]

    def __init__(self, master):
        self._master = master

        master.title("Scrabble!")

        self._top = tk.Frame(self._master)
        self._top.pack(expand=True, fill=tk.BOTH)

        self._left = tk.Frame(self._top)
        self._left.pack(side=tk.LEFT, expand=True, fill=tk.BOTH)

        self._right = tk.Frame(self._top)
        self._right.pack(side=tk.RIGHT, fill=tk.BOTH)

        self._get_names()

        self._view = GridView(self._left, self._game.get_board().get_size())
        self._view.pack()
        self.draw_board()

        self._players = tk.Label(self._right)
        self._players.pack()

        self.draw_players_view()

        self._player_view = PlayerView(self._left)
        self._player_view.pack()

        self._player_view.draw_player(self._game.get_active_player())

        self._active_tiles = set()
        self._player_view.rack.on('select', self._handle_rack_click)
        self._view.on('select', self._handle_board_click)

        self._controls = TurnControls(self._right,
                                      labels=((action, action.value)
                                              for action in PlayerActions))
        self._controls.pack(side=tk.BOTTOM)
        self._controls.on('action', self._handle_action)

        # menu
        menubar = tk.Menu(self._master)
        filemenu = tk.Menu(menubar, tearoff=False)
        filemenu.add_command(label="New Game", command=self.reset)
        menubar.add_cascade(label="File", menu=filemenu)

        self._master.config(menu=menubar)

    def _get_names(self):
        """Initializes the game by asking for the player's names"""
        i = 0
        while True:
            if i == 0:
                names = PLAYER_NAMES
            else:
                names = []

            if len(names) == 0:
                while True:
                    num = len(names) + 1
                    name = simpledialog.askstring(
                        "Player {}".format(num),
                        "Please enter the name of player {} (or leave blank to finish)"
                        .format(num))

                    if name is None:
                        exit()
                    name = name.strip()

                    if not name:
                        break

                    if name in names:
                        messagebox.showerror(
                            "Bad Name",
                            f"{name} is already registered as a player")
                        continue
                    names.append(name)

            try:
                self._game = Scrabble(names)
                return
            except support.ScrabbleError as error:
                messagebox.showerror("Error Starting Game", str(error))

            i += 1

    def reset(self):
        """Resets the game to a blank state; does not re-ask for player's names"""
        if not messagebox.askokcancel(
                "Restart?", "Are you sure you want to start a new game?"):
            return
        self._game.restart()
        self._view.undraw_all_tiles()
        self._player_view.draw_player(self._game.get_active_player())
        self._active_tiles = set()

    def toggle_tile(self, index):
        """Toggles a tile in the active player's rack

        Parameters:
            index (int): The index of the tile to toggle, zero being the left-most            
        """
        if index in self._active_tiles:
            self._active_tiles.remove(index)
            self._player_view.rack.deactivate_tile(index)
        else:
            self._active_tiles.add(index)
            self._player_view.rack.activate_tile(index)

    def _handle_action(self, action):
        """Handles the current player's action"""
        if self._game.is_game_over():
            return

        if action == PlayerActions.PLAY:
            try:
                scores = self._game.confirm_place()

                if len(scores) == 0:
                    return messagebox.showerror(
                        "Invalid Move",
                        f"You must form at least one {support.MIN_WORD_LENGTH}-letter word"
                    )
            except support.ActionError as error:
                return messagebox.showerror("Invalid Word", str(error))

        elif action == PlayerActions.SWAP:
            if len(self._game.get_play()) == 0 and \
                    not messagebox.askokcancel("Confirm Swap", "Are you sure you want to swap {} letter{}?".format(
                            len(self._active_tiles), 's' if len(self._active_tiles) != 1 else ''
                    )):
                return

            try:
                self._game.swap_letters(self._active_tiles)
                self._active_tiles = set()
            except support.ActionErrorsupport.ActionError as error:
                return messagebox.showerror("Invalid Action", str(error))
        elif action == PlayerActions.PASS:
            self._game.skip()
        elif action == PlayerActions.RESET:
            for position, _ in self._game.clear_play():
                self._view.draw_tile(position, None)

            self._active_tiles = set()
        else:
            # This should never happen, but as a precaution
            raise RuntimeError("Unknown event {}".format(action))

        if self._game.is_game_over():
            rankings = self._game.get_rankings()
            winners = []
            score = rankings[0].get_score()

            for player in rankings:
                if player.get_score() < score:
                    break

                winners.append(player.get_name())

            winner = " & ".join(winners)

            messagebox.showinfo(
                "Game Over",
                f"Congratulations to {winner} for winning, with {score} points!"
            )

            return

        self._player_view.draw_player(self._game.get_active_player())
        self.draw_players_view()

    def _handle_rack_click(self, position):
        """Handles a click on the player's tile rack
        
        Parameters:
            position (tuple<int, int>): Row, column position of the tile that was clicked
                                        For single row racks, this is interpreted as (0, index)
        """
        if self._game.is_game_over():
            return

        _, index = position

        self.toggle_tile(index)

    def _handle_board_click(self, position):
        """Handles a click on the board
        
        Parameters:
            position (tuple<int, int>): Row, column position of the tile that was clicked
        """

        if self._game.is_game_over():
            return

        player = self._game.get_active_player()

        if len(self._active_tiles) == 0:
            if not self._game.is_position_active(position):
                return
            # replace tile to player's rack
            self._game.pickup_letter(position)
            self._view.draw_tile(position, None)
        elif len(self._active_tiles) > 1:
            return messagebox.showwarning(
                "Multiple Tiles Selected",
                "Only one tile can be placed at a time")
        else:  # place active tile

            # Ignore placement on existing tile
            if self._game.get_letter(position):
                return

            tile = self._active_tiles.pop()

            try:
                letter = self._game.place_letter(tile, position)

                if isinstance(letter, Wildcard):
                    while True:
                        char = simpledialog.askstring(
                            "Wildcard",
                            "Please choose a letter for this wildcard")

                        if char is None:
                            self._game.pickup_letter(position)
                            return self._active_tiles.add(tile)

                        char = char.strip().upper()

                        if len(char) == 1 and char.isalpha():
                            letter.set_letter(char)
                            break

                self._view.draw_tile(position, letter)
            except support.PositionError as error:
                self._active_tiles.add(tile)
                return messagebox.showerror("Invalid Move", str(error))

        # redraw player_view
        self._player_view.draw_player(player)

    def draw_board(self):
        """Draws the board, start tile & all bonuses"""
        board = self._game.get_board()
        self._view.draw_board(board.get_start(), board.get_all_bonuses())

    def draw_players_view(self):
        """Draws the players view"""
        output = ["Players:"]

        for player in self._game.get_players():
            output.append("{:<20} {:>4}".format(player.get_name(),
                                                player.get_score()))

        self._players.config(text=("\n\n").join(output))
コード例 #7
0
    def __init__(
        self,
        target_building,
        exp_id='none',
        conf={
            'source_buildings': ['ebu3b'],
            'source_samples_list': [5],
            'logger_postfix': 'temp',
            'seed_num': 5
        }):
        super(ScrabbleInterface, self).__init__(conf, exp_id, 'scrabble')
        self.target_building = target_building
        self.source_buildings = conf['source_buildings']
        self.sample_num_list = conf['source_samples_list']
        self.seed_num = conf['seed_num']
        if self.target_building not in self.source_buildings:
            self.source_buildings = self.source_buildings + [
                self.target_building
            ]
            self.sample_num_list = self.sample_num_list + [self.seed_num]
        conf['use_cluster_flag'] = True
        conf['use_brick_flag'] = True
        conf['negative_flag'] = True
        self.logger_postfix = conf['logger_postfix']

        column_names = ['VendorGivenName', 'BACnetName', 'BACnetDescription']

        self.building_sentence_dict = dict()
        self.building_label_dict = dict()
        self.building_tagsets_dict = dict()
        for building in self.source_buildings:
            true_tagsets = {}
            label_dict = {}
            for labeled in LabeledMetadata.objects(building=building):
                srcid = labeled.srcid
                true_tagsets[srcid] = labeled.tagsets
                fullparsing = None
                for clm in column_names:
                    one_fullparsing = [i[1] for i in labeled.fullparsing[clm]]
                    if not fullparsing:
                        fullparsing = one_fullparsing
                    else:
                        fullparsing += ['O'] + one_fullparsing
                        #  This format is alinged with the sentence
                        #  conformation rule.
                label_dict[srcid] = fullparsing

            self.building_tagsets_dict[building] = true_tagsets
            self.building_label_dict[building] = label_dict
            sentence_dict = dict()
            for raw_point in RawMetadata.objects(building=building):
                srcid = raw_point.srcid
                if srcid in true_tagsets:
                    metadata = raw_point['metadata']
                    sentence = None
                    for clm in column_names:
                        if not sentence:
                            sentence = [c for c in metadata[clm].lower()]
                        else:
                            sentence += ['\n'] + \
                                        [c for c in metadata[clm].lower()]
                    sentence_dict[srcid] = sentence
            self.building_sentence_dict[building] = sentence_dict

        # Validation of the dataset
        for building in self.source_buildings:
            for srcid, label_pairs in self.building_label_dict[building]\
                                          .items():
                assert len(label_pairs) == \
                           len(self.building_sentence_dict[building][srcid])

        self.scrabble = Scrabble(self.source_buildings, self.target_building,
                                 self.sample_num_list,
                                 self.building_sentence_dict,
                                 self.building_label_dict,
                                 self.building_tagsets_dict, conf)
コード例 #8
0
class ScrabbleInterface(FrameworkInterface):
    """docstring for ScrabbleInterface"""
    def __init__(
        self,
        target_building,
        exp_id='none',
        conf={
            'source_buildings': ['ebu3b'],
            'source_samples_list': [5],
            'logger_postfix': 'temp',
            'seed_num': 5
        }):
        super(ScrabbleInterface, self).__init__(conf, exp_id, 'scrabble')
        self.target_building = target_building
        self.source_buildings = conf['source_buildings']
        self.sample_num_list = conf['source_samples_list']
        self.seed_num = conf['seed_num']
        if self.target_building not in self.source_buildings:
            self.source_buildings = self.source_buildings + [
                self.target_building
            ]
            self.sample_num_list = self.sample_num_list + [self.seed_num]
        conf['use_cluster_flag'] = True
        conf['use_brick_flag'] = True
        conf['negative_flag'] = True
        self.logger_postfix = conf['logger_postfix']

        column_names = ['VendorGivenName', 'BACnetName', 'BACnetDescription']

        self.building_sentence_dict = dict()
        self.building_label_dict = dict()
        self.building_tagsets_dict = dict()
        for building in self.source_buildings:
            true_tagsets = {}
            label_dict = {}
            for labeled in LabeledMetadata.objects(building=building):
                srcid = labeled.srcid
                true_tagsets[srcid] = labeled.tagsets
                fullparsing = None
                for clm in column_names:
                    one_fullparsing = [i[1] for i in labeled.fullparsing[clm]]
                    if not fullparsing:
                        fullparsing = one_fullparsing
                    else:
                        fullparsing += ['O'] + one_fullparsing
                        #  This format is alinged with the sentence
                        #  conformation rule.
                label_dict[srcid] = fullparsing

            self.building_tagsets_dict[building] = true_tagsets
            self.building_label_dict[building] = label_dict
            sentence_dict = dict()
            for raw_point in RawMetadata.objects(building=building):
                srcid = raw_point.srcid
                if srcid in true_tagsets:
                    metadata = raw_point['metadata']
                    sentence = None
                    for clm in column_names:
                        if not sentence:
                            sentence = [c for c in metadata[clm].lower()]
                        else:
                            sentence += ['\n'] + \
                                        [c for c in metadata[clm].lower()]
                    sentence_dict[srcid] = sentence
            self.building_sentence_dict[building] = sentence_dict

        # Validation of the dataset
        for building in self.source_buildings:
            for srcid, label_pairs in self.building_label_dict[building]\
                                          .items():
                assert len(label_pairs) == \
                           len(self.building_sentence_dict[building][srcid])

        self.scrabble = Scrabble(self.source_buildings, self.target_building,
                                 self.sample_num_list,
                                 self.building_sentence_dict,
                                 self.building_label_dict,
                                 self.building_tagsets_dict, conf)

    @exec_measurement
    def learn_auto(self, iter_num=1):
        params = (self.source_buildings, self.sample_num_list,
                  self.target_building)
        self.learned_srcids = []
        params = {
            'use_cluster_flag': True,
            'use_brick_flag': True,
            'negative_flag': True,
            'target_building': self.target_building,
            'building_list': self.source_buildings,
            'sample_num_list': self.scrabble.sample_num_list
        }
        #self.scrabble.char2tagset_iteration(iter_num, self.logger_postfix, *params)
        step_data = {
            'iter_num':
            0,
            'next_learning_srcids':
            self.scrabble.get_random_srcids(self.scrabble.building_srcid_dict,
                                            self.source_buildings,
                                            self.sample_num_list),
            'model_uuid':
            None
        }
        step_datas = [step_data]
        step_datas.append(
            self.scrabble.char2tagset_onestep(step_data, **params))
        pdb.set_trace()

    @exec_measurement
    def learn_auto2(self, iter_num=1):
        num_sensors_in_gray = 10000
        while num_sensors_in_gray > 0:
            new_srcids = self.zodiac.select_informative_samples_only(10)
            self.update_model(new_srcids)
            num_sensors_in_gray = self.zodiac.get_num_sensors_in_gray()
            pred_point_tagsets = self.zodiac.predict(self.target_srcids)
            for i, srcid in enumerate(self.target_srcids):
                self.pred['tagsets'][srcid] = set([pred_point_tagsets[i]])
            print(num_sensors_in_gray)
            self.evaluate()
        pdb.set_trace()
コード例 #9
0
ファイル: scrabble_new.py プロジェクト: jkatofsky/plastering
    def __init__(self,
                 target_building,
                 target_srcids,
                 source_buildings,
                 config=None
                 ):
        config['required_label_types'] = [POINT_TAGSET,
                                          FULL_PARSING,
                                          ALL_TAGSETS]
        super(ScrabbleInterface, self).__init__(
            target_building=target_building,
            target_srcids=target_srcids,
            source_buildings=source_buildings,
            config=config,
            framework_name='scrabble')

        self.target_label_type = ALL_TAGSETS

        if not config:
            config = {}

        # Prepare config for Scrabble object
        if 'seed_num' in config:
            seed_num = config['seed_num']
        else:
            seed_num = 10

        if 'sample_num_list' in config:
            sample_num_list = config['sample_num_list']
        else:
            sample_num_list = [seed_num] * len(set(source_buildings +
                                            [target_building]))

        if self.target_building not in self.source_buildings:
            self.source_buildings = self.source_buildings + [self.target_building]
        if len(self.source_buildings) > len(sample_num_list):
            sample_num_list.append(0)

        if 'use_cluster_flag' not in config:
            config['use_cluster_flag'] = True
        if 'use_brick_flag' not in config:
            config['use_brick_flag'] = True
        if 'negative_flag' not in config:
            config['negative_flag'] = True
        if 'tagset_classifier_type' not in config:
            config['tagset_classifier_type'] = 'MLP'
        if 'crfqs' not in config:
            config['crfqs'] = 'confidence'
        if 'entqs' not in config:
            config['entqs'] = 'phrase_util'
        if 'n_jobs' not in config:
            config['n_jobs'] = 10
        if 'use_known_tags' not in config:
            config['use_known_tags'] = False
        if 'apply_filter_flag' not in config:
            self.apply_filter_flag = False
        else:
            self.apply_filter_flag = config['apply_filter_flag']
        if 'apply_validating_samples' not in config:
            self.apply_validating_samples = False
        else:
            self.apply_validating_samples = config['apply_validating_samples']

        # TODO: This should be migrated into Plastering
        building_sentence_dict, target_srcids, building_label_dict,\
            building_tagsets_dict, known_tags_dict = load_data(target_building,
                                                               source_buildings)
        self.scrabble = Scrabble(target_building,
                                 target_srcids,
                                 building_label_dict,
                                 building_sentence_dict,
                                 building_tagsets_dict,
                                 source_buildings,
                                 sample_num_list,
                                 known_tags_dict,
                                 config=config,
                                 )
        #self.update_model([])
        new_srcids = deepcopy(self.scrabble.learning_srcids)
        if self.hotstart:
            new_srcids = [obj.srcid for obj in self.query_labels(building=target_building)]
        self.scrabble.clear_training_samples()
        self.update_model(new_srcids)
        self.zodiac_good_preds = {}
コード例 #10
0
    def __init__(self,
                 target_building,
                 target_srcids,
                 source_buildings,
                 config=None
                 ):
        super(ScrabbleInterface, self).__init__(
            target_building=target_building,
            target_srcids=target_srcids,
            source_buildings=source_buildings,
            config=config,
            framework_name='scrabble')

        if not config:
            config = {}

        # Prepare config for Scrabble object
        if 'sample_num_list' in config:
            sample_num_list = config['sample_num_list']
        else:
            sample_num_list = [0] * (len(source_buildings) + 1) # +1 for target

        if self.target_building not in self.source_buildings:
            self.source_buildings = self.source_buildings + [self.target_building]
        if len(self.source_buildings) > len(sample_num_list):
            sample_num_list.append(0)
        if 'use_cluster_flag' not in config:
            config['use_cluster_flag'] = True
        if 'use_brick_flag' not in config:
            config['use_brick_flag'] = True
        if 'negative_flag' not in config:
            config['negative_flag'] = True

        column_names = ['VendorGivenName',
                         'BACnetName',
                         'BACnetDescription']

        self.building_sentence_dict = dict()
        self.building_label_dict = dict()
        self.building_tagsets_dict = dict()
        for building in self.source_buildings:
            true_tagsets = {}
            label_dict = {}
            for labeled in self.query_labels(building=building):
                srcid = labeled.srcid
                true_tagsets[srcid] = labeled.tagsets
                fullparsing = None
                for clm in column_names:
                    one_fullparsing = [i[1] for i in labeled.fullparsing[clm]]
                    if not fullparsing:
                        fullparsing = one_fullparsing
                    else:
                        fullparsing += ['O'] + one_fullparsing
                        #  This format is alinged with the sentence 
                        #  conformation rule.
                label_dict[srcid] = fullparsing

            self.building_tagsets_dict[building] = true_tagsets
            self.building_label_dict[building] = label_dict
            sentence_dict = dict()
            for raw_point in RawMetadata.objects(building=building):
                srcid = raw_point.srcid
                if srcid in true_tagsets:
                    metadata = raw_point['metadata']
                    sentence = None
                    for clm in column_names:
                        if not sentence:
                            sentence = [c for c in metadata[clm].lower()]
                        else:
                            sentence += ['\n'] + \
                                        [c for c in metadata[clm].lower()]
                    sentence_dict[srcid]  = sentence
            self.building_sentence_dict[building] = sentence_dict

        # Validation of the dataset
        for building in self.source_buildings:
            for srcid, label_pairs in self.building_label_dict[building]\
                                          .items():
                assert len(label_pairs) == \
                           len(self.building_sentence_dict[building][srcid])

        self.scrabble = Scrabble(
            target_building=self.target_building,
            target_srcids=self.target_srcids,
            building_label_dict=self.building_label_dict,
            building_sentence_dict=self.building_sentence_dict,
            building_tagsets_dict=self.building_tagsets_dict,
            source_buildings=self.source_buildings,
            source_sample_num_list=sample_num_list,
            conf=config,
            learning_srcids=[])
コード例 #11
0
class ScrabbleInterface(Inferencer):
    """docstring for ScrabbleInterface"""
    def __init__(self,
                 target_building,
                 target_srcids,
                 source_buildings,
                 config=None
                 ):
        super(ScrabbleInterface, self).__init__(
            target_building=target_building,
            target_srcids=target_srcids,
            source_buildings=source_buildings,
            config=config,
            framework_name='scrabble')

        if not config:
            config = {}

        # Prepare config for Scrabble object
        if 'sample_num_list' in config:
            sample_num_list = config['sample_num_list']
        else:
            sample_num_list = [0] * (len(source_buildings) + 1) # +1 for target

        if self.target_building not in self.source_buildings:
            self.source_buildings = self.source_buildings + [self.target_building]
        if len(self.source_buildings) > len(sample_num_list):
            sample_num_list.append(0)
        if 'use_cluster_flag' not in config:
            config['use_cluster_flag'] = True
        if 'use_brick_flag' not in config:
            config['use_brick_flag'] = True
        if 'negative_flag' not in config:
            config['negative_flag'] = True

        column_names = ['VendorGivenName',
                         'BACnetName',
                         'BACnetDescription']

        self.building_sentence_dict = dict()
        self.building_label_dict = dict()
        self.building_tagsets_dict = dict()
        for building in self.source_buildings:
            true_tagsets = {}
            label_dict = {}
            for labeled in self.query_labels(building=building):
                srcid = labeled.srcid
                true_tagsets[srcid] = labeled.tagsets
                fullparsing = None
                for clm in column_names:
                    one_fullparsing = [i[1] for i in labeled.fullparsing[clm]]
                    if not fullparsing:
                        fullparsing = one_fullparsing
                    else:
                        fullparsing += ['O'] + one_fullparsing
                        #  This format is alinged with the sentence 
                        #  conformation rule.
                label_dict[srcid] = fullparsing

            self.building_tagsets_dict[building] = true_tagsets
            self.building_label_dict[building] = label_dict
            sentence_dict = dict()
            for raw_point in RawMetadata.objects(building=building):
                srcid = raw_point.srcid
                if srcid in true_tagsets:
                    metadata = raw_point['metadata']
                    sentence = None
                    for clm in column_names:
                        if not sentence:
                            sentence = [c for c in metadata[clm].lower()]
                        else:
                            sentence += ['\n'] + \
                                        [c for c in metadata[clm].lower()]
                    sentence_dict[srcid]  = sentence
            self.building_sentence_dict[building] = sentence_dict

        # Validation of the dataset
        for building in self.source_buildings:
            for srcid, label_pairs in self.building_label_dict[building]\
                                          .items():
                assert len(label_pairs) == \
                           len(self.building_sentence_dict[building][srcid])

        self.scrabble = Scrabble(
            target_building=self.target_building,
            target_srcids=self.target_srcids,
            building_label_dict=self.building_label_dict,
            building_sentence_dict=self.building_sentence_dict,
            building_tagsets_dict=self.building_tagsets_dict,
            source_buildings=self.source_buildings,
            source_sample_num_list=sample_num_list,
            conf=config,
            learning_srcids=[])

    def learn_auto(self, iter_num=1):
        params = (self.source_buildings,
                  self.sample_num_list,
                  self.target_building)
        self.learned_srcids = []
        params = {
            'use_cluster_flag': True,
            'use_brick_flag': True,
            'negative_flag': True,
            'target_building': self.target_building,
            'building_list': self.source_buildings,
            'sample_num_list': self.scrabble.sample_num_list
            }
        #self.scrabble.char2tagset_iteration(iter_num, self.logger_postfix, *params)
        step_data = {'iter_num':0,
                     'next_learning_srcids': self.scrabble.get_random_srcids(
                                            self.scrabble.building_srcid_dict,
                                            self.source_buildings,
                                            self.sample_num_list),
                     'model_uuid': None}
        step_datas = [step_data]
        step_datas.append(self.scrabble.char2tagset_onestep(step_data,
                                                            **params))

    def update_model(self, srcids):
        self.scrabble.update_model(srcids)

    def predict(self, target_srcids=None):
        return self.scrabble.predict(target_srcids)

    def predict_proba(self, target_srcids=None):
        return self.scrabble.predict_proba(target_srcids)

    def select_informative_samples(self, sample_num=10):
        return self.scrabble.select_informative_samples_only(sample_num)
コード例 #12
0
import sys
from PyQt5.QtWidgets import QApplication
from scrabble import Scrabble

if __name__ == '__main__':
    app = QApplication(sys.argv)
    app.setApplicationName('Scrabble')

    main = Scrabble()
    main.showMaximized()
    sys.exit(app.exec_())
コード例 #13
0
from scrabble import Scrabble

s = Scrabble(players=3)

first = s.who_goes_first()

s.pick_first_tiles()

coords = [
    (0, 0),
    (0, 1),
    (0, 2),
]

word = list()

word = 'ZOO'

s.add_word(s.player_turn_queue, 'test', word, coords)
# CAT
# { player: [1], word: { C: (7, 7), A: (7, 8), T: (7, 9) }

s.get_tiles(player=1)
test = s.scoreboard.get_word_score(player=1, turn=1)

stop = None
コード例 #14
0
from scrabble import Scrabble
import sys
from trie_lexicon import TrieLexicon
import time

language = 'en'
character_distribution = 'resources/scrabble_letter_distribution-{}.txt'.format(language)
vocabulary = 'resources/vocab_{}-cleaned.txt'.format(language)

print('Setting up lexicon ...')
start_time = time.time()
lexicon = TrieLexicon(vocabulary)
duration = time.time() - start_time
print("{:,} words loaded in {:.5f} seconds and {:,} bytes".format(lexicon.nwords, duration, sys.getsizeof(lexicon)))

scrabble = Scrabble(character_distribution)
tiles = scrabble.draw_tiles(7)
print('\nYou have drawn the following letters:')
print(', '.join(tiles))

start_time = time.time()
words = lexicon.find_words(tiles)
duration = time.time() - start_time
print('\nFrom these letters I found {:,} words in {:.5f} seconds'.format(len(words), duration))
      
scored_words = scrabble.score_words(words)
print('\nThe top 10 scoring words are:')
for score, word in scored_words[:10]:
    print(score, word)
       
コード例 #15
0
ファイル: entry.py プロジェクト: jbkoh/scrabble
            use_cluster_flag=True,
            sentence_dict=building_sentence_dict[building],
            shuffle_flag=False)
else:  # Not commonly used but only for rigid evaluation.
    if not os.path.isfile(srcidfile):
        raise Exception('{0} is not a file'.format(srcidfile))
    with open(srcidfile, 'r') as fp:
        predefined_learning_srcids = json.load(fp)

if framework_type == 'char2ir':
    from scrabble import Scrabble
    scrabble = Scrabble(target_building,
                        target_srcids,
                        building_label_dict,
                        building_sentence_dict,
                        building_tagsets_dict,
                        source_buildings,
                        source_sample_num_list,
                        known_tags_dict={},
                        config=config,
                        learning_srcids=predefined_learning_srcids)
    framework = scrabble.char2ir
elif framework_type == 'ir2tagsets':
    from scrabble import Scrabble
    scrabble = Scrabble(
        target_building,
        target_srcids,
        building_label_dict,
        building_sentence_dict,
        building_tagsets_dict,
        source_buildings,
        source_sample_num_list,
コード例 #16
0
ファイル: game.py プロジェクト: gkotas/Scrabble
class GameScene(SceneBase):
    def __init__(self):
        SceneBase.__init__(self)
        self.scrabble = Scrabble(True)
        self.board = Board('imgs/board.jpg', [0, 0])
        self.letter_ss = Spritesheet('imgs/letters.jpg')
        self.player_tiles = []
        self.game_tiles = []
        self.selected_tile = None
        self.offset_x = 0
        self.offset_y = 0

        for i, letter in enumerate(self.scrabble.get_rack()):
            self.player_tiles.append(
                Tile(letter, self.letter_ss, PLAYER_TILE_POSITIONS[i]))

    def process_input(self, events, pressed_keys):
        for event in events:
            if event.type == pygame.KEYDOWN:
                if event.key in (pygame.K_RETURN, pygame.K_KP_ENTER):
                    self._submit_turn()
                elif event.key == pygame.K_p:
                    self.scrabble._print_board()

            elif event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 1:
                    for tile in self.player_tiles:
                        if tile.rect.collidepoint(event.pos):
                            self.selected_tile = tile
                            mouse_x, mouse_y = event.pos
                            self.offset_x = tile.rect.left - mouse_x
                            self.offset_y = tile.rect.top - mouse_y

            elif event.type == pygame.MOUSEBUTTONUP:
                if event.button == 1:
                    if self.selected_tile:
                        if self._hits_tile(event.pos, self.selected_tile):
                            self.selected_tile.rerack()
                        else:
                            self.selected_tile.move(event.pos)

                        # Not selected anymore
                        self.selected_tile = None

            elif event.type == pygame.MOUSEMOTION:
                if self.selected_tile:
                    mouse_x, mouse_y = event.pos
                    self.selected_tile.rect.left = mouse_x + self.offset_x
                    self.selected_tile.rect.top = mouse_y + self.offset_y

    def update(self):
        pass

    def render(self, screen):
        # The game scene is just a blank blue screen
        screen.fill((0, 0, 255))
        screen.blit(self.board.image, self.board.rect)

        for tile in self.player_tiles:
            screen.blit(tile.image, tile.rect)

        for tile in self.game_tiles:
            screen.blit(tile.image, tile.rect)

        # Make selected tile on top
        if self.selected_tile:
            screen.blit(self.selected_tile.image, self.selected_tile.rect)

    def _hits_tile(self, pos, ignore=None):
        """Returns true if the position hits a tile."""
        for tile in self.player_tiles + self.game_tiles:
            if tile == ignore:
                continue
            if tile.rect.collidepoint(pos):
                return True
        return False

    def _submit_turn(self):
        """
        Submits the turn to the scrabble backend. Moves the player tiles to
        game tiles and updates the player tiles.
        """
        # Get a list of tiles that will be sumbit
        tiles = []
        for tile in self.player_tiles:
            if tile.on_board:
                tiles.append(tile.tile())

        # Not a turn if there's no tiles on board
        if len(tiles) == 0:
            return

        if self.scrabble.submit_turn(tiles):
            # Valid turn, move all played tiles to game.
            for tile in self.player_tiles:
                if tile.on_board:
                    self.game_tiles.append(tile)

            # Update the player tiles
            self.player_tiles = []
            for i, letter in enumerate(self.scrabble.get_rack()):
                self.player_tiles.append(
                    Tile(letter, self.letter_ss, PLAYER_TILE_POSITIONS[i]))

        else:
            # Invalid turn, return all tiles to rack
            for tile in self.player_tiles:
                tile.rerack()
コード例 #17
0
ファイル: char2tagset_test.py プロジェクト: jbkoh/scrabble-1
    predefined_learning_srcids = []
    for building, source_sample_num in zip(source_buildings,
                                           source_sample_num_list):
        predefined_learning_srcids += select_random_samples(building,
                                                 building_tagsets_dict[building].keys(),
                                                 source_sample_num,
                                                 True)
    with open(learning_srcid_file, 'w') as fp:
        json.dump(predefined_learning_srcids, fp)

scrabble = Scrabble(target_building,
                    target_srcids,
                    building_label_dict,
                    building_sentence_dict,
                    building_tagsets_dict,
                    source_buildings,
                    source_sample_num_list,
                    known_tags_dict,
                    config=config,
                    learning_srcids=predefined_learning_srcids
                    )

scrabble.update_model([])
history = []
for i in range(0, 20):
    t2 = arrow.get()
    new_srcids = scrabble.select_informative_samples(10)
    scrabble.update_model(new_srcids)
    pred = scrabble.predict(target_srcids + scrabble.learning_srcids)
    pred_tags = scrabble.predict_tags(target_srcids)
    tot_acc, tot_point_acc, learning_acc, learning_point_acc = \
コード例 #18
0
ファイル: my_app.py プロジェクト: monkey72/dev-sprint5
import flask
import settings

# Views
from main import Main
from login import Login
from scrabble import Scrabble

app = flask.Flask(__name__)
app.secret_key = settings.secret_key

# Routes
app.add_url_rule('/',
                 view_func=Main.as_view('main'),
                 methods=["GET"])
app.add_url_rule('/<page>/',
                 view_func=Main.as_view('main'),
                 methods=["GET"])
app.add_url_rule('/login/',
                 view_func=Login.as_view('login'),
                 methods=["GET", "POST"])
app.add_url_rule('/scrabble/',
                 view_func=Scrabble.as_view('scrabble'),
                 methods=["GET", "POST"])

@app.errorhandler(404)
def page_not_found(error):
	return flask.render_template('404.html'), 404

app.debug = True
app.run()
コード例 #19
0
ファイル: scrabble_new.py プロジェクト: jkatofsky/plastering
class ScrabbleInterface(Inferencer):
    """docstring for ScrabbleInterface"""
    def __init__(self,
                 target_building,
                 target_srcids,
                 source_buildings,
                 config=None
                 ):
        config['required_label_types'] = [POINT_TAGSET,
                                          FULL_PARSING,
                                          ALL_TAGSETS]
        super(ScrabbleInterface, self).__init__(
            target_building=target_building,
            target_srcids=target_srcids,
            source_buildings=source_buildings,
            config=config,
            framework_name='scrabble')

        self.target_label_type = ALL_TAGSETS

        if not config:
            config = {}

        # Prepare config for Scrabble object
        if 'seed_num' in config:
            seed_num = config['seed_num']
        else:
            seed_num = 10

        if 'sample_num_list' in config:
            sample_num_list = config['sample_num_list']
        else:
            sample_num_list = [seed_num] * len(set(source_buildings +
                                            [target_building]))

        if self.target_building not in self.source_buildings:
            self.source_buildings = self.source_buildings + [self.target_building]
        if len(self.source_buildings) > len(sample_num_list):
            sample_num_list.append(0)

        if 'use_cluster_flag' not in config:
            config['use_cluster_flag'] = True
        if 'use_brick_flag' not in config:
            config['use_brick_flag'] = True
        if 'negative_flag' not in config:
            config['negative_flag'] = True
        if 'tagset_classifier_type' not in config:
            config['tagset_classifier_type'] = 'MLP'
        if 'crfqs' not in config:
            config['crfqs'] = 'confidence'
        if 'entqs' not in config:
            config['entqs'] = 'phrase_util'
        if 'n_jobs' not in config:
            config['n_jobs'] = 10
        if 'use_known_tags' not in config:
            config['use_known_tags'] = False
        if 'apply_filter_flag' not in config:
            self.apply_filter_flag = False
        else:
            self.apply_filter_flag = config['apply_filter_flag']
        if 'apply_validating_samples' not in config:
            self.apply_validating_samples = False
        else:
            self.apply_validating_samples = config['apply_validating_samples']

        # TODO: This should be migrated into Plastering
        building_sentence_dict, target_srcids, building_label_dict,\
            building_tagsets_dict, known_tags_dict = load_data(target_building,
                                                               source_buildings)
        self.scrabble = Scrabble(target_building,
                                 target_srcids,
                                 building_label_dict,
                                 building_sentence_dict,
                                 building_tagsets_dict,
                                 source_buildings,
                                 sample_num_list,
                                 known_tags_dict,
                                 config=config,
                                 )
        #self.update_model([])
        new_srcids = deepcopy(self.scrabble.learning_srcids)
        if self.hotstart:
            new_srcids = [obj.srcid for obj in self.query_labels(building=target_building)]
        self.scrabble.clear_training_samples()
        self.update_model(new_srcids)
        self.zodiac_good_preds = {}


    def learn_auto(self, iter_num=25, inc_num=10):
        for i in range(0, iter_num):
            print('--------------------------')
            print('{0}th iteration'.format(i))
            new_srcids = self.select_informative_samples(inc_num)
            self.update_model(new_srcids)
            self.evaluate(self.target_srcids)
            print('curr new srcids: {0}'.format(len(new_srcids)))
            print('training srcids: {0}'.format(len(self.training_srcids)))
            print('f1: {0}'.format(self.history[-1]['metrics']['f1']))
            print('macrof1: {0}'.format(self.history[-1]['metrics']['macrof1']))

    def update_model(self, new_srcids):
        super(ScrabbleInterface, self).update_model(new_srcids)
        self.scrabble.update_model(new_srcids)

    def postprocessing_pred(self, pred):
        # Currently only ingest point tagsets.
        pred_g = self.new_graph(empty=True)
        for srcid, tagsets in pred.items():
            point_tagset = sel_point_tagset(tagsets, srcid)
            point_prob = 1 # temporary
            pred_g.add_pred_point_result(pred_g, srcid,
                                         point_tagset, point_prob)
        return pred_g

    def predict(self, target_srcids=None, all_tagsets=False):
        if not target_srcids:
            target_srcids = self.target_srcids
        pred = self.scrabble.predict(target_srcids)
        if self.apply_filter_flag:
            pred = self.apply_filter_by_zodiac(pred)
        self.pred_g = self.postprocessing_pred(pred)

        if all_tagsets:
            return self.pred_g, pred # This should be generalized inside
                                     # postprocessing_pred
        else:
            return self.pred_g

    def predict_proba(self, target_srcids=None):
        return self.scrabble.predict_proba(target_srcids)

    def apply_prior_zodiac(self, sample_num):
        if not self.prior_g:
            return []
        instances = get_instance_tuples(self.prior_g)
        good_preds = {}
        for srcid, point_tagset in instances.items():
            triple = (BASE[srcid], RDF.type, BRICK[point_tagset])
            if self.prior_confidences[triple] > 0.9:
                good_preds[srcid] = point_tagset
        pred_g = self.predict()
        incorrect_srcids = []
        for srcid, good_point_tagset in good_preds.items():
            pred_point_tagset = get_point_type(pred_g, BASE[srcid])
            if (good_point_tagset != pred_point_tagset) or\
               (good_point_tagset == 'unknown' and pred_point_tagset == 'none') or\
               (good_point_tagset == 'none' and pred_point_tagset == 'unknown'):
                incorrect_srcids.append(srcid)
        if not incorrect_srcids:
            return []
        new_srcids = select_random_samples(
            building=self.target_building,
            srcids=incorrect_srcids,
            n=sample_num,
            use_cluster_flag=True,
            sentence_dict=self.scrabble.char2ir.sentence_dict,
            unique_clusters_flag=True,
        )
        return new_srcids

    def is_same_tagset(self, tagset1, tagset2):
        if tagset1 == tagset2:
            return True
        elif tagset1 == 'none' and tagset2 == 'unknown':
            return True
        elif tagset1 == 'unknown' and tagset2 == 'none':
            return True
        else:
            return False

    def apply_filter_by_zodiac(self, pred):
        if not self.prior_g:
            return pred
        instances = get_instance_tuples(self.prior_g)
        self.zodiac_good_preds = {}
        for srcid, point_tagset in instances.items():
            triple = (BASE[srcid], RDF.type, BRICK[point_tagset])
            if self.prior_confidences[triple] > 0.8:
                self.zodiac_good_preds[srcid] = point_tagset
        fixed_cnt = 0
        for srcid, pred_tagsets in pred.items():
            pred_point_tagset = sel_point_tagset(pred_tagsets, srcid)
            good_point_tagset = self.zodiac_good_preds.get(srcid, None)
            if not good_point_tagset:
                continue
            if not self.is_same_tagset(pred_point_tagset, good_point_tagset):
                pred_tagsets = [tagset for tagset in pred_tagsets
                                if not is_point_tagset(tagset)]
                pred_tagsets.append(good_point_tagset)
                print('FIXED {0}, {1} -> {2}'.format(srcid,
                                                     pred_point_tagset,
                                                     good_point_tagset))
                fixed_cnt += 1
                pred[srcid] = pred_tagsets
        print('TOTAL_FIXED_POINTS: {0}'.format(fixed_cnt))
        return pred

    def select_informative_samples(self, sample_num=10):
        # Use prior (e.g., from Zodiac.)
        new_srcids = []
        #if self.apply_validating_samples:
        #    new_srcids += self.apply_prior_zodiac(sample_num)
        if len(new_srcids) < sample_num:
            new_srcids += self.scrabble.select_informative_samples(
                sample_num - len(new_srcids))
        #new_srcids = [srcid for srcid in new_srcids
        #              if srcid not in self.zodiac_good_preds][0:sample_num]
        return new_srcids