-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain
168 lines (143 loc) · 4.34 KB
/
Main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!usr/bin/env python
INITIALISATION : import des packages nécessaires
#python3 ml.py
import warnings
warnings.filterwarnings("ignore", category=FutureWarning)
import seaborn as sns
import matplotlib.pyplot as plt
import sys
import sklearn
import pandas as pd
import numpy as np
#Double-cliquez (ou appuyez sur Entrée) pour modifier
#Lecture des données test
from google.colab import drive
drive.mount('/content/gdrive/')
my_local_drive='/content/gdrive/My Drive/ProjetMachineLearning'
#my_local_drive='/content/gdrive/Partagés avec moi/ProjetMachineLearning'
# Ajout du path pour les librairies, fonctions et données
sys.path.append(my_local_drive)
# Se positionner sur le répertoire associé
%cd $my_local_drive
#IMPORTATION : récupération depuis le Drive des données à traiter
#Lecture des données test
rawtest=pd.read_csv('HAI817_Projet_test.csv', sep=',')
test=rawtest
test=test[['title','text','our rating']]
#Lecture des données train
rawtrain=pd.read_csv('HAI817_Projet_train.csv', sep=',')
train=rawtrain
train=train[['title','text','our rating']]
#PRE-TRAITEMENT : Tokenisation ou conversion String -> Tableau (délimiteur : espaces)
#!python -m spacy download en_core_web_sm
# ^^^ Cette ligne est à décommenter si spacy n'a pas déjà été installé
import spacy
nlp = spacy.load("en_core_web_sm")
for i in range(len(test)):
tokens = nlp(test.loc[i, "title"])
test.loc[i, "title"] = tokens
for i in range(len(test)):
tokens = nlp(test.loc[i, "text"])
test.loc[i, "text"] = tokens
print(test)
train['title'] = train['title'].astype(str)
for i in range(len(train)):
tokens = nlp(train.loc[i, "title"])
test.loc[i, "title"] = tokens
for i in range(len(train)):
tokens = nlp(train.loc[i, "text"])
test.loc[i, "text"] = tokens
#print(len(tokens)) Affiche le nb de mots dans le texte
print(train)
#pip install nltk
#import nltk
#nltk.download('stopwords')
#nltk.corpus.stopwords.words('english')
#from nltk.tokenize import word_tokenize
# la liste des tokens de la première phrase
#tokens = word_tokenize(phrases[0])
#print(tokens)
#nlp = spacy.load(str(test.loc[i, "title"]))
#tokencontainer = nlp(str(test.loc[i, "title"]))
#for token in tokencontainer:
# print(token.text)
from spacy.tokenizer import Tokenizer
from spacy.lang.en import English
nlp = English()
# Creating a blank Tokenizer with just the English vocab
tokenizer = Tokenizer(nlp.vocab)
tokens = tokenizer(str(test.loc[i, "title"]))
print("Blank tokenizer",end=" : ")
for token in tokens:
print(token,end=', ')
# Construction 2
from spacy.lang.en import English
nlp = English()
# Creating a Tokenizer with the default settings for English
tokenizer = nlp.tokenizer
tokens = tokenizer(str(test.loc[i, "title"]))
print("\nDefault tokenizer",end=' : ')
for token in tokens:
print(token,end=', ')
#for i in range(len(test)):
# test.loc[i, "title"] = str(test.loc[i, "title"]).split()
# test.loc[i, "text"] = str(test.loc[i, "text"]).split()
#for i in range(len(train)):
# train.loc[i, "title"] = str(train.loc[i, "title"]).split()
# train.loc[i, "text"] = str(train.loc[i, "text"]).split()
#print(test)
#print(train)
#PRE-TRAITEMENT : suppression des StopWords
#LIST OF STOPWORDS
stopwords = [
#determiners
"The ", " the ", "A ", " a ", "An ", " an ", "Some ", " some ", "Of ", " of ",
"This ", " this ", "That ", " that ", "These ", " these", "Those ", " those ",
"My ", " my ", "Your ", " your ", "Yours ", " yours", "His ", " his ", "Her ", " her ", "Him ", " him", "Our ", " our ", "Their ", " their ",
"Which ", " which ", " Whom ", " whom",
"Another ", " another",
#conjunctions
"For ", " for ", "Nor ", " nor ", "But ", " but ", "Or ", " or ", "Yet ", " yet ", "So ", " so ",
#prepositions
"In ", "in ", "Under ", "under ", "Towards ", "towards ", "Before ", "before "
]
#most used verbs
#
# "be
# "have
# "do
# "say
# "get
# "make
# "go
# "know
# "take
# "see
# "come
# "think
# "look
# "want
# "give
# "use
# "find
# "tell
# "ask
# "work
# "seem
# "feel
# "try
# "leave
# "call
#¬ formes négatives
for i in range(len(test)):
for w in range(len(stopwords)):
test.loc[i, "text"] = test.loc[i, "text"].replace(stopwords[w], " ")
for i in range(len(train)):
for w in range(len(stopwords)):
train.loc[i, "text"] = train.loc[i, "text"].replace(stopwords[w], " ")
#AFFICHAGE : retourne les données une fois traitées
print(test)
print(train)
#test.loc[i, "text"]
s = "Un essai"
print(len(s))