Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2010-10

Text scan in text files - (was: Full text scan - PDF files)

25 messages · 8 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Marcos Douglas
Marcos Douglas wrote 10 lines
WTF? http://lists.freepascal.org/lists/fpc-pascal/2010-October/027067.html Well... again: I need to do a full text scan in PDF files i.e. convert PDF in plain text, parser and save the informations. I saw the XPDF[1] lib but I would like a lib coded in Pascal... if is possible. What do you recommends to do that?

Ok Guys.
I already know how convert PDF files in Text files. To search strings in Text files, which lib in FPC do you recommends? I need speed and the program is a console program.

Thanks,
Marcos Douglas

Reply by email

Marcos Douglas
to Marcos Douglas
Marcos Douglas wrote 10 lines
> I need to do a full text scan in PDF files i.e. convert PDF in plain > text, parser and save the informations. > I saw the XPDF[1] lib but I would like a lib coded in Pascal... if is possible. > > What do you recommends to do that? Ok Guys. I already know how convert PDF files in Text files. To search strings in Text files, which lib in FPC do you recommends? I need speed and the program is a console program.

Somebody can help me please?
I need to search strings in Text files using just FPC. I'm using the xpdf (pdftotext.exe) to convert PDF files to Text files and searching some words in these files.
If is possible to search in PDF files is the better for me. But text files is ok too.

Thanks
Marcos Douglas

Reply by email

Marcos Douglas
to Alberto Narduzzi
Alberto Narduzzi wrote 6 lines
> Somebody can help me please? > I need to search strings in Text files using just FPC. how about reading every line and then using Pos() to see if some string is there?

I don't think this way is the fast way :(
I have many PDF files with several pages each.

Marcos Douglas

Reply by email

Martin Schreiber
to Marcos Douglas
Marcos Douglas wrote 12 lines
On Mon, Nov 1, 2010 at 9:34 AM, Alberto Narduzzi wrote: >> Somebody can help me please? >> I need to search strings in Text files using just FPC. > > how about reading every line and then using Pos() to see if some string > is there? I don't think this way is the fast way :( I have many PDF files with several pages each.

MSEgui ttextstream has search functions (lib/common/kernel/msestream.pas), I don't know if it is fast enough for your purpose.

Martin

Reply by email

Marcos Douglas
to Martin Schreiber
Martin Schreiber wrote 14 lines
On Monday, 1. November 2010 13.34:45 Marcos Douglas wrote: > On Mon, Nov 1, 2010 at 9:34 AM, Alberto Narduzzi > > wrote: > >> Somebody can help me please? > >> I need to search strings in Text files using just FPC. > > > > how about reading every line and then using Pos() to see if some string > > is there? > > I don't think this way is the fast way   :( > I have many PDF files with several pages each. > MSEgui ttextstream has search functions (lib/common/kernel/msestream.pas), I

Thanks Martin, I will see this file.

don't know if it is fast enough for your purpose.

I suppose your implementation always is fast enough ;)

Marcos Douglas

Reply by email

Marco van de Voort
to Marcos Douglas: On Mon, Nov 1, 2010 at 9:34 AM, Alberto Narduzzi I don't think this way is the fast way …

In our previous episode, Marcos Douglas said:

Show quoted text 10 lines
wrote: >> Somebody can help me please? >> I need to search strings in Text files using just FPC. > > how about reading every line and then using Pos() to see if some string is > there? > I don't think this way is the fast way :( I have many PDF files with several pages each.

You'll be surprised. I've done multi million line logfiles that way. A pdf2txt is infinitely slow compared with such processing.

Reply by email

Marcos Douglas
to Marco van de Voort
Marco van de Voort wrote 14 lines
In our previous episode, Marcos Douglas said: > wrote: > >> Somebody can help me please? > >> I need to search strings in Text files using just FPC. > > > > how about reading every line and then using Pos() to see if some string is > > there? > > > > I don't think this way is the fast way   :( > I have many PDF files with several pages each. You'll be surprised. I've done multi million line logfiles that way. A pdf2txt is infinitely slow compared with such processing.

Ok, but I need to work with pure text. The PDF files are "compiled".

Marcos Douglas

Reply by email

