Definition of Natural Sort (edge cases) - sorting

A "natural sort" differs from a well-defined lexicographic sort; it attempts to match human expectation, for example sorting "10 cats" after "2 cats", not before.
However, in implementing a natural sort, there are many edge cases I'm not sure of. For example, in each of the following sets, consider which sorts first:
Leading zero: 0 cats, 00 cats, 01 cat, 1 cat
Whitespace: 0 cats, 0 cats
Whitespace:  a cat, a cat
Accents: éclairs, eclairs
Casing: a cat, A cat
Casing/Cascade: a1, A0, a0, A1
Casing/Truncation: a, A cat, A, a cat
Truncation: cat, catalog
Separators: a cat, a,cat
Separators/Decimals: 1 animals, 1.1 cats, 1.13 dogs, 1.2 iguanas
…
My question is NOT your personal opinion on each of the above. My question is:
Do there exist any precise definitions of "Natural Sort" that clarify edge cases above?
I recognize that there is no single authority that can make a single definition that everyone has to abide by. I recognize that some applications of a natural sort may want to sort 1, 1.11, 1.2 and others may want to sort 1, 1.2, 1.11. So, I'm not looking for the defintion. I'm looking for any one (or more) reasonably-accepted, precise definitions of how a natural sort should behave.

Related

Infer "shapes", or infer analogous relations in Word2Vec

Gensim Word2Vec offers a system for inferring analogous relationships, that is, with the "same shape" as those already found?
Es:
Starting from King, Queen
I would like to get other couples with male / female gender.
In other word:
most_similar(positive=['king', X], negative=['queen']) -> Y
I would like to find as many xy pairs.
There's no built-in facility resembling what I think you're asking.
But, you are of course free to cycle through any number of candidate words (as X, or the other arguments to most_similar()), to see what top-neighbors are reported (candidate Y values) - perhaps applying some threshold of similarity.
Note the famous man:king :: woman: _?_ is usually presented to a word2vec model in Gensim as most_similar(positive=['king', 'woman'], negative=['man']), which sort of achieves king - man + woman = _?_. I'm not sure your alternate formulation, effectively king - queen + X = Y has an analogical meaning, for arbitrary X or responses Y.
And, note that most_similar() suppresses the reporting of any candidate wards that are already arguments to positive or negative. Often, the results of the 'artihmetic' are still closer to the input words than anything else - but that won't be reported, showing next-best words instead.

Knights and Knives Logical Proposition

