Author Topic: I need a favour  (Read 1905 times)

Tomaaz

  • Guest
I need a favour
« on: March 28, 2019, 04:47:32 PM »
Do you remember word count challenge on BP.org? Has anyone still got sources?  The one I need is Lua code. Rest of them are very easy to rewrite from scratch, but, because Lua has limited options for sorting, I had to write a code for it myself (to be honest - this part of the code is what I want). It was really fast and I can't reproduce the speed of it. Thanx!

jbk

  • Guest
Re: I need a favour
« Reply #1 on: March 28, 2019, 07:52:16 PM »
have you tried the wayback machine?
I would try but I don't have the url of the old bp.org

jbk

  • Guest
Re: I need a favour
« Reply #2 on: March 28, 2019, 07:56:25 PM »
hope this helps
Lua
Code: [Select]
unikalne, unikalne2, unikalne3, posortowane, posortowane2 = {}, {}, {}, {}, {}
ilosc, ilosc2 = 0, 0
czas = (os.time())

print("Reading the file...")

plik = io.open("Bible.txt","r")
tekst = plik:read("*a")
io.close(plik)
zawartosc = string.lower(tekst)

print("Counting...")

for x in string.gmatch(zawartosc, "%a+") do
if string.len(x) ~= 1 or x == "a" or x == "i"  then
if not unikalne[x] then
unikalne[x] = 1
ilosc = ilosc + 1
ilosc2 = ilosc2 + 1
else
unikalne[x] = unikalne[x] + 1
ilosc = ilosc + 1
end
end
end

print("All words: ", ilosc)
print("Unique words: ", ilosc2)
print("Sorting...")


for x, y in pairs(unikalne) do
table.insert(posortowane, y)
end


for x = 1, #posortowane do
a = 0.000001
if posortowane[x] == math.floor(posortowane[x]) then
for y = x + 1, #posortowane do
if posortowane[y] == posortowane[x] then
posortowane[y] = posortowane[y] + a
a = a + 0.000001
end
end
end
end

a = 1
for x, y in pairs(unikalne) do
unikalne2[x] = posortowane[a]
a = a + 1
end

for x, y in pairs(unikalne2) do
unikalne3[y] = x
end

for x, y in pairs(unikalne3) do
table.insert(posortowane2, x)
end

table.sort(posortowane2)

print("Writing to the file...")

plik = io.open("words.txt", "w")
plik:write("All words: ", ilosc, "\n")
plik:write("Unique words: ", ilosc2, "\n\n")
for x = #posortowane2, 1, -1 do
plik:write(unikalne3[posortowane2[x]], " - ", math.floor(posortowane2[x]), "\n")
end
io.close(plik)

io.write("Done in ", os.time() - czas, " sec.\n" )
Perl
Code: [Select]
#!/usr/bin/perl

use strict;
use warnings;

open(my $plik, "<", "Bible.txt");
my @lines=<$plik>;
close $plik;

my %pojedyncze;
my $ilosc = 0;

print "Counting...\n";
my $calosc = join(' ', @lines);
$calosc =~ s{[\W\d]}{ }g;
$calosc = lc $calosc;
my @wszystkie = split(' ', $calosc);
foreach my $word (@wszystkie) {
if (length $word > 1 or $word eq "i" or $word eq "a") {
$pojedyncze{$word}++;
$ilosc++;
}
}

print "All words: " . $ilosc . "\n";
print "Unique words: " . keys(%pojedyncze) . "\n";
print "Sorting and writing results to the file...\n";

open(my $slowa, ">", "words.txt");
print $slowa "Total number of words: " . $ilosc . ".\n";
print $slowa "Number of unique words  " . keys(%pojedyncze) . ".\n\n";
foreach my $word (sort { $pojedyncze{$b} <=> $pojedyncze{$a} } keys %pojedyncze) {
print $slowa "$word - $pojedyncze{$word}\n";
}
close $slowa;

