Files
ola5doc/output/sources/redux_fichier2.py
T

46 lines
1.4 KiB
Python

#!/usr/bin/env python
# coding: utf8
#pour installer nltk stopWords tout est là : http://www.nltk.org/data.html
import re
from nltk.tokenize import sent_tokenize, word_tokenize
from nltk.corpus import stopwords
with open("mirabeau.txt", "r") as source:
texte = source.read()
#print("texte or", texte)
#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:
print("no stopword", w)
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))