gyuque (owner)

Revisions

gist: 102055 Download_button fork
public
Public Clone URL: git://gist.github.com/102055.git
Embed All Files: show embed
Python #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# -*- coding: utf-8 -*-
 
import re
 
class ParseTwneruText(object):
    sleeps = u'寝る 就寝 ねる 寝た ねた'.split()
    wakes = u'起きた 起床 おきた'.split()
    time = re.compile(u'([0-9]{1,2})[:時]([0-9]{1,2})')
    date = re.compile(u'([1-9][0-9]?)日')
 
    @classmethod
    def parse(cls, text):
        for sleep in cls.sleeps:
            if sleep in text:
                mode = 'sleep'
                break
        else:
            for wake in cls.wakes:
                if wake in text:
                    mode = 'wake'
                    break
            else:
                raise ValueError('parse error')
        date = time = None
        match = cls.time.search(text)
        if match:
            time = tuple(map(int, match.group(1, 2)))
        match = cls.date.search(text)
        if match:
            date = int(match.group(1))
        return date, time, mode
 
# parse = ParseTwneruText.parse
 
# print parse(u'13日の寝た時刻を24:00に')
# print parse(u'寝た時刻を24時00に')
# print parse(u'寝た')
 
 
# (13, (24, 0), 'sleep')
# (None, (24, 0), 'sleep')
# (None, None, 'sleep')