Another update from ingydotnet^djgoku

This commit is contained in:
Ingy döt Net 2015-11-18 06:14:39 +00:00
parent 91df62d461
commit 948b86eafa
7604 changed files with 108452 additions and 22726 deletions

View file

@ -0,0 +1,21 @@
(defun semordnilaps (word-list)
(let ((word-map (make-hash-table :test 'equal)))
(loop for word in word-list do
(setf (gethash word word-map) t))
(loop for word in word-list
for rword = (reverse word)
when (and (string< word rword) (gethash rword word-map))
collect (cons word rword))))
(defun main ()
(let ((words
(semordnilaps
(with-open-file (s "unixdict.txt")
(loop for line = (read-line s nil nil)
until (null line)
collect (string-right-trim #(#\space #\return #\newline) line))))))
(format t "Found pairs: ~D" (length words))
(loop for x from 1 to 5
for word in words
do (print word)))
(values))

View file

@ -1,80 +1,79 @@
note
description: "Summary description for {SEMORDNILAP}."
author: ""
date: "$Date$"
revision: "$Revision$"
class
SEMORDNILAP
create
make
feature
feature
make
--read wordlist "unixdict.txt", search across wordlist with binary_search
--Semordnilaps in 'solution'.
local
count,i,j, middle, upper, lower: INTEGER
count, i, middle, upper, lower: INTEGER
reverse: STRING
do
read_wordlist
create solution.make_empty
from
i:= 1
i := 1
until
i> word_array.count
i > word_array.count
loop
word_array[i].mirror
reverse:=word_array[i]
word_array [i].mirror
reverse := word_array [i]
from
lower:= i+1
upper:= word_array.count
lower := i + 1
upper := word_array.count
until
lower>=upper
lower >= upper
loop
middle:= (upper-lower)//2+lower
if reverse.is_case_insensitive_equal (word_array[middle]) then
count:= count+1
upper:= 0
lower:= 1
solution.force (word_array[middle],count)
elseif reverse.is_less (word_array[middle]) then
upper:= middle-1
middle := (upper - lower) // 2 + lower
if reverse.same_string (word_array [middle]) then
count := count + 1
upper := 0
lower := 1
solution.force (word_array [i], count)
elseif reverse.is_less (word_array [middle]) then
upper := middle - 1
else
lower:= middle+1
lower := middle + 1
end
end
if lower < word_array.count and then reverse.is_case_insensitive_equal (word_array[lower]) then
count:= count+1
upper:= 0
lower:= 1
solution.force (word_array[middle],count)
if lower < word_array.count and then reverse.same_string (word_array [lower]) then
count := count + 1
upper := 0
lower := 1
solution.force (word_array [i], count)
end
i:= i+1
i := i + 1
end
end
solution: ARRAY[STRING]
solution: ARRAY [STRING]
original_list: STRING = "unixdict.txt"
feature {NONE}
read_wordlist
-- Preprocessed word_array for finding Semordnilaps.
local
l_file: PLAIN_TEXT_FILE
wordlist: LIST[STRING]
i: INTEGER
wordlist: LIST [STRING]
do
create l_file.make_open_read_write ("unixdict.txt")
create l_file.make_open_read_write (original_list)
l_file.read_stream (l_file.count)
wordlist:=l_file.last_string.split ('%N')
wordlist := l_file.last_string.split ('%N')
l_file.close
create word_array.make_empty
from
i:= 1
until
i> wordlist.count
across
1 |..| wordlist.count as i
loop
word_array.force( wordlist.at (i),i)
i:= i+1
word_array.force (wordlist.at (i.item), i.item)
end
end
word_array: ARRAY[STRING]
word_array: ARRAY [STRING]
end

View file

@ -1,16 +1,34 @@
class
APPLICATION
inherit
ARGUMENTS
create
make
feature
make
do
create se.make
across se.solution.subarray (27, 32)as s loop io.put_string (s.item.out+"%T"); s.item.mirror; io.put_string(s.item.out+"%N") end
io.put_string ("There are "+se.solution.count.out+" pairs.")
end
feature
make
local
test: ARRAY [STRING]
s: STRING
do
create se.make
test := se.solution
create sort.sort (test)
across
test.subarray (1, 5) as t
loop
s := t.item
io.put_string (t.item + "%T")
s.mirror
io.put_string (s)
io.new_line
end
io.put_string ("Total number of semordnilaps: ")
io.put_integer (test.count)
end
se: SEMORDNILAP
sort: MERGE_SORT [STRING]
end

View file

@ -0,0 +1,10 @@
import java.nio.file.Files
import java.nio.file.Paths
fun semordnilap() {
val words = Files.readAllLines(Paths.get("unixdict.txt"), Charsets.UTF_8).toSet()
val pairs = words.asSequence().map { it to it.reverse() } // Pair(word, reversed word)
.filter { it.first < it.second && it.second in words }.toList() // avoid dupes+palindromes, find matches
println("Found ${pairs.size()} semordnilap pairs")
println(pairs.take(5))
}

View file

@ -1,8 +1,8 @@
my \words = set slurp("unixdict.txt").lines;
my $words = set slurp("unixdict.txt").lines;
my \sems = gather for words.list -> \word {
my \drow = word.flip;
take [word,drow] if drow words and drow lt word;
my @sems = gather for $words.flat -> $word {
my $drow = $word.key.flip;
take $drow if $drow $words and $drow lt $word;
}
.say for +sems, sems.pick(5);
say $_ ~ ' ' ~ $_.flip for @sems.pick(5);

View file

@ -0,0 +1,36 @@
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objInFile = objFSO.OpenTextFile(objFSO.GetParentFolderName(WScript.ScriptFullName) &_
"\unixdict.txt",1)
Set objUnixDict = CreateObject("Scripting.Dictionary")
Set objSemordnilap = CreateObject("Scripting.Dictionary")
Do Until objInFile.AtEndOfStream
line = objInFile.ReadLine
If Len(line) > 1 Then
objUnixDict.Add line,""
End If
reverse_line = StrReverse(line)
If reverse_line <> line And objUnixDict.Exists(reverse_line) Then
objSemordnilap.Add line, reverse_line
End If
Loop
'Display the first 5 keys.
k = 0
For Each Key In objSemordnilap.Keys
WScript.StdOut.Write Key & " - " & objSemordnilap.Item(Key)
WScript.StdOut.WriteLine
k = k + 1
If k = 5 Then
Exit For
End If
Next
WScript.StdOut.Write "Total Count: " & objSemordnilap.Count
WScript.StdOut.WriteLine
objInFile.Close
Set objFSO = Nothing
Set objUnixDict = Nothing
Set objSemordnilap = Nothing