print "Done!\n";
PHP
Code: [Select]
<?php 
echo "Reading the file...\n";
$zawartosc file_get_contents('Bible.txt');
$wszystkie explode(" "strtolower(preg_replace('/[^A-Za-z]/'' '$zawartosc)));
$unikalne = array();
$posortowane = array();
$wsz 0;
echo 
"Counting...\n";
foreach (
$wszystkie as $x) {
if (strlen($x) > or $x == "a" or $x == "i") {
if (array_key_exists($x$unikalne)) {
$unikalne[$x]++;
$wsz++;
}
else {
$unikalne[$x] = 1;
$wsz++;
}
}
}
$uni count($unikalne);
echo 
"Sorting and writing to the file...\n";
arsort($unikalne);
$plik fopen('words.txt'"w");
fwrite($plik"All words: $wsz\nUnique words : $uni\n\n");
foreach(
$unikalne as $x=>$y) {
fwrite($plik"$x - $y\n");
}
fclose($plik);
ksort($unikalne);
$plik fopen('words_alphabetically.txt'"w");
fwrite($plik"All words: $wsz\nUnique rords : $uni\n\n");
foreach(
$unikalne as $x=>$y) {
fwrite($plik"$x - $y\n");
}
fclose($plik);
echo 
"Done!\n"
?>

Python
Code: [Select]
import re

print('Reading the file...')
plik = open('Bible.txt', 'r')
wszystkie = (re.sub('[\W\d]', ' ', plik.read())).lower().split()

print('Counting...')
pojedyncze = {}
wsz = 0
for x in wszystkie:
if len(x) > 1 or x == "a" or x == "i":
if x in pojedyncze:
pojedyncze[x] = pojedyncze[x] + 1
wsz += 1
else:
pojedyncze[x] = 1
wsz += 1

print('Sorting and writing to the file...')
plik = open('words.txt', 'w')
plik.write('All words: ' + str(wsz) + '\n')
plik.write('Unique words: ' + str(len(pojedyncze)) + '\n\n')
for y in sorted(pojedyncze, key=pojedyncze.get, reverse=True):
plik.write(y + ' - ' + str(pojedyncze[y]) + '\n')
plik.close()

print("Done!")
Ruby
Code: [Select]
puts "Reading the file..."

plik = File.new('Bible.txt', 'r')
wszystkie = plik.read.gsub(/[^A-Za-z]/, ' ').downcase.split(' ')
unikalne = {}
wsz = 0

puts "Counting..."

wszystkie.each do |x|
if x.length > 1 or x == "a" or x == "i" then
if unikalne[x] then
unikalne[x] += 1
wsz += 1
else
unikalne[x] = 1
wsz += 1
end
end
end

puts "Sorting and writing results to the files..."

plik = File.new('words.txt', 'w')
plik.write("All words: " + wsz.to_s + "\n")
plik.write("Unigue words: " + unikalne.length.to_s + "\n\n")
unikalne.sort_by{|slowo, ilosc| ilosc}.reverse.each do |a, b|
plik.write(a + " - " + b.to_s + "\n")
end
plik.close

puts "Done!"
Tcl
Code: [Select]
set plik [open "Bible.txt" r]
set calosc [read $plik]
close $plik

puts "Counting..."

regsub -all {[^a-zA-Z]} $calosc " " calosc
set calosc [string tolower $calosc]

set wszystkie [split $calosc " "]
foreach line $wszystkie {
if {[string length $line] > 1 || $line eq "a" || $line eq "i"} {
incr x
if {[info exists unikalne($line)]} {
set unikalne($line) [expr {$unikalne($line) + 1}]
} else {
set unikalne($line) 1
incr y
}
}
}

puts "All words: $x"
puts "Unique words: $y"

foreach slowo [lsort [array names unikalne]] {
    set wartosci($unikalne($slowo)) 1
}

foreach liczba [lsort [array names wartosci]] {
lappend ilosci $liczba
}

puts "Sorting by occurence and writing to the file..."
set plik [open "words.txt" w]
puts $plik "All words: $x"
puts $plik "Unique words: $y\n"

foreach z [lsort -integer $ilosci] {
foreach slowo [array names unikalne] {
if {$unikalne($slowo) == $z} {
puts $plik "$slowo - $unikalne($slowo)"
unset unikalne($slowo)
}
}
}
close $plik

