Tout d'abord, récupérez les informations textuelles du discours sur la page de l'Université de Stanford et créez un fichier texte sans paragraphes. https://news.stanford.edu/2005/06/14/jobs-061505/
f= open("stevejobs.txt","r")
contents =f.read()
Vous pouvez créer une "(presque) liste de mots" list_word
en procédant comme suit:
list_word = contents.split(" ")
Vous pouvez l'utiliser pour compter le nombre de fois qu'un mot particulier, par exemple le, apparaît.
count = 0
for w in list_word:
if (w == "the"):
count = count + 1
print(count)
# 91
La raison pour laquelle je ne pourrais pas dire «liste de mots» ci-dessus est que cette méthode entraîne l'attachement de «.» Etc. au mot à la fin de la phrase. Regardons un exemple.
for w in list_word:
if ('.' in w ):
print(w)
print('----')
#
"""
world.
----
college.
----
graduation.
----
life.
----
Ci-dessous, beaucoup continue.
"""
Vous pouvez créer une «liste d'instructions (comme)» «list_sentence» en procédant comme suit:
list_sentence = contents.split(". ")
On ne peut pas l'appeler une "liste de phrases" car cette méthode ne se termine pas par un "." Et ne reconnaît pas des choses comme "!", "?", ":" Lorsque la phrase se brise. Regardons un exemple.
for l in list_sentence:
if ("!" in l):
print(l)
print('----')
#Puisque rien n'est affiché, "!Il ne semble y avoir aucune phrase contenant "".
for l in list_sentence:
if ("?" in l):
print(l)
print('----')
#Le résultat de l'exécution est le suivant.
"""
So why did I drop out? It started before I was born
----
So my parents, who were on a waiting list, got a call in the middle of the night asking: “We have an unexpected baby boy; do you want him?” They said: “Of course.” My biological mother later found out that my mother had never graduated from college and that my father had never graduated from high school
----
How can you get fired from a company you started? Well, as Apple grew we hired someone who I thought was very talented to run the company with me, and for the first year or so things went well
----
When I was 17, I read a quote that went something like: “If you live each day as if it was your last, someday you’ll most certainly be right.” It made an impression on me, and since then, for the past 33 years, I have looked in the mirror every morning and asked myself: “If today were the last day of my life, would I want to do what I am about to do today?” And whenever the answer has been “No” for too many days in a row, I know I need to change something
----
"""
Enfin, regardons la fréquence des lettres.
Tout d'abord, réduisez toutes les lettres.
contents_lower = contents.lower()
def count_char(char_target):
count = 0
for i in range(len(contents_lower)):
s = contents_lower[i]
if (s==char_target):
count = count+1
return count
abc = "a b c d e f g h i j k l m n o p q r s t u v w x y z"
list_abc = abc.split()
#list_abc = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
import numpy
list_count_letter = numpy.zeros(26)
for i in range(26):
letter = list_abc[i]
N = count_char(letter)
list_count_letter[i] = N
print(letter, N)
#
"""
a 772
b 132
c 219
d 417
e 1077
f 206
g 207
h 440
i 642
j 9
k 65
l 402
m 216
n 595
o 772
p 183
q 4
r 499
s 510
t 926
u 283
v 115
w 245
x 16
y 257
z 4
"""
Pour faciliter la visualisation, si vous essayez de trier, ce sera comme suit.
Cela ressemble à ceci, et étonnamment "u" est à la 12ème place.
index_sort = numpy.argsort(list_count_letter)
for i in range(26):
k = index_sort[i]
letter = list_abc[k]
N = list_count_letter[k]
print(letter, N)
"""
z 4.0
q 4.0
j 9.0
x 16.0
k 65.0
v 115.0
b 132.0
p 183.0
f 206.0
g 207.0
m 216.0
c 219.0
w 245.0
y 257.0
u 283.0
l 402.0
d 417.0
h 440.0
r 499.0
s 510.0
n 595.0
i 642.0
o 772.0
a 772.0
t 926.0
e 1077.0
"""
Recommended Posts