79 lines
2.4 KiB
Python
79 lines
2.4 KiB
Python
#!/usr/bin/env python
|
|
# coding: utf8
|
|
#pour installer nltk stopWords tout est là : http://www.nltk.org/data.html
|
|
#finalement je n'utilise pas nltk stopWords qui est lacunaire mais ma propre liste
|
|
import re
|
|
#from nltk.tokenize import sent_tokenize, word_tokenize
|
|
#from nltk.corpus import stopwords
|
|
POEME_A_REDUIRE = "mirabeau.txt"
|
|
|
|
def load_stopwords():
|
|
with open("stopwords-fr.txt", "r") as sw:
|
|
text_stopwords = sw.read()
|
|
text_stopwords = text_stopwords[:-1] # pour enlever le retour charriot à la fin
|
|
return text_stopwords.split("\n")
|
|
#print("stopwords_list : ", load_stopwords() )
|
|
#mots_interdits = load_stopwords()
|
|
|
|
def load_texte_a_reduire():
|
|
with open(POEME_A_REDUIRE, "r") as source:
|
|
texte_original = source.read()
|
|
return texte_original
|
|
#print("texte original :", texte_original)
|
|
|
|
def verses_list(texte_original): # sépare les vers sur le retour charriot
|
|
return texte_original.split("\n")
|
|
|
|
# verses = verses_list(texte_original)
|
|
|
|
def words_list(verses):
|
|
for verse in verses:
|
|
liste_mots = verse.split(" ")
|
|
return liste_mots
|
|
|
|
|
|
|
|
|
|
#print("texte original :", texte_original)
|
|
#enlever les traits d'union et les apostrophes
|
|
# texte = texte.replace("'", " ")
|
|
# texte = texte.replace("-", " ")
|
|
# #print(texte)
|
|
# liste_phrase = texte.lower().split(" ")
|
|
# print("liste des mots originaux séparés:", liste_phrase)
|
|
#
|
|
# liste_reduit = []
|
|
# #liste_phrase = str.lower(texte)
|
|
# stopWords = set(stopwords.words('french'))
|
|
# for w in liste_phrase:
|
|
# if w not in stopWords:
|
|
# liste_reduit.append(w)
|
|
# #print("liste des mots réduite :", liste_reduit)
|
|
#
|
|
# #ré-introduire les majuscules en debut de vers
|
|
# for i in range( len ( liste_reduit ) ):
|
|
# mot_split = liste_reduit[i].split("\n")
|
|
# #print("mot_split", mot_split)
|
|
# if ( len(mot_split) == 2):
|
|
# mot_split[1] = mot_split[1].capitalize()
|
|
# liste_reduit[i] = mot_split[0] + "\n" + mot_split[1]
|
|
# if ( i > 0 and liste_reduit[i-1] == '' ):
|
|
# liste_reduit[i] = liste_reduit[i].capitalize()
|
|
#
|
|
# #if (elt.endswith("\n") and elt != " " ):
|
|
# #mot = elt.capitalize()
|
|
# poeme_reduit = " ".join(liste_reduit)
|
|
# #print("version réduite du poeme :", poeme_reduit)
|
|
#
|
|
# with open("mirabeau_reduit.txt", "w") as destination :
|
|
# destination.write(" ".join(liste_reduit))
|
|
|
|
|
|
|
|
#Appel des fonctions
|
|
texte_original = load_texte_a_reduire()
|
|
mots_interdits = load_stopwords()
|
|
verses = verses_list(texte_original)
|
|
mots = words_list(verses)
|
|
print(mots)
|