-
Notifications
You must be signed in to change notification settings - Fork 0
/
summarizer.go
463 lines (400 loc) · 12.4 KB
/
summarizer.go
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
package Posger
import (
"bufio"
"io"
"os"
"strconv"
"strings"
"fmt"
"log"
"gonum.org/v1/gonum/mat"
"math"
"sort"
"github.com/gorilla/mux"
"net/http"
"encoding/json"
"github.com/satori/go.uuid"
"bytes"
"io/ioutil"
)
var (
articleCount int64
wordDictionary = make(map[string]int)
)
func init() {
idfFile, _ := os.Open("tools/idf.dict")
defer idfFile.Close()
buffer := bufio.NewReader(idfFile)
line, _ := buffer.ReadString('\n')
articleCount, _ = strconv.ParseInt(line[:len(line)-1], 10, 32)
for {
line, err := buffer.ReadString('\n')
if err != nil || io.EOF == err {
break
}
res := strings.Split(line[:len(line)-1], " ")
idf, _ := strconv.ParseInt(res[1], 10, 32)
wordDictionary[res[0]] = int(idf)
}
log.Println("wordDict loading end, word count is " + fmt.Sprintf("%d", articleCount))
}
// PathPrefix is api/digest, uploading files api and so on.
func registeSummaryApi(router *mux.Router) {
router.HandleFunc("/", digestTextApi).Methods("POST")
// get paper list by logined user
router.HandleFunc("/paper", getPaperApi).Methods("GET")
// get paper layout data by paperId
router.HandleFunc("/paper/{paperId}", layoutPaperApi).Methods("GET")
router.HandleFunc("/paper", uploadPaperApi).Methods("POST")
router.HandleFunc("/paper/{paperId}", delPaperApi).Methods("DELETE")
}
func getPaper(username string) ([]byte){
res, _ := json.Marshal(struct {
Meta string
Data []Paper `json:"data"`
}{Data: SelectPaper(map[string]interface{}{"owner": username})})
return res
}
func getPaperApi(w http.ResponseWriter, r *http.Request) {
RequireLoginApi(w, r, getPaper, "")
}
func digestTextApi(w http.ResponseWriter, r *http.Request) {
body, _ := ioutil.ReadAll(r.Body)
info := &struct{
Content string `json:"content"`
}{}
json.Unmarshal(body, &info)
article := &Article{Sentences: make([]Sentence, 0)}
article.segmentation(info.Content)
io.Copy(w, bytes.NewReader(Must(json.Marshal(struct {
Msg string `json:"msg"`
Result []string `json:"summary"`
}{"Nice Request!", article.Summary()})).([]byte)))
}
// get Paper layout meta data Api
func layoutPaperApi(w http.ResponseWriter, r *http.Request) {
paperId, error_msg := mux.Vars(r)["paperId"], ""
if ps := SelectPaper(map[string]interface{}{"paperid": paperId}); len(ps) != 0 {
article, err := NewJsonArticle("static/articles/" + paperId)
if err != nil {
error_msg = "The files uploaded error, " + ps[0].Name
} else {
io.Copy(w, bytes.NewReader(Must(json.Marshal(article)).([]byte)))
return
}
} else {
error_msg = "Invalid paper ID."
}
returnJson, _ := json.Marshal(struct {
Msg string `json:"error"`
ID string `json:"paper_id"`
}{error_msg, paperId})
io.Copy(w, bytes.NewReader(returnJson))
}
// Upload pdf files to the server
// Warning: If you have logged in the system, You can save the files, and save the relationships to the database
// If not, only detect the result poster once.
func uploadPaperApi(w http.ResponseWriter, r *http.Request) {
up_file, hd, err := r.FormFile("digest-upload[]")
if err != nil {
http.Error(w, err.Error(), 500)
return
}
defer up_file.Close()
id := uuid.Must(uuid.NewV4()).String() // paper's unique id
path := "static/articles/" + id
new_f, err := os.Create(path)
if err != nil {
Logger.Println("Create File failed, path is ", path)
}
defer new_f.Close()
io.Copy(new_f, up_file) // copy content to the new fiel
// Check login
if users := SelectUser(map[string]interface{}{"username": isLogin(r)}); len(users) != 0 {
go AddPaper(Paper{PaperId: id, Owner: users[0].UserId, Path: path, Name: hd.Filename})
}
returnJson, _ := json.Marshal(struct {
Url string `json:"url"`
Key string `json:"id"`
}{"/api/digest/paper", id})
io.Copy(w, bytes.NewReader(returnJson))
}
func delPaper(paperId string) []byte {
DeletePaper(map[string]interface{}{"paperid": paperId})
res, _ :=json.Marshal(struct {
Msg string `json:"msg"`
}{"deleteSuccess"})
return res
}
func delPaperApi(w http.ResponseWriter, r *http.Request) {
RequireLoginApi(w, r, delPaper, mux.Vars(r)["paperId"])
}
type baseArticle struct {
Abstract string // summary by author, abstract in paper
Keywords []string // keywords in paper
References []string // multi references
Title string // article's title
}
type jsonArticle struct {
baseArticle
Digest []string // summary Content
}
func NewJsonArticle(filepath string) (*jsonArticle, error) {
article, err := NewArticle(filepath)
if err != nil {
return nil, err
}
return &jsonArticle{baseArticle: article.baseArticle, Digest: article.Summary()}, nil
}
// NewArticle use filepath parameter to construct a
// segmented article
func NewArticle(filepath string) (*Article, error) {
article := &Article{Sentences: make([]Sentence, 0), filepath: filepath}
content, err := ExtractDocumentContent(filepath)
if err != nil {
return nil, err
}
residual := article.setMeta(content)
article.segmentation(residual)
return article, nil
}
// segmentation will segment an article to
// an array of multi sentences
// Warning: Now Only Chinese
func (self *Article) segmentation(content string) {
jiebaSentenceSegmentation(self, content)
}
// By analysising the rules, we can get the infomation
// i.e. author, title, abstract, reference, acknowledge
func (self *Article) setMeta(content string) (c string){
// Consider the condition that we can't find below "keywords"
sum_s, sum_e := self.getSummaryIndex(content)
self.setSummaryIndex(content[sum_s:sum_e])
self.setTitleAndAuthor(content[:sum_s])
content = content[sum_e:]
key_s, key_e := self.getKeywordsIndex(content[:])
self.setKeywords(content[key_s:key_e])
sum_s, sum_e = self.getEnglishAbstractIndex(content)
self.setEnglishAbstract(content[sum_s:sum_e])
content = content[sum_e:]
// 如果存在英文摘要,那么一定存在英文关键词
if sum_s != 0 {
key_s, key_e = self.getKeywordsIndex(content[:])
self.setKeywords(content[key_s:key_e])
}
ref_s, ref_e := self.getReferenceIndex(content)
self.setReferenceIndex(content[ref_s:ref_e])
return content[key_e: ref_s]
}
func (self *Article) getSummaryIndex(content string) (s, e int) {
digest_s := 0
if digest_s = strings.Index(content, "摘 要"); digest_s == -1 {
digest_s = strings.Index(content, "摘要")
}
s = strings.LastIndex(content[:digest_s], "\n") // 或者“摘要”
e = strings.LastIndex(content[:strings.Index(content, "关键词")], "\n") + 1
return
}
func (self *Article) setSummaryIndex(abstract string) {
self.Abstract = strings.Replace(abstract, "\n", "", -1)
}
func (self *Article) setTitleAndAuthor(content string) {
self.baseArticle.Title = content
}
func (self *Article) getEnglishAbstractIndex(content string) (s, e int){
if strings.Index(content, "Abstract") != -1 {
s = strings.LastIndex(content[:strings.Index(content, "Abstract")], "\n") // 或者“摘要”
if strings.Index(content, "Keywords") != -1 {
e = strings.LastIndex(content[:strings.Index(content, "Keywords")], "\n") + 1
} else {
e = strings.LastIndex(content[:strings.Index(content, "Key words")], "\n") + 1
}
}
return
}
func (self *Article) setEnglishAbstract(s string) {
self.Abstract += "\n" + s
}
func (self *Article) getKeywordsIndex(content string) (s, e int) {
e = strings.Index(content[:], "\n")
return
}
func (self *Article) setKeywords(keywords string) {
self.Keywords = append(self.Keywords, strings.Split(keywords, " ")...)
}
func (self *Article) getReferenceIndex(content string) (s, e int) {
s = strings.LastIndex(content[:strings.LastIndex(content, "参考文献")], "\n") + 1
e = len(content)
return
}
func (self *Article) setReferenceIndex(reference string) {
// First is 参考文献
// By [1] [2] ... rule consists an array.
var pos []int; res := strings.Split(reference, "\n")
for i, r := range res[1:] {
for _, f := range []string{"[%d]", "[%d】", "【%d]", "【%d】", "[ %d ]"} {
if strings.HasPrefix(r, fmt.Sprintf(f, len(pos) + 1)) {
pos = append(pos, i)
}
}
}
pos = append(pos, len(res))
for i, p := range pos[:len(pos)-1] {
self.References = append(self.References, strings.Join(purifyContent(res[p:pos[i+1]]...), ""))
}
}
type Sentence struct {
Words []string
Point float64
}
type Article struct {
baseArticle
filepath string
Sentences []Sentence
}
func getWordIdf(word string) float64 {
idf := math.Log((float64(articleCount) + 0.5) / (float64(wordDictionary[word]) + 0.5))
return idf
}
const SUMMARY_ADJUST = 0.85
// Summarizer belongs to Article
// through similarity matrix, we
// can get the score matrix eventually.
func (self *Article) Summary() []string {
const ITER = 10
n := len(self.Sentences)
similarityMatrix := self.similarityMatrix()
PR := mat.NewDense(n, 1, make([]float64, n))
for c := 0; c < ITER; c++ {
// PR = 0.15 + 0.85 * M * PR
// and similarityMatrix is 0.85*M
MmulPR := new(mat.Dense)
MmulPR.Mul(similarityMatrix, PR)
PR.Add(wholeMatrix(n, 1, 1-SUMMARY_ADJUST), MmulPR)
// if converagence, break, the threshold is 0.0001
}
resultMap := sortSentences(PR)
result := make([]string, n)
for i, v := range *resultMap {
// assemble all the words, append to the result.
result[i] = connectSliceWords(self.Sentences[v.Key].Words...)
}
return purifyContent(result...)
}
func sortSentences(PR *mat.Dense) *ByPoint {
// sort sentences points
result_map := make(ByPoint, len(PR.RawMatrix().Data))
for p, idf := range PR.RawMatrix().Data {
result_map[p] = PointMap{p, idf}
}
sort.Sort(sort.Reverse(result_map))
return &result_map
}
func connectSliceWords(words ...string) string {
var result string
for _, word := range words {
result += word + " "
}
return result
}
// simlarityMatrix is a score matrix by BM25 alorgithm
// it will record every dual sentences similarity scores.
func (self *Article) similarityMatrix() *mat.Dense {
n := len(self.Sentences)
data := make([]float64, n*n)
for i1, s1 := range self.Sentences {
s1 := s1
for i2, s2 := range self.Sentences {
s2 := s2
if i1 == i2 {
// diagonal Set Zero
data[i1*n+i2] = 0
} else {
data[i1*n+i2] = s1.BM25(&s2) * SUMMARY_ADJUST
}
}
}
return mat.NewDense(n, n, data)
}
// BM25 caculates the similarity score of two sentences.
func (self *Sentence) BM25(s *Sentence) float64 {
const k1, b, avsl = 2.0, 0.75, 5.0
var score float64
for _, word := range self.Words {
var tf int
for _, wordc := range s.Words {
if word == wordc {
tf += 1
}
}
var B float64 = (1 - b) + b*(float64(len(s.Words))/avsl)
score += getWordIdf(word) * (float64(tf*(k1+1)) / (float64(tf) + k1*B))
}
return score
}
// wholeMatrix will return a matrix(mat.Dense)
// that all member is "num" parameter.
// Its size is designited by raw and col.
func wholeMatrix(raw int, col int, num float64) *mat.Dense {
data := make([]float64, raw*col)
for i, _ := range data {
data[i] = num
}
return mat.NewDense(raw, col, data)
}
type PointMap struct {
Key int //Sentence Position(Index)
Value float64 //Sentence Points
}
type ByPoint []PointMap
func (idf ByPoint) Len() int { return len(idf) }
func (idf ByPoint) Swap(i, j int) { idf[i], idf[j] = idf[j], idf[i] }
func (idf ByPoint) Less(i, j int) bool { return idf[i].Value < idf[j].Value }
// We also need keyword.
// keyword
// keyword
// keyword
func (self *Article) GetKeyWords() []string {
var resultMap = make(ByWordIdf, 0)
tokenNum, statistics := self.TokenFrequencyStat()
for _, sentence := range self.Sentences {
for _, word := range sentence.Words {
if isKeywordMapExist(&resultMap, word) {
continue
} // Later judge stop words
resultMap = append(resultMap, WordIdfMap{word, getWordIdf(word) * float64(statistics[word]) / float64(tokenNum)})
}
}
sort.Sort(sort.Reverse(resultMap))
result := make([]string, 0)
for _, v := range resultMap {
result = append(result, v.Word)
}
return result
}
func (self *Article) TokenFrequencyStat() (wordCount int, countMap map[string]int) {
countMap = make(map[string]int)
for _, sentence := range self.Sentences {
for _, word := range sentence.Words {
countMap[word]++
wordCount++
}
}
return
}
func isKeywordMapExist(m *ByWordIdf, word string) bool {
for _, idfs := range *m {
if word == idfs.Word {
return true
}
}
return false
}
type WordIdfMap struct {
Word string // Word Content
Value float64 // Word's IDF Point
}
type ByWordIdf []WordIdfMap
func (idf ByWordIdf) Len() int { return len(idf) }
func (idf ByWordIdf) Swap(i, j int) { idf[i], idf[j] = idf[j], idf[i] }
func (idf ByWordIdf) Less(i, j int) bool { return idf[i].Value < idf[j].Value }