Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Text parsing

Name: Anonymous 2014-04-16 11:26

Hey guys suppose I have an application that is fed the body of my post. Now my post might contain quotations such as
>>8
>>3-7
>>2,3,4
>>7-8,2,5,10-12
It also may have quotations mid-text such as >>42.

Now, I want the result of my application to be
2 3 4 5 6 7 8 10 11 12
.
Notice 42 is missing because it was midtext.

How would you code that? (Before you laugh and say it's trivial - it is, but I want to code it).

I figured, maybe I will use perl. But maybe not. Since it's been a lot of time since I coded, I want to use something that is funky to code it. Perhaps factor, or J. Well, that's my shitty thread, thanks for reading.

Name: Anonymous 2014-04-16 11:27

Oh and by the way I want to code that because the current mechanism of the software to show you the quoted posts when you hit the Preview button is broken. Try quoting >>1,2 on top of your post and hit Preview; only >>1 will be shown. So after we cook up some solutions I think it'd be right to tell the admin.

Name: Anonymous 2014-04-16 12:17

http://www.jsoftware.com/help/primer/j_list_adding.htm
Years of research and thought have gone into how J verbs work with lists. For example, if you wanted to add 1 to each number in a list.

1 + 2 3 4
3 4 5

It just checks if one of the lists has unit length. "Years of thought".

Name: Anonymous 2014-04-16 12:20

>>3
K I was wrong. It actually is some sort of type checking beforehand.

Name: Anonymous 2014-04-16 12:47

Here's the monte carlo method of estimating pi in J gentlemen.

foo =: monad : 0
4*(+/((?y$0^2)+(?y$0^2))<1)%y
)

Although it doesn't fucking work! Maybe the PRNG sucks. I also don't like the number of parentheses I had to use and the fact I repeated the y argument so many times. I wonder how these can be eliminated from the code.

Name: Anonymous 2014-04-16 12:56

>>5
btw in pseudo common lisp this would be

(defun estimate-pi (n) ; number of points n to use for the method
(/ (count (\. (x) (< x 1))
(mapc +
(split-in-half ; splits (a b c d) to ((a b) (c d))
(mapcar (\. (x) (^ x 2))
(randlist (* 2 n))))) ; a list of length 2n of [0,1] random doubles.
n 0.25)

I'm only posting this so that you can see how shitty the algorithm that I used in the >>5 post is, if you don't know J.

Name: Anonymous 2014-04-16 20:45

Bump for code.

Name: Anonymous 2014-04-17 0:42

More COMMON-LITHPy


(defun estimate-pi (n)
(labels ((rand-point ()
(- (random 2.0) 1.0)))
(let* ((hits (loop for sample from 1 to n
sum (let* ((x (rand-point))
(y (rand-point))
(sr (+ (* x x) (* y y))))
(if (< sr 1.0)
1
0))))
(ratio (/ hits n))
;; ratio =~= (area circle)/(area square)
;; = \pir^2/(2r)^2
;; = \pi/4
(pi-aprox (* ratio 4)))
pi-aprox)))

Name: Anonymous 2014-04-17 1:19

>>8
It can be inlined with some additional loop fu

(loop repeat n
counting (< (+ (expt (random 1.0) 2)
(expt (random 1.0) 2))
1.0)
into c
finally (return (/ c n 0.25)))

writing CL is a joy really. I am trying to cook up a solution in mathematica using anonymous functions and the y combinator, the idea is that you need a predicate and just the anonymous function calling itself with two internal counters, n and the number of times the predicate was sucessful. So the black box would look something like this

(whatever P) ==> now you get a function that when called with argument n represents the number of times P was satisfied when called n times

... but I failed. I'll continue watching my movie Donnie Darko.

Thanks for the post!

Name: Anonymous 2014-04-17 1:26

>>9
actually in mathematica I was stuck on how to return a lambda from a lambda, like this

(#[1.0] &)&

Which does not do the expected.
for example if I called this lambda with Random as argument I expect to be returned a function that when called returns a number between 0.0 and 1.0 but that didn't work.

Name: Anonymous 2014-04-17 1:52

(defun estimate pi (n) 3.14)

Name: Anonymous 2014-04-17 1:56

>>11
Couldn't you at least throw some more digits there?
Why not just define it 0? Surely that's an approximation.

Name: Anonymous 2014-04-17 2:16

>>12
Real physicists use "1" as pi.

Name: Anonymous 2014-04-17 4:25

>>9
woweeee

Name: Anonymous 2014-04-17 12:16

It doesn't handle cases like >>-5 or >>5- since I don't feel like adding that, but here's some perl.

perl -lne 'if (/^>>([\d,-]+)/) { $m = $1; $m =~ s/-/../g; push @r, eval($m) } END { print join " ", sort { $a <=> $b } grep { ! $u{$_}++ } @r }'

Name: Anonymous 2014-04-17 12:59

Guess I can't leave code unfinished... here's a revised version.

perl -lne 'if (/^>>([\d,-]+)/) { foreach (split /,/, $1) { s/^-/1-/; s/-$/-1000/; s/-/../g; push @r, eval($_) } } END { print join " ", sort { $a <=> $b } grep { ! $u{$_}++ } @r }'

Name: Anonymous 2014-04-17 13:40

>>15,16
Does it sanitize input? What happens to quotes like
>>1,a
In particular I am worrying about eval($_). It's a nice idea to replace 1-5 with 1..5, but how exactly does this work? Since print 1..5 produces 12345 and not 1 2 3 4 5.

I guess @r is the array that you use to store the numbers and later sort. But how do you make the numbers unique in it? What's the grep statement doing? Is $u a hashtable? I don't understand bawwwwwww

Name: Anonymous 2014-04-17 13:46

>>17
AHa! You use grep later to make the search unique! How the hell does grep know which variable we're searching?

Name: Anonymous 2014-04-17 16:46

>>17,18
It assumes that the input is well formed, but it wouldn't be hard to add sanity checking. Only digits and dots will make it through to the eval as is. Adding something like next if /^-+$/ at the start of the foreach block and next if ! /^\d+(-\d+)?$/ after the second substitution should cover all the cases, 多分.

Though it would be a good idea to check against the textboard software to verify how it handles some of the cases.

The code works by building a list of all the referenced post numbers, with duplicates, in the main block. Then the END block sorts and uniqs that list. The grep block works by building a hash table of all the array elements with their count, and because it's negated, only returns elements that haven't previously been added to the table (count value of zero).

Name: Anonymous 2014-04-17 20:23

Thinking a bit more, you could optimize out the array and just build the hash in the main block. Then you'd have:

perl -lne 'if (/^>>([\d,-]+)/) { foreach (split /,/, $1) { next if /^-+$/; s/^-/1-/; s/-$/-1000/; next if ! /^\d+(-\d+)?$/; s/-/../g; foreach (eval) { $r{$_} = 1 } } } END { print join " ", sort { $a <=> $b } keys %r }'

On a tablet so I can't test this at the moment.

Don't change these.
Name: Email:
Entire Thread Thread List