Skip to content

Commit

Permalink
Mini Desafio 4 & Challenge.
Browse files Browse the repository at this point in the history
  • Loading branch information
CaidevOficial committed Nov 22, 2020
1 parent 600b538 commit de540fc
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
36 changes: 36 additions & 0 deletions Mini_Desafios/C3_Desafios/4_A.py
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)
48 changes: 48 additions & 0 deletions Mini_Desafios/C3_Desafios/4_A_Challenge.py
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)

0 comments on commit de540fc

Please sign in to comment.