News for hackers. Stuff that matters.
Re:Natural Language Processing in Bash by Manchit on Saturday April 20, 2024 @11:29PM (#40102968)
Interesting, though I'll probably won't try it in bash
Re:Natural Language Processing in Bash by totetsu on Sunday April 21, 2024 @12:12AM (#40103128)
I’m still waiting for a shell with a parser as great for natural language as discworld mud was.

$ ls every second jpeg in first red folder in my backpack

Re:Natural Language Processing in Bash by _wire_ on Sunday April 21, 2024 @08:18AM (#40105103)
AppleScript offers a NL style where you code using an english language description of an imperative syntax.

At first it seemed cool, but I quickly found it to be perverse and incomprehensible.

These samples are from Apple's Guide:

set numClowns to 5

--result: 5

set myList to { 1, 2, "four" }

--result: {1, 2, "four"}

tell application "TextEdit"

    set word1 to word 1 of front document
end tell

--result: some word

"DUMPtruck" is equal to "dumptruck" --result: true

considering case "DUMPtruck" is equal to "dumptruck" end considering --result: false

I DID NOT MAKE THIS UP

on raiseToTheNth(x, power)

    set returnVal to x

    repeat power - 1 times
        set returnVal to returnVal \* x

    end repeat

    return returnVal
end raiseToTheNth

The good news is that you can control an app as if you are interacting with its GUI and all the features are in there somewhere, somehow.

tell application "Finder"

    ignoring application responses

        empty the trash

        -- other statements that ignore application responses

        considering application responses

            set itemName to name of first item of startup disk

        end considering

        -- other statements that ignore application responses

    end ignoring
end tell

The bad news is a syntax from hell and how do you discover the available idioms?

The facility can be turned into an abomination when you interface from a shell using the osascript shim.

It's so bad that if I have to use it I can't help but fall in with it love because a working script seems like a minor miracle to be recorded in a religious tome of divine encounters.

Re:Natural Language Processing in Bash by IshKebab on Sunday April 21, 2024 @01:36PM (#40107546)
I agree, the "natural" syntax is awful. Worst of both worlds. Terrible for experienced programmers because the syntax is so bad; terrible for beginners because it pretends to be natural but isn't.

Fortunately they offer a (barely documented) JavaScript alternative these days.

Re:Natural Language Processing in Bash by kwhitefoot on Sunday April 21, 2024 @03:48AM (#40103904)
Here is the code from words.sh

   cat /dev/stdin\
 | tr 'A-Z' 'a-z'\
 | sed 's/[^a-z\.]/ /g'\
 | tr -s ' .'\
 | sed 's/\./ \. /g'\
 | tr -s ' '\
 | tr ' ' '\n'\
 | grep -v '^$';
    exit 0;
Isn't that a Useless Use Of Cat? It works without the cat and the first pipe character.

Similarly:

     cat moby-dick.txt | ./words.sh > words.txt
This could be:

     < moby-dick.txt ./words.sh > words.txt
cat is for concatenating files
Re:Natural Language Processing in Bash by sambazi on Sunday April 21, 2024 @04:29AM (#40104052)
true, though i think the code style focuses on readability
Re:Natural Language Processing in Bash by massimo-nazaria on Sunday April 21, 2024 @10:51AM (#40106185)
Exactly
Re:Natural Language Processing in Bash by olejorgenb on Sunday April 21, 2024 @06:32AM (#40104563)
I don't get the anticat crusaders. The overhead of a `cat` command surly is entirely negligible ?

    cat moby-dick.txt | ./words.sh > words.txt
I much more readable to me than

    < moby-dick.txt ./words.sh > words.txt
In the `tr` case I kinda agree, but there is a niceness to the flow when all the transformation steps are aligned like that.

> cat is for concatenating files

The real world disagrees.

It's nice to know that `< file` work though. If the input of an pipeline is what's varying writing it like `{ command1 | command 2 } < input.txt` make it slightly more convenient iterating on since changing the last word is more accessible after getting the command from history. (turning on noclobber to avoid accidentally typing `>` to be safe)

Re:Natural Language Processing in Bash by massimo-nazaria on Sunday April 21, 2024 @10:50AM (#40106173)
Great comment, strongly agree.
Re:Natural Language Processing in Bash by _wire_ on Sunday April 21, 2024 @07:31AM (#40104829)
It's been wisely pointed out elsewhere that pre-pending an empty cat saves the need to futz with the first stage syntax when reusing the pipeline. It serves a syntactic function.
Re:Natural Language Processing in Bash by massimo-nazaria on Sunday April 21, 2024 @10:51AM (#40106182)
Strongly agree.
Re:Natural Language Processing in Bash by jwilk on Sunday April 21, 2024 @01:56PM (#40107731)
It's certainly a useless use of /dev/stdin.
Re:Natural Language Processing in Bash by massimo-nazaria on Sunday April 21, 2024 @10:44AM (#40106132)
Great to see a post of mine shared and commented here on hacker news! Never happened before and I'm glad for that. Thank you all