forked from dylanninin/blog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tool.py
202 lines (168 loc) · 6.03 KB
/
tool.py
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/env python
"""ToolKit.
version 1.0
history:
2013-6-19 [email protected] init
2013-11-23 [email protected] update tags, categories
"""
# -*- coding: utf-8 -*-
import yaml
class Extract:
"""Extrace tool."""
def __init__(self):
"""
#TODO: FIXME: init
"""
self.stop_words = []
self.categories = ['Web', 'Linux', 'Database', 'Development']
def auto_keyphrase(self, entry):
"""
#TODO: FIXME: extract keyphrase automaticly
reference:
http://www.ruanyifeng.com/blog/2013/03/tf-idf.html
"""
return self.categories
def auto_categories(self, entry):
"""
#TODO: FIXME: extract categories automaticly
"""
return self.categories
def auto_summarization(self, entry):
"""
#TODO: FIXME: extract summarization automaticly
reference:
http://www.ruanyifeng.com/blog/2013/03/automatic_summarization.html
"""
return entry.content[:200]
def auto_similiarities(self, entry, entries):
"""
#TODO: FIXME: extract similiarities automaticly
reference:
http://www.ruanyifeng.com/blog/2013/03/cosine_similarity.html
"""
return entries
def parse(self, entry):
"""
parse the raw content of a markdown entry
TODO: FIXME ... ...
args:
filename: the filename of a markdown entry
return:
a tuple like (yaml_header, title, categories, tags)
the content will be preprocessed if it does have a yaml header declaration.
yaml header field options:
title
category or categories
tags
blog example:
---
title: the title, default None if it's empty
category: category, default Uncategorised if it's empty.
tags: [tag1, tag2], default [Untagged] if it's empty.
---
##header
the content of the blog
blah blah ...
... ...
reference:
http://jekyllrb.com/docs/frontmatter/
"""
seperators = ['---\n', '---\r\n']
newlines = ['\n', '\r\n']
title = None
categories = ['Uncategorised']
tags = ['Untagged']
number = 4
yml = []
header = ''
with open(entry.path, 'r') as f:
first = f.readline()
if first in seperators:
count, line = 1, f.readline()
while count <= number and not line in seperators:
yml.append(line)
line = f.readline()
count += 1
if len(yml) == 0 or not line in seperators or not f.readline() in newlines:
msg = 'Error, YAML header declaration with %s does not match in %s ' % (seperators, entry.path)
raise Exception(msg)
skip = count + 2
f.seek(0)
header = ''.join([f.readline() for i in xrange(skip)])
yml = ''.join(yml)
if not yml == '':
y = yaml.load(yml)
title = y.get('title') or title
categories = y.get('categories') or categories
category = y.get('category')
if not category == None:
categories = [category]
tags = y.get('tags') or tags
return header, title, categories, tags
class Dict2Object(dict):
"""
dict to object
so you can access like a.attribute but not a['attribute']
reference:
http://stackoverflow.com/questions/1305532/convert-python-dict-to-object
"""
def __init__(self, data=None):
super(Dict2Object, self).__init__()
if data:
self.__update(data, {})
def __update(self, data, did):
dataid = id(data)
did[dataid] = self
for k in data:
dkid = id(data[k])
if did.has_key(dkid):
self[k] = did[dkid]
elif isinstance(data[k], Dict2Object):
self[k] = data[k]
elif isinstance(data[k], dict):
obj = Dict2Object()
obj.__update(data[k], did)
self[k] = obj
obj = None
else:
self[k] = data[k]
def __getattr__(self, key):
return self.get(key, None)
def __setattr__(self, key, value):
if isinstance(value, dict):
self[key] = Dict2Object(value)
else:
self[key] = value
def update(self, *args):
for obj in args:
for k in obj:
if isinstance(obj[k], dict):
self[k] = Dict2Object(obj[k])
else:
self[k] = obj[k]
return self
def merge(self, *args):
for obj in args:
for k in obj:
if self.has_key(k):
if isinstance(self[k], list) and isinstance(obj[k], list):
self[k] += obj[k]
elif isinstance(self[k], list):
self[k].append(obj[k])
elif isinstance(obj[k], list):
self[k] = [self[k]] + obj[k]
elif isinstance(self[k], Dict2Object) and isinstance(obj[k], Dict2Object):
self[k].merge(obj[k])
elif isinstance(self[k], Dict2Object) and isinstance(obj[k], dict):
self[k].merge(obj[k])
else:
self[k] = [self[k], obj[k]]
else:
if isinstance(obj[k], dict):
self[k] = Dict2Object(obj[k])
else:
self[k] = obj[k]
return self
if __name__ == '__main__':
import doctest
doctest.testmod()