lolamontes69

Ch4 Ex1-Programming Collective Intelligence

Jun 24th, 2013
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.64 KB | None | 0 0
  1. from string import punctuation
  2.  
  3. def separatewords(text):
  4.     punctuation1 = punctuation.replace('+','').replace('$','').replace('#','')
  5.     res = []
  6.     result = text.replace('\n',' ').replace('\t',' ').replace('\r',' ').replace('\x0b',' ').replace('\x0c',' ').lower()
  7.     result1 = result.split(' ')
  8.     # Passes reiterates over the cleaned text to get, for example, a ? within quotes
  9.     for passes in range(2):
  10.         for a in range(len(result1)):
  11.             for punk in punctuation1:
  12.                 if result1[a] == punk:
  13.                     result1[a] = ""
  14.                 if result1[a][-1:] == punk:
  15.                     result1[a] = result1[a].replace(punk, ' ')[:-1]
  16.                 if result1[a][:0] in punk:
  17.                     result1[a] = result1[a].replace(punk, '')[0:]
  18.     for b in result1:
  19.         if b != '': res.append(b)
  20.     return res
  21.  
  22.  
  23. """   COLLECTIVE INTELLIGENCE - CHAPTER 4 EXERCISE 1
  24.  
  25. 1. Word separation. The separatewords() method currently considers any non-alphanumeric character to be a separator, meaning it will not properly index entries like "C++," "$20," "Ph.D.," or "617-555-1212." What is a better way to separate words? Does using whitespace as a separator work? Write a better word separation function.
  26.  
  27. Note: There is no real way of catching all exceptions
  28.      example  chillin'  or  'chilian'
  29.      example  C++  or 1+ 1 = 3
  30.  
  31.      There are no perfect solutions with humans especially where spoken languages are concerned.
  32.      The method described above was designed to index the entries in the exercise,
  33.      but there's unicode to take into account, and french, spanish, japanese........
  34. """
Advertisement
Add Comment
Please, Sign In to add comment