An open API service indexing awesome lists of open source software.

https://github.com/flight-school/guide-to-swift-strings-sample-code

Xcode Playground Sample Code for the Flight School Guide to Swift Strings
https://github.com/flight-school/guide-to-swift-strings-sample-code

antlr4 binary-to-text nlp parser regex strings swift unicode

Last synced: 3 months ago
JSON representation

Xcode Playground Sample Code for the Flight School Guide to Swift Strings

Awesome Lists containing this project

README

          


Flight School Guide to Swift Strings Cover

# Guide to Swift Strings Sample Code

[![Build Status][build status badge]][build status]
[![License][license badge]][license]
[![Swift Version][swift version badge]][swift version]

This repository contains sample code used in the
[Flight School Guide to Swift Strings](https://flight.school/books/strings).

---

- [Chapter 2: Working with Strings in Swift](#chapter-2)
- [Chapter 3: Swift String Protocols and Supporting Types](#chapter-3)
- [Chapter 4: Working with Foundation String APIs](#chapter-4)
- [Chapter 5: Binary-to-Text Encoding](#chapter-5)
- [Chapter 6: Parsing Data From Text](#chapter-6)
- [Chapter 7: Natural Language Processing](#chapter-7)

---

## Chapter 2

### String Literals

You can construct string values in Swift using string literals.
This Playground has examples of each variety,
from the conventional, single-line to the raw, multi-line.

```swift
let multilineRawString = #"""
\-----------------------\
\ \
\ ___ \
\ (_ /'_ /_/ \ __
\ / (/(//)/ \ | \
> _/ >------| \ ______
/ __ / --- \_____/**|_|_\____ |
/ ( _ / / / \_______ --------- __>-}
/ __)( /)()()( / / \_____|_____/ |
/ / * |
/-----------------------/ {o}
"""#
```

### String Indexes

Swift strings have opaque index types.
One consequence of this is that
you can't access character by integer position directly,
as you might in other languages.
This Playground shows various strategies for
working with string indices and ranges.

```swift
let string = "Hello"

string[string.startIndex] // "H"
string[string.index(after: string.startIndex)] // "e"
string[string.index(string.startIndex, offsetBy: 4)] // "o"
```

### Canonical Equivalence

In Swift,
two `String` values are considered equal if they are
[canonically equivalent](http://unicode.org/notes/tn5/),
even if they comprise different Unicode scalar values.

```swift
let precomposed = "expos\u{00E9}" // Γ© LATIN SMALL LETTER E WITH ACUTE
let decomposed = "expose\u{0301}" // Β΄ COMBINING ACUTE ACCENT

precomposed == decomposed
precomposed.elementsEqual(decomposed) // true

precomposed.unicodeScalars.elementsEqual(decomposed.unicodeScalars) // false
```

### Unicode Views

Swift `String` values
provide views to their UTF-8, UTF-16, and UTF-32 code units.
This Playground shows the correspondence between
the characters in a string and their various encoding forms.

```swift
let string = "東京 πŸ‡―πŸ‡΅"
for unicodeScalar in character.unicodeScalars {
print(unicodeScalar.codePoint, terminator: "\t")
}
```

### Character Number Values

In Swift 5,
you can access several Unicode properties of `Character` values,
which allow you to determine things like
Unicode general category membership,
whether a character has case mapping (lowercase / uppercase / titlecase),
and whether the character has an associated number value.

```swift
// U+2460 CIRCLED DIGIT ONE
("β‘ " as Character).isNumber // true
("β‘ " as Character).isWholeNumber // true
("β‘ " as Character).wholeNumberValue // 1
```

### Emoji Detection

For more direct access to the aforementioned character information,
you can do so through the `properties` property on `Unicode.Scalar` values.
For example,
the `isEmoji` property does...
well, exactly what you'd expect it to do.

```swift
("πŸ‘" as Unicode.Scalar).properties.isEmoji // true
```

## Chapter 3

### String as **\*\***\_\_\_**\*\***

In Swift,
`String` functionality is inherited from
a complex hierarchy of interrelated protocols,
including
`Sequence`,
`Collection`,
`BidirectionalCollection`,
`RangeReplaceableCollection`,
`StringProtocol`,
and others.

Each of the protocols mentioned has their own Playground
demonstrating the specific functionality they provide.

```swift
"Boeing 737-800".filter { $0.isCased }
.map { $0.uppercased() }
["B", "O", "E", "I", "N", "G"]
```

### Unicode Logger

The `print` function can direct its output
to a custom type conforming to the `TextOutputStream` protocol.
This example implements a logger
that prints the Unicode code points of the provided string.

```swift
var logger = UnicodeLogger()
print("πŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘§", to: &logger)

// 0: πŸ‘¨ U+1F468 MAN
// 1: ‍ U+200D ZERO WIDTH JOINER
// 2: πŸ‘© U+1F469 WOMAN
// 3: ‍ U+200D ZERO WIDTH JOINER
// 4: πŸ‘§ U+1F467 GIRL
// 5: ‍ U+200D ZERO WIDTH JOINER
// 6: πŸ‘§ U+1F467 GIRL
```

### Stderr Output Stream

Text output streams can also be used to
direct print statements from the default `stdout` destination.
In this example,
the `print` function is directed to write to `stderr`.

```swift
var standardError = StderrOutputStream()
print("Error!", to: &standardError)
```

### Booking Class

Swift allows any type that conforms to `ExpressibleByStringLiteral`
to be initialized from a string literal.
This Playground provides a simple example through the `BookingClass` type.

```swift
("J" as BookingClass) // Business Class
```

### Flight Code

Types conforming to the `LosslessStringConvertible` protocol
can be initialized directly from `String` values.
This Playground shows a `FlightCode` type that adopts both
the `LosslessStringConvertible` and `ExpressibleByStringLiteral` protocols.

```swift
let flight: FlightCode = "AA 1"

flight.airlineCode
flight.flightNumber

FlightCode(String(flight))
```

### Unicode Styling

Swift 5 makes it possible to customize
the behavior of interpolation in string literals
by way of the `ExpressibleByStringInterpolation` protocol.
To demonstrate this,
we implement a `StyledString` type that
allows interpolation segments to specify a style,
such as **bold**, _italic_, and π”£π”―π”žπ”¨π”±π”²π”―.

```swift
let name = "Johnny"
let styled: StyledString = """
Hello, \(name, style: .fraktur(bold: true))!
"""

print(styled)
```

## Chapter 4

### Range Conversion

Objective-C APIs that take `NSString` parameters
or have `NSString` return values
are imported by Swift to use `String` values instead.
However, some of these APIs still specify ranges using the `NSRange` type
instead of `Range`.
This Playground demonstrates how to convert back and forth
between the two range types.

```swift
import Foundation

let string = "Hello, world!"
let nsRange = NSRange(string.startIndex..[A-Z]{3}[0-9]{3}) \h (?[0-9]{0,8}) \n
(?[A-Z]{2}) \h (?[A-Z]{8}) \n
(?

let regex = try NSRegularExpression(pattern: pattern,
options: [])
```

### Parsing with ANTLR4

[ANTLR](https://www.antlr.org) is a parser generator
with support for Swift code generation.
This example provides a functional integration between ANTLR4
and the Swift Package Manager to demonstrate yet another approach
to parsing the same AFTN message from the previous examples.

```swift
import AFTN

let message = try Message(string)!
message.priority
message.destination.location
message.destination.organization
message.destination.department
message.filingTime
message.text
```

## Chapter 7

### Tokenization

The NaturalLanguage framework's `NLTokenizer` class
can tokenize text by word, sentence, and paragraph,
as demonstrated in this example.

```swift
import NaturalLanguage

let string = "Welcome to New York, where the local time is 9:41 AM."
let tokenizer = NLTokenizer(unit: .word)
tokenizer.string = string

let stringRange = string.startIndex..()
classifier.trainText("great flight", for: .positive)
classifier.trainText("flight was late and turbulent", for: .negative)

classifier.classifyText("I had a great flight") // positive
```

### Sentiment Classification

Using Create ML, we can build a Core ML classifier model
that can be used by the Natural Language framework
to determine if a piece of natural language text expresses
positive, negative, or neutral sentiment.

```swift
import NaturalLanguage

let url = Bundle.main.url(forResource: "SentimentClassifier",
withExtension: "mlmodelc")!
let model = try NLModel(contentsOf: url)

model.predictedLabel(for: "Nice, smooth flight") // positive
```

### N-Grams

This Playground provides a Swift implementation of n-grams,
which, combined with `NLTokenizer`,
can produce bigrams and trigrams of words in a piece of natural language text.

```swift
import NaturalLanguage

let string = """
Please direct your attention to flight attendants
as we review the safety features of this aircraft.
"""

let tokenizer = NLTokenizer(unit: .word)
tokenizer.string = string
let words = tokenizer.tokens(for: string.startIndex...

[build status]: https://travis-ci.org/Flight-School/Guide-to-Swift-Strings-Sample-Code
[build status badge]: https://api.travis-ci.com/Flight-School/Guide-to-Swift-Strings-Sample-Code.svg?branch=master
[license]: http://img.shields.io/badge/license-MIT-blue.svg?style=flat
[license badge]: http://img.shields.io/badge/license-MIT-blue.svg?style=flat
[swift version]: https://swift.org/download/
[swift version badge]: http://img.shields.io/badge/swift%20version-5.0-orange.svg?style=flat