Natural language generation for time objects - nlg

Is there a library (preferably in JavaScript but not required) that takes an array of datetime objects and perform natural language generation and produce a human readable sentence?
e.g.
["monday","tuesday","wednesday","thrusday","friday"] // "on weekdays"
[9,10,11,12,13,14] // "between 9am and 2pm"
It only need to work in English, but of course additional language support would be preferred.

Found a C# library that takes a cron expression and generate the natural language output: https://github.com/bradyholt/cron-expression-descriptor

Related

Is there a specific way to define language grammar?

If I were to want to create a new programming language, is there a specific file format or extension used to define the grammar of this new language? Or should I just use a plain text file?
What you are looking for might be a metasyntax notation, sometimes also called "formal grammar".
A common notation to describe at least certain types of formal languages is Extended Backus-Naur Form. It defines the grammar by means of "production rules" that gradually expand a "start symbol" into any valid expressions in the described language.
This format is, however, mainly a notation (i.e. it describes how you write down the language description, even when you're writing on paper). It is not so much a "file format" in the sense that there would be a standard extension for it.

When to use an inline codeSystem in a FHIR ValueSet

When creating a FHIR ValueSet using ALL codes of a small externally defined code list, which would be more appropriate (and indeed correct per the FHIR specification) - a composition or an inline codeSystem?
As an example, creating a ValueSet from the following code list:
http://www.datadictionary.nhs.uk/data_dictionary/attributes/e/end/ethnic_category_code_de.asp
Would there be advantages/disadvantages of using either method?
Inline definition of a code system is used when the code system and value set are synonymous - you're inventing codes and saying the value set contains all of them. Places this occurs are when we're defining structural codes for FHIR (ones that we'll be maintaining rather than external organization) or for things like Questionnaires where the codes might be specific to that particular questionnaire. In general, inventing your own code system isn't encouraged because it's less likely people will recognize it. It's better to draw codes from standardized code systems, be those international (like SNOMED, LOINC, ICD9, etc.) or national or even organization-maintained code systems.

is there a generic query language for arbitrary sets independent from a programming language?

I'm looking for a way to define queries on sets independently from a programming language or the kind of sets.
In detail this would be a language definition and implementations for common languages like Java, C++, Python etc.
As commented I'm not looking for a database or any implementation of a set-representation but only a way to define a query for elements from e.g. a std::set/vector a Python set() or any linear structure which can be seen as a set.
A close example would be something like jLinq but without being tied to JSON or javascript and with a well defined string representation.
Of course without knowing the kind of data structure you would have to implement any conditional filter for every problem and every programming language, but the way you construct query strings and how you evaluate them would be clear and you would not have to write parsers.
So what I'd like to write in Java or C++ is something like
q = query()
.created_after("14.03.2010")
.and(contains("hello")
.or(contains("hallo")))
.sort("caption")
or written as a string:
"(created_after("14.03.2010") and ( contains("hello") or contains("hallo"))) sort("caption")"
(this is not thought through - just to show what an interface could look like)
A good example for a different problem would be JSON or XML: clear language definition and parsers/tools for any platform or programming language.
I know this is an old question, but I think I know what you mean and I was actually looking for something similar. What you need is a "search query parser".
I found search-query-parser for nodejs (I'm not the author). Haven't tried it yet but looks promising.The example in the docs is very illustraring, you would receive an input string from the UI
from:hi#retrace.io,foo#gmail.com to:me subject:vacations date:1/10/2013-15/04/2014 photos
And the library would parse it to a structured json object
{
from: ['hi#retrace.io', 'foo#gmail.com'],
to: 'me',
subject: 'vacations',
date: {
from: '1/10/2013',
to: '15/04/2014'
},
text: 'photos'
}
And the from that object you could construct and issue a query command to your database. As you can see it handles lists and ranges. Right away I can't see any boolean operator (AND,OR) but I guess could be easily implemented.
Hope this helps.
RSQL is a good option these days. There are plenty of parsers available and the queries are URL friendly.

Recursive logic for parsing string into complex boolean?

I'm sure this has been done before, I just can't find it.
I need to turn something like, "((A OR B) AND C) OR D" into a database query for an attribute. Specifically I'm using Ruby Sequel. Can anyone point me at an example or utility or something that will keep me from reinventing the wheel?
You can define a grammar using ANTLR and automatically generate a Ruby parser for those type of strings. ANTLR is a parser generator and it allows you to define a grammar for a language (such as a the boolean language that you described).
After parsing, you can specify what actions need to be taken to build the desired data structure (in your case a tree data structure that captures the structure of the query).
This is not particularly a Ruby problem as ANTLR can also generate parsers for other languages. In your case it would produce a Ruby parser that you can integrate into your application to parse the strings and to produce the data structure that you need.

Is Automatic Refactoring Possible in Dynamic Languages?

Perhaps I am limited by my experience with dynamic languages (Ruby on Netbeans and Groovy on Eclipse), but it seems to me that the nature of dynamic languages makes it impossible to refactor (renaming methods, classes, pushing-up, pulling-down, etc.) automatically.
Is it possible to refactor AUTOMATICALLY in any dynamic language (with any IDE/tool)? I am especially interested in Ruby, Python and Groovy, and how the refactoring compares to the 100% automatic refactoring available in all Java IDEs.
Given that automatic refactoring was invented in a dynamic language (Smalltalk), I would have to say "Yes".
In particular, John Brant, Don Roberts and Ralph Johnson developed the Refactoring Browser which is one of the core tools in, for instance, Squeak.
My Google-fu is weak today, but you could try find this paper: Don Roberts, John Brant, and Ralph Johnson, A Refactoring Tool for Smalltalk, "The Theory and Practice of Object Systems", (3) 4, 1997.
Smalltalk does not declare any types. The Refactoring Browser has successfully performed correct refactorings in commercial code since 1995 and is incorporated in nearly all current Smalltalk IDE's. - Don Roberts
Automatic Refactoring was invented in Smalltalk, a highly dynamic language.
And it works like a charm ever since.
You can try yourself in a free Smalltalk version (for instance http://pharo-project.org)
In a dynamic language you can also script refactorings yourself or query the
system. Simple example to get the number of Test classes:
TestCase allSubclasses size
I have wondered the same thing. I'm not a compiler/interpreter writer, but I think the answer will be that it is impossible to get it perfect. However, you can get it correct in most cases.
First, I'm going to change the name "dynamic" language to "interpreted" language which is what I think of with Ruby, Javascript, etc. Interpreted languages tend to take advantage of run-time capabilities.
For instance, most scripting languages allow the following
-- pseudo-code but you get the idea
eval("echo(a)");
I just "ran" a string! You would have to refactor that string also. And will a be a variable or does this language allow you to print the character a without quotes if there is no variable a?
I want to believe this kind of coding is probably the exception and that you will get good refactoring almost all of the time. Unfortunately it seems that when I look through libraries for scripting languages, they get into such exceptions normally and maybe even base their architecture on them.
Or to up the ante a bit:
def functionThatAssumesInputWillCreateX(input)
eval(input)
echo(x)
def functionWithUnknownParms( ... )
eval(argv[1]);
At least when you refactor Java, and change a variable from int to string, you get errors in all the places that were expecting the int still:
String wasInt;
out = 3 + wasInt;
With interpreted languages you will probably not see this until run-time.
Ditto the points about the Refactoring Browser...it is highly effective in Smalltalk. However, I imagine there are certain types of refactoring that would be impossible without type information (whether obtain by explicit type annotation in the language or through some form of type inferencing in a dynamic language is irrelevant). One example: when renaming a method in Smalltalk, it will rename all implementors and senders of that method, which most often is just fine, but is sometimes undesirable. If you had type information on variables, you could scope the rename to just the implementors in the current class hierarchy and all senders when the message is being sent to a variable declared to be of a type in that hierarchy (however, I could imagine scenarios where even with type declaration, that would break down and produce undesirable results).

Resources