-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
CaidevOficial
committed
Nov 22, 2020
1 parent
600b538
commit de540fc
Showing
2 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Encontrar la cantidad de ocurrencias de la palabras | ||
# "Trump" y "the" en el texto de la noticia. | ||
|
||
nametext = 'noticia.txt' | ||
|
||
def openText(nameText): | ||
f = open(nameText, encoding='utf-8') | ||
texto = f.read() | ||
return texto | ||
|
||
def replaceCharacters(texto): | ||
reemplazar = ['’s','—',',','.','\n','\ufeff'] | ||
for indeseado in reemplazar: | ||
texto = texto.replace(indeseado,' ') | ||
return texto | ||
|
||
def dictionaryOcurrences(palabras): | ||
ocurrencias = {} # Hago diccionario para contar ocurrencias | ||
for palabra in palabras: | ||
palabra = palabra.lower() | ||
if palabra != '' and (palabra=='trump' or palabra=='the'): | ||
if palabra not in ocurrencias: | ||
ocurrencias[palabra] = 1 | ||
else: | ||
ocurrencias[palabra] += 1 | ||
print(ocurrencias) | ||
|
||
def desafio4A(nametext): | ||
texto = openText(nametext) | ||
texto = replaceCharacters(texto) | ||
# Convierto texto en lista | ||
palabras = texto.split(' ') | ||
dictionaryOcurrences(palabras) | ||
|
||
# Test | ||
desafio4A(nametext) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# Encontrar la palabra con mayor numero de ocurrencias | ||
# en el texto de la noticia. | ||
link = 'noticia.txt' | ||
|
||
def openText(link): | ||
f = open(link, encoding='utf-8') | ||
texto = f.read() | ||
return texto | ||
|
||
def replaceCharacters(texto): | ||
reemplazar = ['’s','—',',','.','\n','\ufeff'] | ||
for indeseado in reemplazar: | ||
texto = texto.replace(indeseado,' ') | ||
return texto | ||
|
||
def dictionaryOcurrences(palabras): | ||
# Hago diccionario para contar ocurrencias | ||
ocurrencias = {} | ||
for palabra in palabras: | ||
palabra = palabra.lower() | ||
if palabra != '' and (palabra=='trump' or palabra=='the'): | ||
if palabra not in ocurrencias: | ||
ocurrencias[palabra] = 1 | ||
else: | ||
ocurrencias[palabra] += 1 | ||
return ocurrencias | ||
|
||
def biggerOcurrence(ocurrencias): | ||
maximaCantidad = 0 | ||
mayorOcurrencia = {} | ||
|
||
for palabra in ocurrencias.keys(): | ||
if ocurrencias[palabra] > maximaCantidad: | ||
mayorOcurrencia = palabra | ||
maximaCantidad = ocurrencias[palabra] | ||
print(mayorOcurrencia) | ||
|
||
def desafio4Challenge(link): | ||
ocurrencias = {} | ||
texto = openText(link) | ||
texto = replaceCharacters(texto) | ||
# Convierto texto en lista | ||
palabras = texto.split(' ') | ||
ocurrencias = dictionaryOcurrences(palabras) | ||
biggerOcurrence(ocurrencias) | ||
|
||
# Test | ||
desafio4Challenge(link) |