python获取手机文件列表_python实现获取文件列表中每一个文件keyword

功能描写叙述:

获取某个路径下的全部文件,提取出每一个文件里出现频率最高的前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()

这个问题需要用到 iTextSharp 库来解决。首先,你需要安装该库并导入它。然后,你可以使用以下代码获取指定关键字的坐标位置信息: ```c# using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using iTextSharp.text.pdf; using iTextSharp.text.pdf.parser; namespace PdfKeywordCoordinates { class Program { static void Main(string[] args) { string filename = @"C:\example.pdf"; // pdf 文件路径 string keyword = "example keyword"; // 指定关键字 using (PdfReader reader = new PdfReader(filename)) { for (int page = 1; page <= reader.NumberOfPages; page++) { ITextExtractionStrategy strategy = new LocationTextExtractionStrategy(); string currentText = PdfTextExtractor.GetTextFromPage(reader, page, strategy); if (currentText.Contains(keyword)) { var kwLocation = new List<RectAndText>(); var renderFilter = new RenderFilter[1]; renderFilter[0] = new RegionTextRenderFilter(new Rectangle(0, 0, 1000, 1000)); var textExtractionStrategy = new FilteredTextRenderListener(new LocationTextExtractionStrategy(), renderFilter); PdfContentStreamProcessor processor = new PdfContentStreamProcessor(textExtractionStrategy); processor.ProcessContent(reader.GetPageContent(page)); kwLocation = ((LocationTextExtractionStrategy)textExtractionStrategy).GetLocations(); foreach (RectAndText rectAndText in kwLocation) { if (rectAndText.text.Contains(keyword)) { Console.WriteLine("Page: " + page + " X: " + rectAndText.rect.Left + " Y: " + rectAndText.rect.Bottom); } } } } } Console.ReadLine(); } } public class RectAndText { public iTextSharp.text.Rectangle rect; public String text; public RectAndText(iTextSharp.text.Rectangle rect, String text) { this.rect = rect; this.text = text; } } } ``` 这个代码将在指定的 PDF 文件查找指定的关键字,并输出该关键字在每一页的坐标位置信息。注意,这个代码是使用 C# 编写的,如果你使用的是 Python,你需要使用 Python 版本的 iTextSharp 库,并使用相应的语法来实现相同的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值