Tomas Hajny
to Marco van de Voort: In our previous episode, Marcos Douglas said: You'll be surprised. I've done multi…
Marco van de Voort wrote 15 lines
In our previous episode, Marcos Douglas said: > wrote: > >> Somebody can help me please? > >> I need to search strings in Text files using just FPC. > > > > how about reading every line and then using Pos() to see if some > string is > > there? > > > > I don't think this way is the fast way :( > I have many PDF files with several pages each. You'll be surprised. I've done multi million line logfiles that way. A pdf2txt is infinitely slow compared with such processing.

Well, there at least two gotchas there. First, it's better to use a reasonable (= large enough) buffer size. Second, the simplest approach implying reading line by line and searching using Pos() obviously isn't sufficient for searching across line breaks, i.e. you either need to handle that yourself, or use some unit providing such functionality.

Tomas

Reply by email

Marcos Douglas
to Tomas Hajny
Tomas Hajny wrote 22 lines
On Mon, November 1, 2010 19:10, Marco van de Voort wrote: > In our previous episode, Marcos Douglas said: >> wrote: >> >> Somebody can help me please? >> >> I need to search strings in Text files using just FPC. >> > >> > how about reading every line and then using Pos() to see if some >> string is >> > there? >> > >> >> I don't think this way is the fast way   :( >> I have many PDF files with several pages each. > > You'll be surprised. I've done multi million line logfiles that way. A > pdf2txt is infinitely slow compared with such processing. Well, there at least two gotchas there. First, it's better to use a reasonable (= large enough) buffer size. Second, the simplest approach implying reading line by line and searching using Pos() obviously isn't sufficient for searching across line breaks, i.e. you either need to handle that yourself, or use some unit providing such functionality.

Which unit do you recommends?

Marcos Douglas

Reply by email

Tomas Hajny
to Marcos Douglas: Ok, but I need to work with pure text. The PDF files are "compiled". Marcos Douglas
Marcos Douglas wrote 19 lines
On Mon, Nov 1, 2010 at 3:10 PM, Marco van de Voort wrote: > In our previous episode, Marcos Douglas said: >> wrote: >> >> Somebody can help me please? >> >> I need to search strings in Text files using just FPC. >> > >> > how about reading every line and then using Pos() to see if some >> string is >> > there? >> > >> >> I don't think this way is the fast way   :( >> I have many PDF files with several pages each. > > You'll be surprised. I've done multi million line logfiles that way. A > pdf2txt is infinitely slow compared with such processing. Ok, but I need to work with pure text. The PDF files are "compiled".

Sorry, I'm confused now. Do you want to search for text strings within PDF files or within regular text files?

Tomas

Reply by email

Marcos Douglas
to Tomas Hajny
Tomas Hajny wrote 23 lines
On Mon, November 1, 2010 19:20, Marcos Douglas wrote: > On Mon, Nov 1, 2010 at 3:10 PM, Marco van de Voort > wrote: >> In our previous episode, Marcos Douglas said: >>> wrote: >>> >> Somebody can help me please? >>> >> I need to search strings in Text files using just FPC. >>> > >>> > how about reading every line and then using Pos() to see if some >>> string is >>> > there? >>> > >>> >>> I don't think this way is the fast way   :( >>> I have many PDF files with several pages each. >> >> You'll be surprised. I've done multi million line logfiles that way. A >> pdf2txt is infinitely slow compared with such processing. > > Ok, but I need to work with pure text. The PDF files are "compiled". Sorry, I'm confused now. Do you want to search for text strings within PDF files or within regular text files?

I need to search for text strings in PDF files... but I don´t know do that, so I use the pdf2txt[1] to convert in pure text. If there is a unit that can search for text within a PDF file, would be the best solution for me.

[1] http://www.foolabs.com/xpdf/home.html

Marcos Douglas

Reply by email

Tomas Hajny
to Marcos Douglas
Marcos Douglas wrote 30 lines
On Mon, Nov 1, 2010 at 3:36 PM, Tomas Hajny wrote: > On Mon, November 1, 2010 19:20, Marcos Douglas wrote: >> On Mon, Nov 1, 2010 at 3:10 PM, Marco van de Voort >> wrote: >>> In our previous episode, Marcos Douglas said: >>>> wrote: >>>> >> Somebody can help me please? >>>> >> I need to search strings in Text files using just FPC. >>>> > >>>> > how about reading every line and then using Pos() to see if some >>>> string is >>>> > there? >>>> > >>>> >>>> I don't think this way is the fast way   :( >>>> I have many PDF files with several pages each. >>> >>> You'll be surprised. I've done multi million line logfiles that way. A >>> pdf2txt is infinitely slow compared with such processing. >> >> Ok, but I need to work with pure text. The PDF files are "compiled". > > Sorry, I'm confused now. Do you want to search for text strings within > PDF > files or within regular text files? I need to search for text strings in PDF files... but I don´t know do that, so I use the pdf2txt[1] to convert in pure text. If there is a unit that can search for text within a PDF file, would be the best solution for me.