I have a question regarding Knights and Knaves and logical proposition. If I want to solve the puzzle and I assume I have two kinds of citizens: Knights, who always tell the truth, and knaves, who always tell lies. On the basis of utterances from some citizens, I must decide what kind they are.
There are three kinds of citizens: a, b and c, who are talking about themselves:
a says: ”All of us are knaves.”
b says: ”Exactly one of us is a knight.”
To solve the puzzle I should determine: What kinds of citizens are a, b and c? I should solve the puzzle by modelling the two utterances above using propositional logic, and I assume that I can use p to describe a knight and ¬p to describe a knave. How would I go about doing that? Any hint for someone who hasn't done any noticeable discrete mathematics in college?
A and C are Knaves. B is a Knight.
If A is a Knight, "All of us are knaves" is true. So, A would also be a Knave. This is a contradiction. Hence, A is a Knave.
If B is a Knave, then "Exactly one of us is a knight." is false. Meaning that 2 or more are Knights. But neither A nor B is a Knight. How can possibly be 2 or more Knights (since C is the only one with a possibility of being a Knight). This is also a contradiction. So, B is a Knight.
We just showed that B is a Knight. So, he himself is the only Knight he is talking about. So, C is a Knave.
Now, I don't think you can model this argument in the Propositional Logic. For one, notice the universal and existential quantifiers ("All" and "Exactly One") in the statements "All of us are knaves" and "Exactly one of us is a knight.". For another one, notice that A and B are talking about themselves. Modeling situations like this is one of the hardest problems in the history (not kidding!). Look at the following links for more info:
https://en.wikipedia.org/wiki/Liar_paradox
https://en.wikipedia.org/wiki/Self-reference
you can create a Truth table,
by first look at it i can say A must be knave, and B is a knight. because if A is a knight he can't say he's a knave(lie), also he can't be right about that all are knaves (can't say the truth) so B is a knight(if B Knave he can't say the truth that makes A a liar and he must be one) and then C is a Knave.

Real numbers not working in Prolog

I have following code:
:-use_module(library(clpfd)).
afn(A,B,C):-
C #= B*A.
It works all right with integers but not with decimal numbers:
43 ?- afn(20, 10, C).
C = 200.
44 ?- afn(20, -10, C).
C = -200.
45 ?- afn(20, -10.5, C).
ERROR: Domain error: `clpfd_expression' expected, found `-10.5'
46 ?-
How can I work with decimals also here? Thanks.
Edit: I find following works with decimals:
afn(A,B,C):-
C is B * A.
67 ?- afn(20.895, 40.5, C).
C = 846.2475.
Is 'is' a correct approach ?!
CLP(FD) implements reasoning over integers.
To extend declarative arithmetic to decimal numbers, you have basically two options:
use a dedicated constraint solver over rationals. See CLP(Q).
scale all numbers so that only integers arise and continue using CLP(FD).
I have seen both approaches successfully applied. Option (2) is sometimes preferable because CLP(FD) is more widely available, and also because you can successfully solve non-linear constraints with it over finite domains.
EDIT: In your case, a suitable query could look like:
?- afn(200, -105, C).
C = -21000.
At the end, you have to scale the results again. This way, you can simulate decimals via integers.
Note that resorting to floats is not a good solution at all, since you cannot really trust the results. Therefore, either use rational numbers or integers, as long as more advanced formats are not yet widely available in Prolog systems.
You have to distinguish
Functional (or "moded") arithmetic. This is the one you are familiar with from other programming languages: You know which of your arguments are input and which is output, and you simply compute the output from the inputs. That's what you get from is/2 (which is part of standard Prolog), and if that's all you want to do, stick with it.
?- A=3, B=1.5, C is B * A.
C = 4.5
?- A=3, C=4.5, C is B * A.
instantiation fault <<< input B not kown
Relational arithmetic. Here, you state an equation or inequation that must hold between the variables involved. There is no notion of input/output arguments. The system's job is instead to make sure that all the stated relations hold at the time a solution for the variables is presented.
There are many different ways to implement relational arithmetic, and many methods only work for subsets of the problem (e.g. only integers, or only linear expressions), therefore these features are typically provided in the form of libraries.
One available implementation of general relational arithmetic over the real numbers is ECLiPSe's library(ic) (see 2, 3), which represents real numbers as floating point intervals:
?- A=3, C=4.5, C $= B * A.
B = 1.5__1.5 <<< precise result
?- C $= B * A, C=1, A=10.
B = 0.099999999999999992__0.1 <<< imprecise but accurate result
There are 2 delayed goals.
?- ln(X) $>= sin(X).
X = X{0.36787944117144228 .. 1.0Inf} <<< partially solved
There are 3 delayed goals.
Having said that, using interval arithmetic and interpreting results correctly is not always straightforward, that's why the kind of workarounds suggested by #mat can be useful (when applicable).

First-order predicate calculus, am I doing this right?

This is the statement:
All birds can fly except for penguins and ostriches or unless they
have a broken wing.
Here is my attempt:
∀x birds(x)→ fly(x)^((birds(x, penguins)^birds(x,
ostriches))˅broken(wing)→¬fly(x))
is my attempt correct?
how do we present "except" in predicate logic?
thanks
On understanding "except" ...
When we say "A except B" we normally mean that A and B are mutually exclusive. Either A is the case or B is the case but not both.
If you think in terms of sets then
All birds can fly except for penguins and ostriches or unless they have a broken wing
could be re-written as
In the universe of birds, there are exactly two distinct sets -- one in which every member of the set can fly and the other in which you find penguins and ostriches and birds with broken wings.
(In passing, note the way the words "and" and "or" in English often need to be adjusted in a symbolic expression.)
Birds
+-----------+------------+
| | |
| Fly | Exceptions |
| | |
+-----------+------------+
Representing mutual exclusion in predicate logic is most easily handled by exclusive-or (XOR). We want to say fly XOR exceptions.
In systems that allow quantifiers to limit the universe of discourse, we could write:
∀x∊birds (fly(x) XOR (penguin(x) v ostrich(x) v brokenWing(x)))
If quantifiers are unlimited, then:
∀x (bird(x) → (fly(x) XOR (penguin(x) v ostrich(x) v brokenWing(x))))
And if XOR is not in the set of allowed operators, then you might have to use the equivalence:
p XOR q ≡ ((p v q) & -(p & q))
There are a couple of other implications hiding in the English sentence that are not fully expressed in the suggestions above.
The sentence in predicate logic allows the case that there are
no birds, whereas the English sentence probably implies that there is
at least one bird.
"A except B" in English normally implies that there are at
least some instances of the exception. Not only is there at least one
bird, but there is at least one penguin that cannot fly. That could
be added to the predicate sentence via appropriate use of existential
quantifiers.
"A except B" in English nearly always
implies that A is the most common case and B is the exception. In the
absence of other evidence, we would assume A. In the universe of
birds, most can fly and only the listed exceptions cannot fly. There
is no easy construct in predicate logic to capture the sense of a
majority case.
No, your attempt is incorrect. It says that all birds fly and also some birds don't fly, so it's a contradiction. Also note that broken(wing) doesn't mention x at all.
As a hint, it should look like
∀x (bird(x) ^ ¬<conditions under which birds don't fly>) → fly(x)
There is a "prolog" tag to your question. In Prolog it can be:
fly(X, WingCondition) :-
bird(X),
X \= penguin,
X \= ostrich,
WingCondition \= broken.
So all birds and not birds that are penguins, ostriches and birds with broken wings can fly
∀x (birds(x) ^ ¬ (birds(x, penguins) ^ birds(x, ostriches) ^ broken_wing(x))) → fly(x))
or this maybe
∀x (birds(x) ^ ¬ (birds(x, penguins) ^ birds(x, ostriches) ^ birds(x,broken_wing))) → fly(x))

