I have some code which needs to do things based on a schedule: e.g. during business hours do X, after hours do Y. The schedule will be defined by our customer's so I need a notation which can be written by people and parsed by my program. I'm thinking of something like:
12/25:0730-1730 Do Y
[Mo-Fr]:0730-1730 Do X
[Mo-Tu]:1730-0730 Do Y
Fr:1730-Mo:0730 Do Y
There will definitely be weekly variation. Yearly variation (holidays) seems likely. I would like a notation that is efficient and flexible.
I also need java code which will parse the time ranges and tell me which range a given date time is in.
I've searched the web and found nothing. Closest is CRON notation, which is not quite what I need.
Any one know of an existing notation definition and implementation?
Thanks,
For Java Joda time (Scala wrapper scala-time) is a powerful library for time calculations. You could look at the google-rfc-2445 which does something like what you are asking for (?).
If you are looking for a Java scheduler http://www.quartz-scheduler.org/ is a good option.
I don't think you will find something out of the box. In such cases it's better to do the implementation by yourself and have a full control of the code. You can use antlr to create a parser.
Add a notion of priority to your syntax. Then it will be easier to schedule someting
01.01.2011-31.01.2011 prio 1 do-idle-stuff
[Mo-Fr] prio 2 do-work
[Sa-Su] prio 2 weekend
10.02.2011-17.02.2011 prio 3 go-on-holidays
Related
I had this problem of trying to identifying whether there is a date information contained in a paragraph. So here are the issues:
We don't know where the date string might appear. A paragraph would be something like "We would like set the appointment at Nov. 15th. Then we would .....". So we cannot directly use DateTime.parse()
The format of the date is arbitrary, it can be more formal forms like "Nov. 15th" or "08/21/1988" or "5th in this month".
It would be unlikely to cover all the cases given that the date information can have various forms, I just want to cover as many cases as possible. The lightweight solution I can come up with would be regular expressions I guess.... And again that would be a huge expression. Does anyone know if there are better solutions or available regular expressions for this?
(P.S. I would prefer more light weighted approaches, methods like machine learning might be more general but is not applicable to my task here)
I'd propably approach it with a regular expression (or multiple) as well.
I'd make the regular expression match regions that look date-like by matching everything around "th", "nd" "st", month/day names and abbreviations, dot/line/slash/colon separated numbers or such things. Experiment with that and see how good it finds dates with a ton of test-cases.
Parsing the possible dates is another story. I guess you'd need something as powerful as PHP's strtotime.
Another approach is to just clearly define a big collection of possible formats. Then, when one is detected, you can easily parse it. Feels too brute-force for me though
As a starting point, there are seven pages of date regexes over at http://regexlib.com. If you don't know which one you're looking for, I would create an array and apply them one at a time. You'll still have a problem with dates like 11/12/2015 vs. 12/11/2015 so some kind of process for clarification is still necessary (e.g., automatically mail back and ask "Do you mean December 11 or November 12?").
I want to take input in the name form like two-third or one-fifth and I want my system to convert it into numerical form and give the answer.
Que: two-third of thirty is?
The system should output 20
How can I program it?
As a general problem natural language processing (NLP) - which is what you're talking about - is a difficult open-ended problem.
There are lots of libraries for this stuff. If you want background look here:
Is there a good natural language processing library
Or look up Natural Language Processing in Wikipedia.
However you said you want to do this and you're new to programming.
The first thing you need to do is break the problem down. That's how we solve programming problems.
So first try writing a program that can read a string containing a single word and map it to a number.
For example "One" outputs 1, "Two" outputs 2, "Thirty" outputs 30.
Next try and write a program that cuts a string into its constituent words.
You probably want to use an array here.
That's a process called tokenizing and Java has a built in StringTokenizer to do that.
You might want to code that yourself, but you're learning and it might be the moment to start learning using library code.
When you've got those try combining them so your program can convert "Thirty Seven" into 37 (i.e. numbers under 100).
That new program should combine the ideas of your program than can convert "Thirty" and "Seven" and the one that can split words up.
This is the other thing we do in programming - combining things.
We break it down to smaller problems solve them and then build them back up to solve the bigger problems.
(I apologize if I'm patronizing you but I have no idea of your experience).
After that you might add logic that handles "Five Hundred And Thirty Seven".
Again, notice how spotting Five followed by Hundred is like converting Five and then finding a token that tells you to multiply what you just saw by 100.
You could go on to handle Thousands, Hundred Thousand etc.
Or you could branch off into the fractions.
That's similar but you just have a different vocabulary.
Seven Forty-Seconds = 7/42.
As a learning challenge I would suggest you'll have come a long way if your program handles things like "forty two ninety-thirds of eight hundred and eighty nine".
The easy solution outputs 0.000508 - the floating point answer to (42/93)*889.
The extra credit solution outputs 2/3937 - (42/93)*889 can be simplified as a rational number to 2/3937.
To be honest, you'll be doing well if you can handle "nine-ninths of ninety nine".
Notice that the first word is the numerator (n). The second is the denominator (d). The third is always 'of'. The forth word is either the tens (t) or the units (u). If the forth was the units you're done otherwise if there is a fifth word it's the units.
The answer in that case is n/d*(t*10+u). If the tens or units are missing they're zero - obviously.
PS: You might need special handling for zero if you object to someone typing in ninety zero. It obviously means ninety but we don't say it in English!
you could try an mapping from
one ->1
two ->2
three ->3
four ->4
and so on
and on the other hand:
half ->2
third ->3
fourth ->4
then create an double to divide first value with 2nd..
at least multiply this value with the third (you can use the first mapping for this value) and you got the result.
At least, it is not easy due to you have to build the mapping between string and int manually.
I want to be able to parse expressions representing physical quantities like
g/l
m/s^2
m/s/kg
m/(s*kg)
kg*m*s
°F/(lb*s^2)
and so on. In the simplest way possible. Is it possible to do so using something like Pyparsing (if such a thing exists for Java), or should I use more complex tools like Java CUP?
EDIT: To answere MrD's question the goal is to make conversion between quantities, so for example convert g to kg (this one is simple...), or maybe °F/(kg*s^2) to K/(lb*h^2) supposing h is four hour and lb for pounds
This is harder than it looks. (I have done a fair amount of work here). The main problem is there is no standard (I have worked with NIST on units and although they have finally created a markup language few people use it). So it's really a form of natural language processing and has to deal with :
ambiguity (what does "M" mean - meters or mega)
inconsistent punctuation
abbreviations
symbols (e.g. "mu" for micro)
unclear semantics (e.g. is kg/m/s the same as kg/(m*s)?
If you are just creating a toy system then you should create a BNF for the system and make sure that all examples adhere to it. This will use common punctuation ("/", "", "(", ")", "^"). Character fields can be of variable length ("m", "kg", "lb"). Algebra on these strings ("kg" -> 1000"g" has problems as kg is a fundamental unit.
If you are doing it seriously then ANTLR (#Yaugen) is useful, but be aware that units in the wild will not follow a regular grammar due to the inconsistencies above.
If you are REALLY serious (i.e. prepared to put in a solid month), I'd be interested to know. :-)
My current approach (which is outside the scope of your question) is to collect a large number of examples from the literature automatically and create a number of heuristics.
I am wondering how one would go about specifing a certain amount time, say X seconds. I'm writing the behaviour for a class that represents a Till (as in, a supermarket till), and whish to specify how long it takes to check out 1 item.
I'm doing this so once I receive the number of items a customer has, the time taken to serve the customer is simply:
ITEM_CHECKOUT_TIME * NumberOfItems;
ITEM_CHECKOUT_TIME would be a constant, and what I wish to specify. Some basic arithmetic would be done on this constant, like above.
Sure, I could use a double to represent the time, but I was wondering if it's actually possible with the Time classes, or anything else specifically for this task.
Thanks!
I would not use a double to represent time. I would probably represent it as a whole number of milliseconds (or nanoseconds). If you're looking for something fancier, you might want to look at the Duration class in the Joda-Time library:
http://joda-time.sourceforge.net/key_duration.html
I have a database table which stores the units name in full like liters, kilograms, milliliters, milligrams etc.. I need a library to recogonize these units and convert it to the unit I wish to. How do i do this ?
Code Logic:
I will read the unit "liters" from database and i wish to convert it to milli-liters so her the input is "20 liters" and output should be "20000 milli-liters"
I downloaded JScience library but i am not sure how to do this. please tel me how to use that or suggest any alternative. It would be better if you explain me with a code sample. Thanks!!
I'm inclined to say use Frink, but it does WAY more than you need here, although it does solve the problem
20 litres -> milliliters
//gives 20000
And it is just a cool little language. In java you'd have to run it like any other scripting library. The website has loads of info
I'm not aware that JScience provides a facility for parsing strings like "20 liters" (that spelling makes me cringe...), so you'll probably have to handle that yourself, by tokenizing the string into quantities and units.
Once you have that, you can use JScience to convert between units easily enough, although obviously converting from litres to milliltres is trivial. But in principle, it's something like:
Measure<Integer, Volume> input = Measure.valueOf(20, NonSI.LITRE);
Measure<Integer, Volume> output = input.to(SI.MILLI(NonSI.LITRE));
System.out.println(output);