Posted By

god_of_nothing on 03/07/11


Tagged

scripting


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

saman
doryexmachina


word counter


 / Published in: Python
 

with given key_word searches through a text or web file and indicates number of times that the word is repeated.

  1. # BY: AMIR NAGHAVI
  2. # <><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
  3. # <><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
  4.  
  5. """
  6. this two programm go through lines among text or html files
  7. and count number of repeating the given word
  8. """
  9. class find(object):
  10. def __init__(self,path,word):
  11. self.p=path #-----text file path
  12. self.w=word #-----the word to look for repeating
  13. def start(self):
  14. f=open(self.p)
  15.  
  16. chrcount=0 # character counter ;indicates and separates words
  17. numofrep=0 # number of word that would repeat
  18.  
  19. length=len(self.w)
  20.  
  21. for line in f:
  22. lineflag=False #--to printing line once if it has the give word more than once <--
  23. for j in line: # \
  24. if j==self.w[chrcount] and chrcount<=length: # \
  25. chrcount+=1 # \
  26. if chrcount==length: #--finding the word |
  27. chrcount=0 # |
  28. numofrep+=1 # |
  29. if lineflag!=True: # if in line are more than one "word" print line once-|
  30. print line
  31. lineflag=True
  32. else:
  33. chrcount=0
  34. print 'Number of repeat: ',numofrep
  35.  
  36.  
  37.  
  38.  
  39. # ------------------------------- searvhing through web pages
  40. class word_counter_in_web(find):
  41. def __init__(self,add,word):
  42. self.add=add
  43. self.w=word
  44. def start(self):
  45. from urllib import urlopen
  46. #url=urlopen(self.add)
  47.  
  48. length=len(self.w)
  49. chrcount=0
  50. numofrep=0
  51.  
  52. for line in self.add:
  53. lineflag=False
  54. for j in line:
  55. if j==self.w[chrcount] and chrcount<=length:
  56. chrcount+=1
  57. if chrcount==length:
  58. numofrep+=1
  59. chrcount=0
  60. if lineflag!=True:
  61. lineflag=True
  62.  
  63. #print 'repeat number for %s is %d' %(self.w,numofrep)
  64. return numofrep
  65. # -------------------------------------
  66. # RUN:
  67. if __name__=="__main__":
  68. find('D:\cpuz-readme.txt','cpu').start()
  69. #print word_counter_in_web('http://www.python.org','python').start()

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: god_of_nothing on March 7, 2011

please run and if you want study code and give your opinion on me.

You need to login to post a comment.