#!/bin/env python3 #-*- coding: utf8 -*- """ This module supplies functions to manage the game, it is designed to be used by a main that implements an interface. """ #PYTHON PACKAGES IMPORTS import re from random import choice import wikipedia as wkp #LOCAL IMPORTS import src.tagging as tg import src.modify_sentences as ms wkp.set_lang('fr') def all_names(): """ A function that returns all named available for the game """ with open('wikipages.txt', 'r') as namefile: names = namefile.readlines() return [name.strip('\n ') for name in names] def new_name(names, old=''): """ A function that randomly choose a new name to guess for the game avoiding the current one. Input : - names : the list of names we want too choose from - old : the current name Output : - Returns a string. """ new = choice(names) while new == old: new = choice(names) return new def spaces_parsing(text): """ A function that regularize spaces Arguments: text {string} -- text where we need to clean spaces Returns: [string] -- Returns modified text """ text = re.sub(r'\s+', ' ', text) text = re.sub(r'\s([,\.])', r'\1', text) text = re.sub(r'([\'’´])\s', r'\1', text) text = re.sub(r'(\S)([:;!?])', r'\1 \2', text) text = re.sub(r'([,:;!?])(\S)', r'\1 \2', text) text = re.sub(r'([.])([^.\s])', r'\1 \2', text) return text def clean_sentences(tagged_sentences): """ A function that transforms tagged sentences into clean sentences Input : - tagged_sentences : a list of tuples Output : - Returns a list of strings """ return [spaces_parsing(' '.join([stce[1] for stce in sentence])) for sentence in tagged_sentences] def initial_sentences(article, article_file): """ A function that get the summary from a wikipedia page and tags it Input : - article : the name of the wikipedia page - article_file : a file where all the steps of transformation are registered Output : - Returns a list of tagged sentences. """ wikipage = wkp.page(article) page_summ = wikipage.summary page_title = wikipage.title article_file.write(page_title+'\n\n') article_file.write("----- Initial text : -----\n\n") article_file.write(page_summ+'\n\n') new_sentences = [] for sentence in tg.separe_sentences(page_summ, page_title, article_file): new_sentences.append( tg.check_entities( tg.transform_sentence(sentence, article_file), page_title)) return new_sentences def generate_sentences(sentences, article_file): """ A function that transform third person tagged sentences into first person tagged sentences Input : - sentences : a list of tagged sentences - article_file : a file where all the steps of transformation are registered Output : - Returns a list of tagged sentences. """ return ms.replace_sujet_verbe(sentences, article_file) def update_score(score, operation, total=False): """A function that updates the score and prevent it to go below 0.""" which = 'total' if total else 'current' if score[which] + int(operation) >= 0: score[which] += int(operation) score['last_op'] = operation + ('(total)' if total else '') return score def game(name='', score=None): """ A function that runs a game Input : - name : current name to find - names : list of all available names for the game - score : dictionnary of the state of the score Output : - Returns a dict containing : - the name chosen for the game - a list of tagged sentences to use - a list of used tagged sentences - a dictionnary representing the score """ name = new_name(all_names(), name) article_file = open('./ArticlesReports/'+name+'.txt', 'w') sentences = initial_sentences(name, article_file) new_stces = generate_sentences(sentences, article_file) if score is None: score = {'total':0, 'games':0} score['current'] = len(new_stces)*100 score['last_op'] = '+'+str(score['current']) #print('\n'.join(clean_sentences(sentences))) #print('\n'.join(clean_sentences(new_sentences))) new_stces = clean_sentences(new_stces) article_file.write("\n ----- Final output : -----\n\n") for sent in new_stces: article_file.write(sent+'\n') article_file.close() used_stces = [new_stces.pop(0)] return {'name':name, 'new_stces':new_stces, 'used_stces':used_stces, 'score':score} def next_stce(game_data, minus='-100'): """ This function gives one more sentence to be showed to the player Input: - game_data : a dictionnary summarizing the state of the game - minus : a string representing the number of point to remove from the player's score Output: - a tuple containing: - a boolean (whether there is sentences left or not) - the updated game data """ last = len(game_data['new_stces']) == 1 if game_data['new_stces']: game_data['used_stces'].append(game_data['new_stces'].pop(0)) ret = True else: ret = False game_data['score'] = update_score(game_data['score'], minus) return (ret, last, game_data) def check_answer(game_data, answer): """ Returns whether the answer is the name in the game data or not and updates the score dictionnary """ if answer.lower() == game_data['name'].lower(): game_data['score']['total'] += game_data['score']['current'] return True return False def replay(game_data, won): """ Update the score and launch a new game Input: - game_data : a dictionnary representing the state of the game Output: - a dictionnary representing the state of the game """ if won: game_data['score']['games'] += 1 else: game_data['score'] = update_score(game_data['score'], '-200', True) return game(game_data['name'], game_data['score'])