Advertisement
Guest User

Untitled

a guest
Jun 25th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. import sqlite3
  2. import os.path
  3. import os
  4.  
  5. os.chdir('C:/Users/Trevor/Desktop')
  6. conn = sqlite3.connect("a.db")
  7. cursor = conn.cursor()
  8. all_words = []
  9.  
  10. for file in os.listdir('gutenberg'):
  11. print('processing {}'.format(file))
  12. with open(os.path.join('gutenberg', file), 'r', encoding='utf-8') as file_in:
  13. all_words.extend(file_in.read().split())
  14.  
  15. print('{} words'.format(len(all_words)))
  16. freq_dict = {}
  17. for word in all_words:
  18. if word in freq_dict:
  19. freq_dict[word] += 1
  20. else:
  21. freq_dict[word] = 1
  22.  
  23. for word, freq in freq_dict.items():
  24. cursor.execute('INSERT INTO X VALUES(?, ?)', (word, freq))
  25. conn.commit()
  26. conn.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement