-
Notifications
You must be signed in to change notification settings - Fork 2
/
summarizer.go
112 lines (91 loc) · 3.05 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
package goSummarizer
import (
"errors"
"goSummarizer/helpers"
)
// Summarizer instance, used for extracting summary from raw texts and urls
type Summarizer struct {
url string
title string
fullText string
summarizedText string
images []string
summarized bool
}
// CreateFromURL creates summarizer instance, using the url parameter for summarizing
func CreateFromURL(url string) *Summarizer {
var summarizer = new(Summarizer)
summarizer.url = url
return summarizer
}
// CreateFromText creates summarizer instance, using the text parameter for summarizing
func CreateFromText(text string) *Summarizer {
var summarizer = new(Summarizer)
summarizer.fullText = text
return summarizer
}
// Summarize returns summary of the text, extracted from the url or the saved text
func (s *Summarizer) Summarize() (string, error) {
if s.IsSummarized() {
return s.summarizedText, nil
}
if s.fullText == "" && s.url == "" {
return "", errors.New("You must submit text or url for summarizing")
}
if s.url != "" {
s.GetMainTextFromURL()
}
var summarizedText = s.summarizeFromText()
if len(summarizedText) == 0 {
return "", errors.New("Something happened while summarizing. Please try again")
}
s.summarizedText = summarizedText
s.summarized = true
if s.title != "" {
return s.title + "\n\n" + s.summarizedText, nil
}
return s.summarizedText, nil
}
// GetMainTextFromURL parses the summarizer object URL property and returns the main text
// from the website without ads, unnecessary images and other not important data
func (s *Summarizer) GetMainTextFromURL() (string, error) {
if s.url == "" {
return "", errors.New("You must use summarizer from URL")
}
if s.fullText != "" {
return s.fullText, nil
}
extractedTitle, extractedText, extractedImages, err := helpers.ExtractMainInfoFromURL(s.url)
if err != nil {
return "", err
}
s.title = extractedTitle
s.fullText = extractedText
s.images = extractedImages
return extractedTitle + "\n\n" + extractedText, nil
}
func (s *Summarizer) summarizeFromText() string {
// Build the summary with the sentences dictionary
var summary = helpers.GetSummary(s.fullText)
return summary
}
// GetSummaryInfo returns summary information statistics if the text is summarized and an error if not
func (s *Summarizer) GetSummaryInfo() (string, error) {
if !s.IsSummarized() {
return "", errors.New("You must first summarize the text in order to get information for it")
}
var summaryInfo = helpers.GetSummaryInfo(s.fullText, s.summarizedText, len(s.images))
return summaryInfo, nil
}
// IsSummarized checks if the instance was already summarized
func (s *Summarizer) IsSummarized() bool {
return s.summarized
}
// StoreToFile stores the summarized text to the file from the given path
func (s *Summarizer) StoreToFile(filePath string) (bool, error) {
if !s.IsSummarized() {
return false, errors.New("You must first summarize the text in order to save the summary to a file")
}
stored, err := helpers.StoreTextToFile(filePath, s.title, s.summarizedText, s.images)
return stored, err
}