@@ -10,14 +10,19 @@ import Foundation
1010public class TextRank {
1111 public var text : String {
1212 didSet {
13- sentences = TextRank . splitIntoSentences ( text ) . filter { $0 . length > 0 }
13+ textToSentences ( )
1414 }
1515 }
1616
17- public var summarizationFraction : Float = 0.2
1817 public var graph : TextGraph
19- public var graphDamping : Float = 0.85
2018 public var sentences = [ Sentence] ( )
19+ public var summarizationFraction : Float = 0.2
20+ public var graphDamping : Float = 0.85
21+ public var stopwords = [ String] ( ) {
22+ didSet {
23+ textToSentences ( )
24+ }
25+ }
2126
2227 public init ( ) {
2328 text = " "
@@ -26,16 +31,20 @@ public class TextRank {
2631
2732 public init ( text: String ) {
2833 self . text = text
29- sentences = TextRank . splitIntoSentences ( text) . filter { $0. length > 0 }
3034 graph = TextGraph ( damping: graphDamping)
35+ textToSentences ( )
3136 }
3237
3338 public init ( text: String , summarizationFraction: Float = 0.2 , graphDamping: Float = 0.85 ) {
3439 self . text = text
3540 self . summarizationFraction = summarizationFraction
3641 self . graphDamping = graphDamping
37- sentences = TextRank . splitIntoSentences ( text) . filter { $0. length > 0 }
3842 graph = TextGraph ( damping: graphDamping)
43+ textToSentences ( )
44+ }
45+
46+ func textToSentences( ) {
47+ sentences = TextRank . splitIntoSentences ( text, additionalStopwords: stopwords) . filter { $0. length > 0 }
3948 }
4049}
4150
@@ -78,13 +87,17 @@ extension TextRank {
7887 /// Split text into sentences.
7988 /// - Parameter text: Original text.
8089 /// - Returns: An array of sentences.
81- static func splitIntoSentences( _ text: String ) -> [ Sentence ] {
90+ static func splitIntoSentences( _ text: String , additionalStopwords stopwords : [ String ] = [ String ] ( ) ) -> [ Sentence ] {
8291 if text. isEmpty { return [ ] }
8392
8493 var x = [ Sentence] ( )
8594 text. enumerateSubstrings ( in: text. range ( of: text) !, options: [ . bySentences, . localized] ) { substring, _, _, _ in
8695 if let substring = substring, !substring. isEmpty {
87- x. append ( Sentence ( text: substring. trimmingCharacters ( in: . whitespacesAndNewlines) , originalTextIndex: x. count) )
96+ x. append (
97+ Sentence ( text: substring. trimmingCharacters ( in: . whitespacesAndNewlines) ,
98+ originalTextIndex: x. count,
99+ additionalStopwords: stopwords)
100+ )
88101 }
89102 }
90103 return Array ( Set ( x) )
@@ -101,15 +114,12 @@ public extension TextRank {
101114 func filterTopSentencesFrom( _ results: TextGraph . PageRankResult , top percentile: Float ) -> TextGraph . NodeList {
102115 let idx = Int ( Float ( results. results. count) * percentile)
103116 let cutoffScore : Float = results. results. values. sorted ( ) [ min ( idx, results. results. count - 1 ) ]
104-
105117 var filteredNodeList : TextGraph . NodeList = [ : ]
106-
107118 for (sentence, value) in results. results {
108119 if value >= cutoffScore {
109120 filteredNodeList [ sentence] = value
110121 }
111122 }
112-
113123 return filteredNodeList
114124 }
115125}
0 commit comments