commited all contents created by participants
This commit is contained in:
@@ -0,0 +1,275 @@
|
||||
# function pour stocker les mots
|
||||
import nltk
|
||||
from duckduckpy import query
|
||||
import random
|
||||
from googletrans import Translator
|
||||
from nltk.corpus import wordnet
|
||||
import codecs
|
||||
|
||||
# ------------------ #
|
||||
# en-tête du script > déclaration des fonctions et des variables
|
||||
# ------------------ #
|
||||
|
||||
# on crée une liste pour stocker tous les mots
|
||||
all_words = []
|
||||
sentences = []
|
||||
|
||||
# fonction qui extrait les mots et les stockent dans une liste
|
||||
def words_extract( str ):
|
||||
tokens = nltk.word_tokenize( str )
|
||||
words = []
|
||||
for word, pos in nltk.pos_tag(tokens):
|
||||
if pos == 'NN':
|
||||
words.append(word)
|
||||
return words
|
||||
|
||||
# fonction qui génère un nombre aléatoire à partir de la liste de mots pour pouvoir choisir un mot
|
||||
def rand_words( list ):
|
||||
# test si la longueur du tableau est egal à zero
|
||||
list_len = len( list )
|
||||
if list_len != 0:
|
||||
ran_num = random.randint(0,list_len-1)
|
||||
return ran_num
|
||||
else:
|
||||
return 0;
|
||||
|
||||
# fonction pour chercher dans duckduckgo
|
||||
def srch_engine( list, num ):
|
||||
if len(list) != 0:
|
||||
print('ok')
|
||||
if list not in all_words:
|
||||
srch = query(list[num], container='dict')
|
||||
res = srch['related_topics'][0]['text']
|
||||
return res
|
||||
else:
|
||||
srch = query(all_words[num], container='dict')
|
||||
res = srch['related_topics'][0]['text']
|
||||
return res
|
||||
else:
|
||||
print('pas ok', num, len(all_words))
|
||||
if num < len(all_words) and num >= 0 :
|
||||
srch = query(all_words[num], container='dict')
|
||||
res = srch['related_topics'][0]['text']
|
||||
return res
|
||||
else:
|
||||
syn = wordnet.synsets('computer')
|
||||
num = random.randint(0,len(syn)-1)
|
||||
print(num)
|
||||
res = syn[num].definition()
|
||||
return res
|
||||
|
||||
|
||||
# fonction pour générer des définitions avec wordnet
|
||||
def words_gen(list, num):
|
||||
if len(list) != 0:
|
||||
print('ok')
|
||||
if list not in all_words:
|
||||
syn = wordnet.synsets(list[num])
|
||||
num = random.randint(0,len(syn)-1)
|
||||
print(num)
|
||||
res = syn[num].definition()
|
||||
return res
|
||||
else:
|
||||
syn = wordnet.synsets(all_words[num])
|
||||
num = random.randint(0,len(syn)-1)
|
||||
print(num)
|
||||
res = syn[num].definition()
|
||||
return res
|
||||
else:
|
||||
print('pas ok')
|
||||
if num < len(all_words) and num >= 0:
|
||||
syn = wordnet.synsets(all_words[num])
|
||||
num = random.randint(0,len(syn)-1)
|
||||
print(num)
|
||||
res = syn[num].definition()
|
||||
return res
|
||||
else:
|
||||
syn = wordnet.synsets('computer')
|
||||
num = random.randint(0,len(syn)-1)
|
||||
print(num)
|
||||
res = syn[num].definition()
|
||||
return res
|
||||
|
||||
|
||||
# fonction principale
|
||||
|
||||
|
||||
|
||||
# ------------------ #
|
||||
# corps du script > appel des fonctions, lecture de la source
|
||||
# ------------------ #
|
||||
|
||||
with open('input.txt') as f:
|
||||
sentences = f.read().splitlines()
|
||||
|
||||
num = rand_words(sentences)
|
||||
|
||||
# phrase de départ
|
||||
sentence = sentences[num]
|
||||
sentence.encode('utf-8')
|
||||
sentence.replace("(...$)/g", "")
|
||||
|
||||
# ------------------- #
|
||||
# niveau 1
|
||||
# ------------------- #
|
||||
|
||||
# 1
|
||||
print('\n')
|
||||
print('1. ' + sentence)
|
||||
|
||||
# extraction des mots de la phrase de départ
|
||||
words = words_extract( sentence )
|
||||
|
||||
# 1.2 on ajoute à all_words tous les mots trouver
|
||||
print(words)
|
||||
if words not in all_words:
|
||||
all_words.extend(words)
|
||||
print(all_words)
|
||||
|
||||
# 1.3 - génération du nombre pour le choix du mot
|
||||
ran_num = rand_words( words )
|
||||
print(ran_num)
|
||||
|
||||
# ------------------- #
|
||||
# niveau 2
|
||||
# ------------------- #
|
||||
|
||||
# 2 - on va chercher une phrase à partir du mot sélectionné
|
||||
sentence2 = words_gen(words, ran_num)
|
||||
sentence2.encode('utf-8')
|
||||
sentence2.replace("(...$)/g", "")
|
||||
|
||||
print('\t 2. ' + sentence2)
|
||||
|
||||
|
||||
# extraction des mots de la phrase 2
|
||||
words = words_extract( sentence2 )
|
||||
|
||||
# 2.2
|
||||
print(words)
|
||||
if words not in all_words:
|
||||
all_words.extend(words)
|
||||
print(all_words)
|
||||
|
||||
# 2.3 - génération du nombre pour le choix du mot
|
||||
ran_num = rand_words( words )
|
||||
print(ran_num)
|
||||
|
||||
# ------------------- #
|
||||
# niveau 3
|
||||
# ------------------- #
|
||||
|
||||
# 3 - on va chercher une phrase à partir du mot sélectionné
|
||||
sentence3 = srch_engine(words, ran_num)
|
||||
sentence3.encode('utf-8')
|
||||
sentence3.replace("(...$)/g", "")
|
||||
|
||||
print('\t\t 3. ' + sentence3)
|
||||
|
||||
# extraction des mots de la phrase 2
|
||||
words = words_extract( sentence3 )
|
||||
|
||||
# 3.2
|
||||
print(words)
|
||||
if words not in all_words:
|
||||
all_words.extend(words)
|
||||
print(all_words)
|
||||
|
||||
# 3.3 - génération du nombre pour le choix du mot
|
||||
ran_num = rand_words( words )
|
||||
print(ran_num)
|
||||
|
||||
# ------------------- #
|
||||
# niveau 4
|
||||
# ------------------- #
|
||||
|
||||
# 4 - on va chercher une phrase à partir du mot sélectionné
|
||||
sentence4 = words_gen(words, ran_num)
|
||||
sentence4.encode('utf-8')
|
||||
sentence4.replace("(...$)/g", "")
|
||||
|
||||
print('\t\t\t 4. ' + sentence4)
|
||||
|
||||
# extraction des mots de la phrase 2
|
||||
words = words_extract( sentence4 )
|
||||
|
||||
# 4.2
|
||||
print(words)
|
||||
if words not in all_words:
|
||||
all_words.extend(words)
|
||||
print(all_words)
|
||||
|
||||
# 4.3 - génération du nombre pour le choix du mot
|
||||
ran_num = rand_words( words )
|
||||
print(ran_num)
|
||||
|
||||
# ------------------- #
|
||||
# niveau 5
|
||||
# ------------------- #
|
||||
|
||||
# 5 - on va chercher une phrase à partir du mot sélectionné
|
||||
sentence5 = srch_engine(words, ran_num)
|
||||
sentence5.encode('utf-8')
|
||||
sentence5.replace("(...$)/g", "")
|
||||
|
||||
print('\t\t\t\t 5. ' + sentence5)
|
||||
|
||||
# extraction des mots de la phrase 2
|
||||
words = words_extract( sentence5 )
|
||||
|
||||
# 5.2
|
||||
print(words)
|
||||
if words not in all_words:
|
||||
all_words.extend(words)
|
||||
print(all_words)
|
||||
|
||||
# 5.3 - génération du nombre pour le choix du mot
|
||||
ran_num = rand_words( words )
|
||||
print(ran_num)
|
||||
|
||||
# ------------------- #
|
||||
# niveau 6
|
||||
# ------------------- #
|
||||
|
||||
# 6 - on va chercher une phrase à partir du mot sélectionné
|
||||
sentence6 = words_gen(words, ran_num)
|
||||
sentence6.encode('utf-8')
|
||||
sentence6.replace("(...$)/g", "")
|
||||
|
||||
print('\t\t\t\t\t 6. ' + sentence6)
|
||||
|
||||
# extraction des mots de la phrase 2
|
||||
words = words_extract( sentence6 )
|
||||
|
||||
# 6.2
|
||||
print(words)
|
||||
if words not in all_words:
|
||||
all_words.extend(words)
|
||||
print(all_words)
|
||||
|
||||
# 6.3 - génération du nombre pour le choix du mot
|
||||
ran_num = rand_words( words )
|
||||
print(ran_num)
|
||||
|
||||
|
||||
# ------------------- #
|
||||
# écriture du fichier
|
||||
# ------------------- #
|
||||
|
||||
translator = Translator()
|
||||
translations = translator.translate([sentence, sentence2, sentence3, sentence4, sentence5, sentence6], dest='fr')
|
||||
|
||||
i = 0
|
||||
|
||||
for translation in translations:
|
||||
i += 1
|
||||
#print(i)
|
||||
# on ouvre un fichier
|
||||
file = open('ola#5doc/jeremie_nuel/sources/output.txt', 'a')
|
||||
# on l'écrit
|
||||
file.write('\n\n' + str(i) + '. ' + translation.text )
|
||||
|
||||
file.write('\n\n')
|
||||
file.write('--------------- + ----------------')
|
||||
file.close()
|
||||
print('fichier écrit')
|
||||
Reference in New Issue
Block a user