Advertisement
Guest User

Untitled

a guest
May 23rd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. import random
  2. import re
  3. from collections import Counter
  4. lista_alteracoes={}
  5. flag=0
  6.  
  7. def tp3():
  8. lista_palavras=[];
  9. pal=""
  10. while(pal!="FIMFIM"):
  11. pal=raw_input()
  12. lista_palavras.append(pal)
  13. for elem in lista_palavras:
  14. for item in lista_alteracoes:
  15. if elem in item:
  16. flag=1
  17. #propor como está
  18. #else trocar flag
  19. #print
  20. if flag==0:
  21. #else tem de ver no dicionário as correções e propor alteracao em d
  22. correcao=corrector(elem)
  23. #ocorrencias estao no dicionario
  24. dici_alteracoes.append((elem,correcao,ocorrencias))
  25. flag=0
  26. return
  27.  
  28. def words(text): return re.findall(r'\w+', text.lower())
  29.  
  30. WORDS = Counter(words(open('big.txt').read()))
  31. #Alterar, ficheiro descarregado e alterar leitura
  32.  
  33. def P(word, N=sum(WORDS.values())):
  34. #alterar vem de ficheiro texto, ler e capturar num dicionario????
  35. "Probability of `word`."
  36. return WORDS[word] / N
  37.  
  38. def corrector(palavra):
  39. return max(candidatos(palavra), chave=P)
  40.  
  41. def candidatos(palavra):
  42. return (known([palavra]) or known(edits1(palavra)) or known(edits2(palavra)) or [palavra])
  43.  
  44. def known(words):
  45. "The subset of `words` that appear in the dictionary of WORDS."
  46. return set(w for w in words if w in WORDS)
  47.  
  48. def edits1(word):
  49. "All edits that are one edit away from `word`."
  50. letters = 'abcdefghijklmnopqrstuvwxyz'
  51. splits = [(word[:i], word[i:]) for i in range(len(word) + 1)]
  52. deletes = [L + R[1:] for L, R in splits if R]
  53. transposes = [L + R[1] + R[0] + R[2:] for L, R in splits if len(R)>1]
  54. replaces = [L + c + R[1:] for L, R in splits if R for c in letters]
  55. inserts = [L + c + R for L, R in splits for c in letters]
  56. return set(deletes + transposes + replaces + inserts)
  57.  
  58. def edits2(word):
  59. "All edits that are two edits away from `word`."
  60. return (e2 for e1 in edits1(word) for e2 in edits1(e1))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement