LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-06-2011, 09:38 AM   #1
fad216
LQ Newbie
 
Registered: Feb 2011
Posts: 11

Rep: Reputation: Disabled
ngram text file using perl


Hello...I have a few problem. I have txt file like this:

Quote:
00 21 55 84 9a ff 00 1f 9e 1a 5b 00 08 00 45 00 00 4b 00 00 40 00 3f 11 9a 0e a1 8b fa 02 04 02
Then, based on my txt file, I would like to generate text like this:

Quote:
00215584 2155849a 55849aff 849aff00 9aff001f ff001f9e 001f9e1a 1f9e1a5b 9e1a5b00 1a5b0008 5b000800 00080045 08004500 00450000 00004b00 004b0000 4b000040 00004000 0040003f 40003f11 003f119a 3f119a0e 119a0ea1 9a0ea18b 0ea18bfa a18bfa02
Based in my reading, I found about ngram solution in perl, but I not really understand to edit from source code given. I m begineer user in programming language. I hope to get the solution.

This is ngram source code in .pm (http://search.cpan.org/~simon/Text-Ngram-0.03/Ngram.pm):
Quote:
package Text::Ngram;

use 5.006;
use strict;
use warnings;

require Exporter;

our @ISA = qw(Exporter);
our %EXPORT_TAGS = ( 'all' => [ qw( ngram_counts add_to_counts) ] );
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
our @EXPORT = qw();

our $VERSION = '0.03';

require XSLoader;
XSLoader::load('Text::Ngram', $VERSION);

# Preloaded methods go here.

sub clean_buffer {
my $buffer = lc shift;
$buffer =~ s/\s+/ /g;
$buffer =~ s/[^a-z ]+/ \xff /g;
return $buffer;
}

sub ngram_counts {
my ($buffer, $width) = @_;
my $href = process_buffer(clean_buffer($buffer), $width||5);
for (keys %$href) { delete $href->{$_} if /\xff/ }
return $href;
}

sub add_to_counts {
my ($buffer, $width, $href) = @_;
if (!defined $width or !$width) {
my ($key, undef) = each %$href; # Just gimme a random key
$width = length $key || 5;
}
process_buffer_incrementally(clean_buffer($buffer), $width, $href);
for (keys %$href) { delete $href->{$_} if /\xff/ }
}

1;

Thank you...
 
Old 03-07-2011, 06:11 PM   #2
kris_kiil
LQ Newbie
 
Registered: Aug 2005
Distribution: Slackware 12.2
Posts: 13

Rep: Reputation: 0
You don't need the Ngram module for this.

Just read the text into an array by splitting on space, then you can print a moving slice of the array and achieve your desired result.

Good luck
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Parse Text File With Perl unsymbol Programming 1 03-05-2011 05:36 AM
Perl replace text in file ShaqDiesel Programming 27 08-11-2010 10:50 PM
deleting rows in text file in Perl shifter Programming 4 06-28-2010 07:10 PM
[SOLVED] Perl replace text in file mwanakenya Programming 9 04-16-2010 03:08 AM
How can I attach a text file to the email in perl prakash.akumalla Linux - Newbie 8 10-30-2008 06:41 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:13 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration