8
8
import Foundation
9
9
10
10
public class TextRank {
11
- public var text : String {
11
+ public var pages : [ String ] = [ String ] ( ) {
12
12
didSet {
13
13
textToSentences ( )
14
14
}
@@ -25,26 +25,35 @@ public class TextRank {
25
25
}
26
26
27
27
public init ( ) {
28
- text = " "
28
+ pages = [ " " ]
29
29
graph = TextGraph ( damping: graphDamping)
30
30
}
31
31
32
+ public init ( pages: [ String ] ) {
33
+ self . pages = pages
34
+ graph = TextGraph ( damping: graphDamping)
35
+ textToSentences ( )
36
+ }
37
+
32
38
public init ( text: String ) {
33
- self . text = text
39
+ self . pages = [ text]
34
40
graph = TextGraph ( damping: graphDamping)
35
41
textToSentences ( )
36
42
}
37
43
38
44
public init ( text: String , summarizationFraction: Float = 0.2 , graphDamping: Float = 0.85 ) {
39
- self . text = text
45
+ self . pages = [ text]
40
46
self . summarizationFraction = summarizationFraction
41
47
self . graphDamping = graphDamping
42
48
graph = TextGraph ( damping: graphDamping)
43
49
textToSentences ( )
44
50
}
45
51
46
52
func textToSentences( ) {
47
- sentences = TextRank . splitIntoSentences ( text, additionalStopwords: stopwords) . filter { $0. length > 0 }
53
+ sentences = [ ] ;
54
+ for (pageIndex, page) in pages. enumerated ( ) {
55
+ sentences. append ( contentsOf: TextRank . splitIntoSentences ( page, pageIndex: pageIndex, additionalStopwords: stopwords) . filter { $0. length > 0 } )
56
+ }
48
57
}
49
58
}
50
59
@@ -87,7 +96,7 @@ extension TextRank {
87
96
/// Split text into sentences.
88
97
/// - Parameter text: Original text.
89
98
/// - Returns: An array of sentences.
90
- static func splitIntoSentences( _ text: String , additionalStopwords stopwords: [ String ] = [ String] ( ) ) -> [ Sentence ] {
99
+ static func splitIntoSentences( _ text: String , pageIndex : Int = 0 , additionalStopwords stopwords: [ String ] = [ String] ( ) ) -> [ Sentence ] {
91
100
if text. isEmpty { return [ ] }
92
101
93
102
var x = [ Sentence] ( )
@@ -96,6 +105,7 @@ extension TextRank {
96
105
x. append (
97
106
Sentence ( text: substring. trimmingCharacters ( in: . whitespacesAndNewlines) ,
98
107
originalTextIndex: x. count,
108
+ pageIndex: pageIndex,
99
109
additionalStopwords: stopwords)
100
110
)
101
111
}
0 commit comments