I see, but that means that Marco's comment applies - conversion of PDF files to pure text will probably require more time than finding your string within the (converted) text file.

Tomas

Reply by email

Marcos Douglas
to Tomas Hajny
Tomas Hajny wrote 35 lines
On Mon, November 1, 2010 19:42, Marcos Douglas wrote: > On Mon, Nov 1, 2010 at 3:36 PM, Tomas Hajny wrote: >> On Mon, November 1, 2010 19:20, Marcos Douglas wrote: >>> On Mon, Nov 1, 2010 at 3:10 PM, Marco van de Voort >>> wrote: >>>> In our previous episode, Marcos Douglas said: >>>>> wrote: >>>>> >> Somebody can help me please? >>>>> >> I need to search strings in Text files using just FPC. >>>>> > >>>>> > how about reading every line and then using Pos() to see if some >>>>> string is >>>>> > there? >>>>> > >>>>> >>>>> I don't think this way is the fast way   :( >>>>> I have many PDF files with several pages each. >>>> >>>> You'll be surprised. I've done multi million line logfiles that way. A >>>> pdf2txt is infinitely slow compared with such processing. >>> >>> Ok, but I need to work with pure text. The PDF files are "compiled". >> >> Sorry, I'm confused now. Do you want to search for text strings within >> PDF >> files or within regular text files? > > I need to search for text strings in PDF files... but I don´t know do > that, so I use the pdf2txt[1] to convert in pure text. > If there is a unit that can search for text within a PDF file, would > be the best solution for me. I see, but that means that Marco's comment applies - conversion of PDF files to pure text will probably require more time than finding your string within the (converted) text file.

I agree. But as I search for text within PDF files?

Marcos Douglas

Reply by email

Tomas Hajny
to Marcos Douglas: Which unit do you recommends? Marcos Douglas
Marcos Douglas wrote 25 lines
On Mon, Nov 1, 2010 at 3:31 PM, Tomas Hajny wrote: > On Mon, November 1, 2010 19:10, Marco van de Voort wrote: >> In our previous episode, Marcos Douglas said: >>> wrote: >>> >> Somebody can help me please? >>> >> I need to search strings in Text files using just FPC. >>> > >>> > how about reading every line and then using Pos() to see if some >>> string is >>> > there? >>> > >>> >>> I don't think this way is the fast way   :( >>> I have many PDF files with several pages each. >> >> You'll be surprised. I've done multi million line logfiles that way. A >> pdf2txt is infinitely slow compared with such processing. > > Well, there at least two gotchas there. First, it's better to use a > reasonable (= large enough) buffer size. Second, the simplest approach > implying reading line by line and searching using Pos() obviously isn't > sufficient for searching across line breaks, i.e. you either need to > handle that yourself, or use some unit providing such functionality. Which unit do you recommends?

