eventual
DESCRIPCIÓN:
Reconocimiento de fechas y periodos en lenguaje natural. Útil para crear interfaces de usuario basadas en texto.
SINOPSIS:
El método event_parse del modulo Eventual reconoce y convierte una fecha o periodo expresado en lenguaje natural en objetos Date o DateTime Ejemplos:
require 'rubygems'
require 'eventual'
Eventual.event_parse( 'del 5 al 7 de junio' )
=> [#<DateTime: 4909975/2,0,2299161>, #<DateTime: 4909977/2,0,2299161>, #<DateTime: 4909979/2,0,2299161>]
Eventual.event_parse( 'del 5 al 7 de junio 2009', Date )
=> [#<Date: 4909975/2,0,2299161>, #<Date: 4909977/2,0,2299161>, #<Date: 4909979/2,0,2299161>]
Eventual.event_parse( 'del 5 al 7 de junio del 2009 a las 16:00 y 18:00 horas' ){ |d| d.to_s }
=> ["2009-06-05T16:00:00+00:00", "2009-06-05T18:00:00+00:00", "2009-06-06T16:00:00+00:00", "2009-06-06T18:00:00+00:00", "2009-06-07T16:00:00+00:00", "2009-06-07T18:00:00+00:00"]
Eventual.event_parse( 'del 5 al 7 de junio 2009' ){ |d| Eventual::WDAY_LIST[ d.wday ] }
=> ['viernes', 'sabado', 'domingo']
Ejemplos de formatos reconocidos:
-
1 de enero
-
21, 22 y 23 de enero del 2009
-
21, 22 y 23 de enero a las 20:00 horas
-
21, 22 y 23 de enero a las 20:00 y 22:00 horas
-
martes y miércoles del 1 al 20 de junio del 2009 a las 16:00 y 18:00 horas
Se puede extender Date y DateTime con Eventual pero en principio no extienden Eventual. Para activar el parche:
require 'rubygems'
require 'eventual'
require 'eventual/date_time'
require 'eventual/date'
DateTime.event_parse( 'del 5 al 7 de junio' )
=> [#<DateTime: 4909975/2,0,2299161>, #<DateTime: 4909977/2,0,2299161>, #<DateTime: 4909979/2,0,2299161>]
Date.event_parse( 'del 5 al 7 de junio 2009' )
=> [#<Date: 4909975/2,0,2299161>, #<Date: 4909977/2,0,2299161>, #<Date: 4909979/2,0,2299161>]
Si se pasa un bloque se puede especificar si se desea usar el texto que sigue a la definición de fechas mediante pasando la opción use_trailing => true:
str = "1 de enero del 2009\nSede:El tercer lugar\n2 de enero del 2009\nSede:El tercer lugar"
Eventual.event_parse( str, :use_trailing => true ) do |dia, texto_extra|
[dia.to_s, texto_extra]
end
=> [["2009-01-01T00:00:00+00:00", "Sede:El tercer lugar"], ["2009-01-02T00:00:00+00:00", "Sede:El tercer lugar"]]
TODO:
-
No estoy seguro de que Iconv funcione en windows, lo arreglaré pronto
Formatos a reconocer
-
todos los lunes de junio
-
domingos de septiembre
-
martes y miércoles de agosto
-
todo el año
-
todo junio
INSTALACIÓN:
sudo gem install maca-eventual -s http://gems.github.com
LICENCIA:
(The MIT License)
Copyright © 2009 Macario Ortega
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.