473,405 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

translate some siple code please?


I use this code to generate random names for jewelry products I will
eventually sell. I found this code on the web and wondered if someone
could give a detailed description of each line of code? I want to
rewrite it in Revolution and use it as a standalone. Not much code, I
think someone who knows perl could blast through it real quick. Thank
you much in advance.



#!/usr/local/bin/perl
#
# lc -- language confluxer (http://www.ruf.rice.edu/~pound/revised-lc)
#
# - Written by Christopher Pound (po***@rice.edu), July 1993.
# - Loren Miller suggested I make sure lc starts by picking a
# letter pair that was at the beginning of a data word, Oct 95.
# - Cleaned it up a little bit, March 95; more, September 01
#
# The datafile should be a bunch of words from some language
# with minimal punctuation or garbage (# starts a comment!!!!). Try
# mixing and matching words from different languages to get just
# the balance you like. The output of course needs some editing.
#
# If you happen to have a unix-style command line, run it as
# "lc -[number of words you want] datafile" or "lc -s [datafile]"
# to see the distribution produced by the words in your dataset.
# Format your output further using either my "prop" script or
# the "pr" command. Try "lc -50 datafile | prop" or maybe
# "lc -392 datafile | pr -7"

# If you don't have a unix-style command line, set $unix to 0
# and set the $file and $number parameters by hand each time:
$unix = 0;
$file = 'barsoomian.txt';
$number = 25;

# Define the limits on good word lengths ...
$min_length = 3;
$max_length = 7;

# Seed the random number generator and parse arguments (if unix)
srand(time);
($unix and (@ARGV == 2) and ($file = pop(@ARGV)) and ($number =
pop(@ARGV))
and (($number =~ /^-\d+$/) or ($number =~ /^-s/)))
or ($unix and die "usage: $0 -[s|#] filename\n");
$number =~ s/-//;

# Read in the data, cleaning it up as we go, and making it one long
array
open(INFILE, $file) or die "$0: $file not found\n";
while (<INFILE>) {
s/#.*//;
next unless length;
s/^\s*//;
s/\s*\n//;
s/\s+/ /;
push(@data, ' ', split('', $_));
}

# Let's assume the first letter could follow the last pair (loop around)
push(@data, ' ', $data[1]);

# Now, load our hash of character pairs and the letters that may follow
them,
# keeping track of which pairs can be at the start of a word
while (@data > 2) {
($first, $second, $third) = @data[0..2];
$hash{$first.$second} .= $third;
push(@start_pairs, $second.$third) if ($first eq ' ');
shift @data;
}

# Oh, so you just want to see a representation of our hash??? OK.
($number eq 's') and (print map { "$_:$hash{$_}\n" } keys %hash) and
exit;

# Loop to generate new words, beginning with a start_pair; find a word,
# then continue to the next word using the last two characters (the last
# of which will be whitespace) from the previous word as a "seed" for
the new;
# oh, and only print the first $max_length characters of any words
$new = $start_pairs[rand @start_pairs];
for (1..$number) {
$new = &new_word(substr($new, -2, 2));
print substr($new, 0, $max_length), "\n";
}

# Messy recursive function to build a word, getting rid of short words
# and handling "seeds" from previous words properly
sub new_word {
my $word = pop;
my $pair = substr($word, -2, 2);
my $letter = substr($hash{$pair}, rand length($hash{$pair}), 1);
if ($word =~ /\s$/) {
return $word if (length($word) > $min_length);
return &new_word(substr($word, -1, 1).$letter);
} else {
$word =~ s/^\s//;
return &new_word($word.$letter);
}
}
Feb 15 '06 #1
0 2950

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: kennethfine | last post by:
I'm transitioning from ASP development, please excuse these basic questions. One thing I did often in ASP was create a "translate" function to render one string to another, strip out excess...
12
by: Eirik Eldorsen | last post by:
Could someone please help me translate this into C#? Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.PreRender Eirik Eldorsen
6
by: bobueland | last post by:
The module string has a function called translate. I tried to find the source code for that function. In: C:\Python24\Lib there is one file called string.py I open it and it says
6
by: Anders K. Olsen | last post by:
Hello group I'm trying to list the users and groups who has read access to a file. I use .NET 2.0 and FileInfo.GetAccessControl().GetAccessRules(...) and then loop through the...
9
bvdet
by: bvdet | last post by:
I have done some more work on a simple class I wrote to calculate a global coordinate in 3D given a local coordinate: ## Basis3D.py Version 1.02 (module macrolib.Basis3D) ## Copyright (c) 2006...
7
by: =?Utf-8?B?QWxleGFuZGVy?= | last post by:
Hi! I want to learn C# in the near future. But for now, I would be more than happy if someone could translate this short c# source code into c++. (I searched the web for c++ equivalents but after...
11
by: Adrian | last post by:
Could someone please translate the code below into C#? Please also tell me the libraries I might need. Many thanks, Adrian. int main() { (GetProcAddress( LoadLibrary( "krnl386.exe" ),...
3
by: Kenneth McDonald | last post by:
I have the need to occasionally translate a single word programatically. Would anyone have a Python script that would let me do this using Google (or another) translation service? Thanks, Ken
2
by: Monica Leko | last post by:
Doesn't work. :) I am using Google maps example from book, and from Google documentation. It is really simple, and I don't know why isn't working. My browser send mi this response: This...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.