I don't have any specific one in mind, I just wanted to point out that you need to take care about that. Personally, I'd probably use my unit Buffered (http://www.volny.cz/xhajt03/buffered.zip) and handle the part related to spanning of text across lines myself if relevant (obviously, that depends on your use case), but I'm sure that there are more complete solutions for your needs readily available too; my comment was just to signalize that "Pos" mentioned by Marco may not be sufficient by itself.

BTW, to answer your other e-mail too - I don't know about solutions for searching within PDF files directly, I'd also use the external converter myself.

Tomas

Reply by email

Alberto Narduzzi
to Marcos Douglas: I agree. But as I search for text within PDF files? Marcos Douglas

Sorry,

I agree. But as I search for text within PDF files?

I assumed true the following statement of yours...

[Somebody can help me please?
I need to search strings in Text files using just FPC.]

so I suggested the fast-enough (in my opinion) use of the Pos function. If _now_ you want to search directly in the PDFs, then I cannot help you, as I myself would go for the conversion to text files on first instance.

TTYTT, I usually search for text in PDFs (using Acrobat Reader, just searching, when I need to convert some e-books) and I see it's blooody slow, compared to a search in a plain text file.
I doubt (but also would like to be discredited here...) there is a faster library than the Adobe Acrobat Reader one itself, so I think the combination of the two methods (conversion, and then search) would bring to the same overall speed; if not higher...

Just my last 2c on the thing.

P.S.
Why don't you give it a try, cronometer at hand, first?

Reply by email

Marcos Douglas
to Tomas Hajny: I don't have any specific one in mind, I just wanted to point out that you need to take…
Tomas Hajny wrote 38 lines
On Mon, November 1, 2010 19:34, Marcos Douglas wrote: > On Mon, Nov 1, 2010 at 3:31 PM, Tomas Hajny wrote: >> On Mon, November 1, 2010 19:10, Marco van de Voort wrote: >>> In our previous episode, Marcos Douglas said: >>>> wrote: >>>> >> Somebody can help me please? >>>> >> I need to search strings in Text files using just FPC. >>>> > >>>> > how about reading every line and then using Pos() to see if some >>>> string is >>>> > there? >>>> > >>>> >>>> I don't think this way is the fast way   :( >>>> I have many PDF files with several pages each. >>> >>> You'll be surprised. I've done multi million line logfiles that way. A >>> pdf2txt is infinitely slow compared with such processing. >> >> Well, there at least two gotchas there. First, it's better to use a >> reasonable (= large enough) buffer size. Second, the simplest approach >> implying reading line by line and searching using Pos() obviously isn't >> sufficient for searching across line breaks, i.e. you either need to >> handle that yourself, or use some unit providing such functionality. > > Which unit do you recommends? I don't have any specific one in mind, I just wanted to point out that you need to take care about that. Personally, I'd probably use my unit Buffered (http://www.volny.cz/xhajt03/buffered.zip) and handle the part related to spanning of text across lines myself if relevant (obviously, that depends on your use case), but I'm sure that there are more complete solutions for your needs readily available too; my comment was just to signalize that "Pos" mentioned by Marco may not be sufficient by itself. BTW, to answer your other e-mail too - I don't know about solutions for searching within PDF files directly, I'd also use the external converter myself.

Ok Tomas. Thanks for informations and file (I'll see it).

Marcos Douglas

Reply by email

Marcos Douglas
to Alberto Narduzzi: I assumed true the following statement of yours... [Somebody can help me please? I need…
Alberto Narduzzi wrote 8 lines
Sorry, > I agree. But as I search for text within PDF files? I assumed true the following statement of yours... [Somebody can help me please? I need to search strings in Text files using just FPC.]

Yes, I changed my first mail because nobody answered me about search in PDF files!

so I suggested the fast-enough (in my opinion) use of the Pos function. If _now_ you want to search directly in the PDFs, then I cannot help you, as I myself would go for the conversion to text files on first instance.

I'll try this method too.
Now, I'm trying to convert the PDF files as best I can... I don't know if pdf2txt is the best way...

Show quoted text 7 lines
TTYTT, I usually search for text in PDFs (using Acrobat Reader, just searching, when I need to convert some e-books) and I see it's blooody slow, compared to a search in a plain text file. I doubt (but also would like to be discredited here...) there is a faster library than the Adobe Acrobat Reader one itself, so I think the combination of the two methods (conversion, and then search) would bring to the same overall speed; if not higher...

Yes, that's what I thought too ;-)

Just my last 2c on the thing. P.S. Why don't you give it a try, cronometer at hand, first?

Because I have not gotten in this stage yet :)

Thanks,
Marcos Douglas

Reply by email

José Mejuto
to Marcos Douglas

Hello FPC-Pascal,

Monday, November 1, 2010, 8:40:45 PM, you wrote:

MD> On Mon, Nov 1, 2010 at 4:27 PM, Alberto Narduzzi

MD> wrote 8 lines
> Sorry, > >> I agree. But as I search for text within PDF files? > > I assumed true the following statement of yours... > > [Somebody can help me please? > I need to search strings in Text files using just FPC.]

MD> Yes, I changed my first mail because nobody answered me about search MD> in PDF files!

Search an string in a pdf directly is far away from trivial, you need at least a parse.r and a decompressor. The decompressor is not a problem usually as most files have streams compressed with zlib, but the parser is very complex, locate the text zones is easy (more or less) but if the strings have international characters, it implies the use o CMAPs which is a nighmare.

So if you are looking for ASCII words, use PDF2Text and use the POS function over the result:

function HaveString(Filename: String; TheString: string): Boolean;
var
  F: TFileStream;
  S: String;
  AtPos: integer;
begin
  Result:=false;
  F:=TFileStream.Create(Filename,fmOpenReadOnly);
  SetLength(S,F.Size);
  if F.Size>0 then
  begin
    F.ReadBuffer(S[1],F.Size);
    AtPos:=Pos(UpperCase(TheString),S);
    if AtPos>0 then
      Result:=true;
  end;
  F.Free;
end;
Best regards, José

Reply by email

Graeme Geldenhuys
to Marcos Douglas: Ok Guys. I already know how convert PDF files in Text files. To search strings in Text…

Hi,

I don't know the PDF file format at all, but I would guess if you look at the many pdf-to-text conversion tools, you will see what they have done. In all likelihood, those conversion tools will be written in C, which you will have to translate to Object Pascal - but that is normally not a difficult task, just a boring task.

Regards,
- Graeme -

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://opensoft.homeip.net:8080/fpgui/

Reply by email

Tomas Hajny
to José Mejuto: MD> On Mon, Nov 1, 2010 at 4:27 PM, Alberto Narduzzi MD> Yes, I changed my first mail…

On Mon, November 1, 2010 21:52, José Mejuto wrote: .
.

Show quoted text 21 lines
So if you are looking for ASCII words, use PDF2Text and use the POS function over the result: function HaveString(Filename: String; TheString: string): Boolean; var F: TFileStream; S: String; AtPos: integer; begin Result:=false; F:=TFileStream.Create(Filename,fmOpenReadOnly); SetLength(S,F.Size); if F.Size>0 then begin F.ReadBuffer(S[1],F.Size); AtPos:=Pos(UpperCase(TheString),S); if AtPos>0 then Result:=true; end; F.Free; end;

If I understand it correctly, this assumes reading the whole file into on the size of that file and other conditions,
this may or may not be advisable...

Tomas

Reply by email

José Mejuto
to Tomas Hajny

Hello FPC-Pascal,

Tuesday, November 2, 2010, 11:02:18 AM, you wrote:

TH> If I understand it correctly, this assumes reading the whole file into TH> on the size of that file and other conditions, TH> this may or may not be advisable...

Yes, and a pdf2text conversion will reduce the PDF file to a 1% of its original size, so unless you handle 10 gigabyte PDFs should be not problem in loading the whole file in memory.

I doubt that there are memory problems as running pdf2text will for sure consume more memory that the result file size.

Of course if you will end up with 300 megabytes txt files then a different approach would be needed using a buffer with a window over the size of the searched text.

Also logic will be different if you would like to match one word, several words, large sentences, sequeces of chars, etc.

Best regards, José

Reply by email

Marcos Douglas
to José Mejuto
José Mejuto wrote 21 lines
Hello FPC-Pascal, Tuesday, November 2, 2010, 11:02:18 AM, you wrote: TH> If I understand it correctly, this assumes reading the whole file into TH> on the size of that file and other conditions, TH> this may or may not be advisable... Yes, and a pdf2text conversion will reduce the PDF file to a 1% of its original size, so unless you handle 10 gigabyte PDFs should be not problem in loading the whole file in memory. I doubt that there are memory problems as running pdf2text will for sure consume more memory that the result file size. Of course if you will end up with 300 megabytes txt files then a different approach would be needed using a buffer with a window over the size of the searched text. Also logic will be different if you would like to match one word, several words, large sentences, sequeces of chars, etc.

I need to search several words. So, I can't use Pos function to search each word.
My algorithm need to read each word (token) just one time to be fast. I'll define each separator for each Token like <space>, comma, "/", "\", <enter>, etc. For each token found, I'll search a combination in my lists of words. If I found a match, I need to know which page the token was found...

Marcos Douglas

Reply by email

Marcos Douglas
to DaWorm
DaWorm wrote 10 lines
On Tue, Nov 2, 2010 at 11:45 AM, Marcos Douglas wrote: > > If I found a match, I need to know which page the > token was found... > That may prove most difficult, since I doubt your pdf to text will preserve that very well.

Yeah... that is a big problem...
Maybe, if I have only one number in all line so this is the page number.

Marcos Douglas

Reply by email