puts "Done!"

BaCon
Code: [Select]
DECLARE kawalki$[100000] TYPE STRING
DECLARE zastapione$[100000] TYPE STRING
DECLARE wszystkie ASSOC int

PRINT "Reading the file..."

OPEN "Bible.txt" FOR READING AS ksiazka
x = 0
WHILE NOT(ENDFILE(ksiazka)) DO
READLN akapit$ FROM ksiazka
kawalki$[x] = LCASE$(akapit$)
INCR x
WEND
CLOSE FILE ksiazka

PRINT "Counting..."


FOR y = 0 TO x
zastapione$[y] = REPLACE$(kawalki$[y], "[^a-z]", " ", TRUE)
NEXT

wsz = 0
uni = 0
FOR y = 0 TO x
FOR slowo$ IN zastapione$[y]
IF LEN(slowo$) > 1 OR slowo$ = "a" OR slowo$ = "i" THEN
IF ISKEY(wszystkie, slowo$) = TRUE THEN
INCR wszystkie(slowo$)
INCR wsz
ELSE
wszystkie(slowo$) = 1
INCR uni
INCR wsz
ENDIF
ENDIF
NEXT
NEXT

PRINT "All words - ", wsz
PRINT "Unique words - ", uni

PRINT "Sorting and writing to the file..."

SORT wszystkie

OPEN "words2.txt" FOR WRITING AS wynik
WRITELN "All words - " & STR$(wsz) TO wynik
WRITELN "Unique words - " & STR$(uni) TO wynik
WRITELN " " TO wynik
LOOKUP wszystkie TO klucze$ SIZE rozmiar
FOR x = rozmiar - 1 TO 0 STEP -1
    WRITELN klucze$[x] & " - " & STR$(wszystkie(klucze$[x])) TO wynik
NEXT
CLOSE FILE wynik

PRINT "Done!"
Pike
Code: [Select]
void main() {
array|string a;
array|string b = ({});
mapping c = ([]);
int x;
int x2 = 0;
string y;
write("Reading the file...\n");
string content=Stdio.File("Bible.txt")->read();
content = lower_case(content);
a = content / " ";
write("Counting...\n");
for(x = 0; x < (sizeof(a) - 1); x++) {
a[x] = Regexp.PCRE("[^a-z]")->replace(a[x]," ");
b = b + (a[x] / " ");
}
for(x = 0; x < (sizeof(b) - 1); x++) {
if (strlen(b[x]) > 1 || b[x] == "a" || b[x] == "i") {
if (c[b[x]]) {
c[b[x]]++;
}
else {
c[b[x]]=1;
}
x2++;
}
}
object plik = Stdio.File();
plik -> open("Words-pike.txt", "wc");
write("All words: ");
plik -> write("All words: ");
write("%d\n", x2);
plik -> write("%d\n", x2);
write("Unique words: ");
plik -> write("Unique words: ");
write("%d\n", sizeof(c));
plik -> write("%d\n\n", sizeof(c));
write("Counting numbers of occurences of each word, sorting and writing the result to the file...\n");
a = reverse(sort(values(c)));
foreach(a, x) {
foreach(indices(c), y) {
if (c[y] == x) {
plik -> write("%s - ", y);
plik -> write("%d\n", c[y]);
m_delete(c, y);
}
}
}
write("Done!\n");
plik ->close();
}
« Last Edit: March 28, 2019, 08:04:42 PM by jbk »

Tomaaz

  • Guest
Re: I need a favour
« Reply #3 on: March 28, 2019, 08:50:15 PM »
Yes! Thanx a lot! :)

jbk

  • Guest
Re: I need a favour
« Reply #4 on: March 28, 2019, 08:57:26 PM »
I had that web page saved to my hard drive, after making the post above the file disappeared, go figure.

Aurel

  • Guest
Re: I need a favour
« Reply #5 on: March 28, 2019, 09:40:56 PM »
Quote
. Rest of them are very easy to rewrite

really ...wow  ;D