Deterministic Finite Automata pattern

I'm trying to solve this problem using Deterministic Finite Automata :
inputs: {a,b}
conditions:
a. must have exactly 2 a
b. have more than 2 b
so a correct input should be like this abbba or bbbaa or babab
now my question is, "is there a pattern to solve this things?"
Yes there is a pattern. You can take each statement and deduct pre-states from them. Then you take the cross-product of those pre-states, which will comprise the final states. In this example:
a. will yield states: 0a, 1a, 2a, 2+a (you've seen 0 a, 1 a, 2 as or more than 2 as)
b. will yield states: 0b, 1b, 2b, 2+b (you've seen 0 b, 1 b, 2 bs or more than 2 bs)
The cross product of these states result in 4x4=16 states. You'll start from {0a,0b} states. The inputs can be 3 types: a, b or something else.
From that you should be able to go. Do you need more help?
(Are we solving homework?)
Always draw such things first.
Feel free to give states any meanings. What you need here is states like: q2: (1 b, 2 a's). Draw states like this, until the accept state and connect them with lines. The accept state is qx: 2 a's 3 b's.
After reaching the accept state, if input is "b" that line goes to itself, the accept state. If the input is "a", draw a new state, that will get into an endless loop and goes into itself no matter what the input is.
(are we helping for an exam here?)

Resources