python实现获取文件列表中每个文件关键字
2014-12-18 11:59
302 查看
功能描述:
获取某个路径下的所有文件,提取出每个文件中出现频率最高的前300个字。保存在数据库当中。
前提,你需要配置好nltk
转载注明出处:http://blog.csdn.net/chichoxian/article/details/42003603
获取某个路径下的所有文件,提取出每个文件中出现频率最高的前300个字。保存在数据库当中。
前提,你需要配置好nltk
#!/usr/bin/python #coding=utf-8 ''' function : This script will create a database named mydb then abstract keywords of files of privacy police. author : Chicho date : 2014/7/28 running : python key_extract.py -d path_of_file ''' import sys,getopt import nltk import MySQLdb from nltk.corpus import PlaintextCorpusReader corpus_root = "" if __name__ == '__main__': opts,args = getopt.getopt(sys.argv[1:], "d:h","directory=help") #get the directory for op,value in opts: if op in ("-d", "--directory"): corpus_root = value #actually, the above method to get a directory is a little complicated,you can #do like this ''' the input include you path and use sys.argv to get the path ''' ''' running : python key_extract.py you path_of_file corpus_root = sys.argv[1] ''' # corpus_root is the directory of files of privacy policy, all of the are html files filelists = PlaintextCorpusReader(corpus_root, '.*') #get the files' list files = filelists.fileids() #connect the database conn = MySQLdb.connect(host = 'your_personal_host_ip_address', user = 'rusername', port =your_port, passwd = 'U_password') #get the cursor curs = conn.cursor() conn.set_character_set('utf8') curs.execute('set names utf8') curs.execute('SET CHARACTER SET utf8;') curs.execute('SET character_set_connection=utf8;') ''' conn.text_factory=lambda x: unicode(x, 'utf8', "ignore") #conn.text_factory=str ''' # create a database named mydb ''' try: curs.execute("create database mydb") except Exception,e: print e ''' conn.select_db('mydb') try: for i in range(300): sql = "alter table filekeywords add " + "key" + str(i) + " varchar(45)" curs.execute(sql) except Exception,e: print e i = 0 for privacyfile in files: #f = open(privacyfile,'r', encoding= 'utf-8') sql = "insert into filekeywords set id =" + str(i) curs.execute(sql) sql = "update filekeywords set name =" + "'" + privacyfile + "' where id= " + str(i) curs.execute(sql) # get the words in privacy policy wordlist = [w for w in filelists.words(privacyfile) if w.isalpha() and len(w)>2] # get the keywords fdist = nltk.FreqDist(wordlist) vol = fdist.keys() key_num = len(vol) if key_num > 300: key_num = 300 for j in range(key_num): sql = "update filekeywords set " + "key" + str(j) + "=" + "'" + vol[j] + "' where id=" + str(i) curs.execute(sql) i = i + 1 conn.commit() curs.close() conn.close()
转载注明出处:http://blog.csdn.net/chichoxian/article/details/42003603
相关文章推荐
- python实现获取文件列表中每一个文件keyword
- python 获取文件列表(或是目录例表)
- python实现批量获取指定文件夹下的所有文件的厂商信息
- Python获取文件夹下所有的文件路径列表
- Python实现通过文件路径获取文件hash值的方法
- python实现字符串全排列(注:每个字符写在了列表里)
- 获取一个索引文件频率最高的Term(可实现热点关键字的一个思路之一)
- [脚本无敌2]python获取cocos 2dx项目文件列表
- 从指定文件夹下获取文件路径列表的实现方法
- python实现分析apache和nginx日志文件并输出访客ip列表的方法
- python实现文件夹名/文件名/文件内容的关键字替换
- python中如何获取文件列表
- python 获取指定文件列表
- c# 使用FtpWebRequest,FtpWebResponse ,Stream 实现获取文件列表及下载
- python 获取文件列表(或是目录例表)
- Python 获取目录下的文件列表与内容
- 用python实现的可以拷贝或剪切一个文件列表中的所有文件
- python实现批量获取指定文件夹下的所有文件的厂商信息
- WPF下采用C#实现FTP上传下载获取文件列表
- 使用python的yield实现任务调度.给定一个任务列表,每个任务轮流切换执行,类似于切片