11
11
//===----------------------------------------------------------------------===//
12
12
13
13
import Foundation
14
+ #if os(macOS)
15
+ import NaturalLanguage
16
+ #endif
14
17
import SwiftSyntax
15
18
16
19
/// All documentation comments must begin with a one-line summary of the declaration.
@@ -125,19 +128,31 @@ public final class BeginDocumentationCommentWithOneLineSummary: SyntaxLintRule {
125
128
}
126
129
127
130
var sentences = [ String] ( )
131
+ var tags = [ NLTag] ( )
128
132
var tokenRanges = [ Range < String . Index > ] ( )
129
- let tags = text. linguisticTags (
133
+
134
+ let tagger = NLTagger ( tagSchemes: [ . lexicalClass] )
135
+ tagger. string = text
136
+ tagger. enumerateTags (
130
137
in: text. startIndex..< text. endIndex,
131
- scheme: NSLinguisticTagScheme . lexicalClass. rawValue,
132
- tokenRanges: & tokenRanges)
138
+ unit: . word,
139
+ scheme: . lexicalClass
140
+ ) { tag, range in
141
+ if let tag {
142
+ tags. append ( tag)
143
+ tokenRanges. append ( range)
144
+ }
145
+ return true
146
+ }
147
+
133
148
var isInsideQuotes = false
134
149
let sentenceTerminatorIndices = tags. enumerated ( ) . filter {
135
- if $0. element == " OpenQuote " {
150
+ if $0. element == NLTag . openQuote {
136
151
isInsideQuotes = true
137
- } else if $0. element == " CloseQuote " {
152
+ } else if $0. element == NLTag . closeQuote {
138
153
isInsideQuotes = false
139
154
}
140
- return !isInsideQuotes && $0. element == " SentenceTerminator "
155
+ return !isInsideQuotes && $0. element == NLTag . sentenceTerminator
141
156
} . map {
142
157
tokenRanges [ $0. offset] . lowerBound
143
158
}
@@ -158,8 +173,8 @@ public final class BeginDocumentationCommentWithOneLineSummary: SyntaxLintRule {
158
173
/// Returns the best approximation of sentences in the given text using string splitting around
159
174
/// periods that are followed by spaces.
160
175
///
161
- /// This method is a fallback for platforms (like Linux, currently) where `String` does not
162
- /// support `NSLinguisticTagger ` and its related APIs. It will fail to catch certain kinds of
176
+ /// This method is a fallback for platforms (like Linux, currently) that does not
177
+ /// support `NaturalLanguage ` and its related APIs. It will fail to catch certain kinds of
163
178
/// sentences (such as those containing abbreviations that are followed by a period, like "Dr.")
164
179
/// that the more advanced API can handle.
165
180
private func nonLinguisticSentenceApproximations( in text: String ) -> (
0 commit comments