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