Last active
April 4, 2016 16:38
-
-
Save bjhomer/a60947391e115d2c5112e5a6e02bcf29 to your computer and use it in GitHub Desktop.
A command line tool to output words from input text, reading from stdin or from a passed file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Usage: echo "these are some words" | words | |
// Output: | |
// these | |
// are | |
// some | |
// words | |
import Foundation | |
import Swift | |
public struct StderrOutputStream: OutputStreamType { | |
public static let stream = StderrOutputStream() | |
public func write(string: String) {fputs(string, stderr)} | |
} | |
public var errStream = StderrOutputStream.stream | |
let tagger = NSLinguisticTagger(tagSchemes: [NSLinguisticTagSchemeTokenType], options: Int(NSLinguisticTaggerOptions.JoinNames.rawValue)) | |
let args = Process.arguments | |
let input: NSString | |
var fileHandle = NSFileHandle.fileHandleWithStandardInput() | |
if args.count > 1 { | |
guard let handle = NSFileHandle(forReadingAtPath: args[1]) else { | |
print("Invalid input file", toStream: &errStream) | |
exit(1) | |
} | |
fileHandle = handle | |
} | |
let inputData = fileHandle.readDataToEndOfFile() | |
guard let handleInput = NSString(data:inputData, encoding: NSUTF8StringEncoding) else { | |
print("Unable to read input as text", toStream: &errStream) | |
exit(1) | |
} | |
input = handleInput | |
tagger.string = input as String | |
let range = NSMakeRange(0, input.length) | |
tagger.enumerateTagsInRange(range, scheme: NSLinguisticTagSchemeTokenType, | |
options: [NSLinguisticTaggerOptions.OmitOther, | |
NSLinguisticTaggerOptions.OmitPunctuation, | |
NSLinguisticTaggerOptions.OmitWhitespace] | |
) { (tag, tokenRange, sentenceRange, stopPtr) in | |
let word = input.substringWithRange(tokenRange) | |
print(word.lowercaseString) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment