Permalink
Browse files

Update syntax and tests for Swift 2.0

  • Loading branch information...
ayanonagon committed Nov 26, 2015
1 parent 65ba832 commit fc6bbadf67a42df76047fd84cc60e328c983eac7
View
@@ -1,6 +1,7 @@
language: objective-c
osx_image: xcode7.1
before_install:
- gem i cocoapods --no-ri --no-rdoc
- gem i xcpretty --no-ri --no-rdoc
script:
- set -o pipefail && xcodebuild -workspace ParsimmonSample.xcworkspace -scheme ParsimmonSample -sdk iphonesimulator test | xcpretty -c
- set -o pipefail && xcodebuild -workspace ParsimmonSample.xcworkspace -scheme ParsimmonSample -sdk iphonesimulator9.1 test | xcpretty -c
View
@@ -32,7 +32,7 @@ protocol Analyzer {
internal func analyze(analyzer: Analyzer, text: String, options: NSLinguisticTaggerOptions?) -> [Pair] {
var pairs: [Pair] = []
let range = NSRange(location: 0, length: count(text))
let range = NSRange(location: 0, length: text.characters.count)
let options = options ?? analyzer.seed.linguisticTaggerOptions
let tagger = analyzer.seed.linguisticTaggerWithOptions(options)
@@ -83,7 +83,7 @@ public class DecisionTree {
var node = self.root
while (node != nil) {
let unwrappedNode = node!
if let leftChild = unwrappedNode.leftChild {
if let _ = unwrappedNode.leftChild {
let pathToTake = sample[unwrappedNode.value.rawValue]
if pathToTake == 0 {
node = unwrappedNode.leftChild
@@ -143,7 +143,7 @@ public class DecisionTree {
}
let splitData = self.splitData(data, onFeature: bestFeature)
var newRemainingFeatures = remainingFeatures
if let bestFeatureIndex = find(newRemainingFeatures, bestFeature) {
if let bestFeatureIndex = newRemainingFeatures.indexOf(bestFeature) {
newRemainingFeatures.removeAtIndex(bestFeatureIndex)
tree.leftChild = self.decisionTree(splitData.0, remainingFeatures: newRemainingFeatures, maxDepth: maxDepth - 1)
tree.rightChild = self.decisionTree(splitData.1, remainingFeatures: newRemainingFeatures, maxDepth: maxDepth - 1)
@@ -41,6 +41,8 @@ public struct Lemmatizer: Analyzer {
@return The lemmatized tokens
*/
public func lemmatizeWordsInText(text: String, options: NSLinguisticTaggerOptions? = nil) -> [String] {
return analyze(self, text, options).map { (token, lemma) in lemma }
return analyze(self, text: text, options: options).map { (token, lemma) in lemma.lowercaseString }.filter {
!$0.isEmpty
}
}
}
@@ -120,7 +120,7 @@ public class NaiveBayesClassifier {
*/
public func classifyTokens(tokens: [Word]) -> Category? {
// Compute argmax_cat [log(P(C=cat)) + sum_token(log(P(W=token|C=cat)))]
return argmax(map(categoryOccurrences) { (category, count) -> (Category, Double) in
return argmax(categoryOccurrences.map { (category, count) -> (Category, Double) in
let pCategory = self.P(category)
let score = tokens.reduce(log(pCategory)) { (total, token) in
total + log((self.P(category, token) + smoothingParameter) / (pCategory + smoothingParameter + Double(self.wordCount)))
View
@@ -26,7 +26,7 @@ public struct Seed {
typealias Language = String
private let language: Language = "en"
let linguisticTaggerOptions: NSLinguisticTaggerOptions = .OmitWhitespace | .OmitPunctuation | .OmitOther
let linguisticTaggerOptions: NSLinguisticTaggerOptions = [.OmitWhitespace, .OmitPunctuation, .OmitOther]
let orthography = NSOrthography(dominantScript: "Latn", languageMap: ["Latn" : ["en"]])
func linguisticTaggerWithOptions(options: NSLinguisticTaggerOptions) -> NSLinguisticTagger {
@@ -32,7 +32,7 @@ public struct TaggedToken: Equatable {
}
}
extension TaggedToken: Printable {
extension TaggedToken: CustomStringConvertible {
public var description: String {
return "('\(token)' \(tag))"
}
View
@@ -41,7 +41,7 @@ public struct Tagger: Analyzer {
@return The tagged tokens
*/
public func tagWordsInText(text: String, options: NSLinguisticTaggerOptions? = nil) -> [TaggedToken] {
return analyze(self, text, options).map { (token, tag) in
return analyze(self, text: text, options: options).map { (token, tag) in
TaggedToken(token: token, tag: tag)
}
}
@@ -41,6 +41,6 @@ public struct Tokenizer: Analyzer {
@return The tokens
*/
public func tokenize(text: String, options: NSLinguisticTaggerOptions? = nil) -> [String] {
return analyze(self, text, options).map { (token, tag) in token }
return analyze(self, text: text, options: options).map { (token, tag) in token }
}
}
@@ -7,8 +7,8 @@
objects = {
/* Begin PBXBuildFile section */
0D47ED5DE340DDD01EBB99E1 /* Pods_ParsimmonSample.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 890ECA8C11825D41385B73DB /* Pods_ParsimmonSample.framework */; settings = {ATTRIBUTES = (Weak, ); }; };
DFE6B5C127F2F4B301DE3D61 /* Pods_ParsimmonTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = BC471E95CCAF79111B1F28DC /* Pods_ParsimmonTests.framework */; settings = {ATTRIBUTES = (Weak, ); }; };
0D47ED5DE340DDD01EBB99E1 /* Pods_ParsimmonSample.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 890ECA8C11825D41385B73DB /* Pods_ParsimmonSample.framework */; };
DFE6B5C127F2F4B301DE3D61 /* Pods_ParsimmonTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = BC471E95CCAF79111B1F28DC /* Pods_ParsimmonTests.framework */; };
E0DA52F51AD3A94000F3A28F /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = E0DA52F41AD3A94000F3A28F /* AppDelegate.swift */; };
E0DA52FE1AD3A94000F3A28F /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = E0DA52FD1AD3A94000F3A28F /* Images.xcassets */; };
E0DA53011AD3A94000F3A28F /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = E0DA52FF1AD3A94000F3A28F /* LaunchScreen.xib */; };
@@ -217,6 +217,7 @@
E0DA52E71AD3A94000F3A28F /* Project object */ = {
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0710;
LastUpgradeCheck = 0630;
ORGANIZATIONNAME = "Ayaka Nonaka";
TargetAttributes = {
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0630"
LastUpgradeVersion = "0710"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
@@ -51,10 +51,10 @@
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
buildConfiguration = "Debug">
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
skipped = "NO">
@@ -76,15 +76,18 @@
ReferencedContainer = "container:ParsimmonSample.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
buildConfiguration = "Debug"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
@@ -100,10 +103,10 @@
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
buildConfiguration = "Release"
debugDocumentVersioning = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
@@ -31,7 +31,8 @@ class ClassifierViewController: UIViewController {
private lazy var classifier = NaiveBayesClassifier()
@IBAction private func spamOrHamAction(sender: UIButton) {
let category = classifier.classify(messageTextField.text)
guard let text = messageTextField.text else { return }
let category = classifier.classify(text)
resultLabel.text = category
}
@@ -35,7 +35,8 @@ class TaggerViewController: UIViewController {
}
@IBAction private func parsimmonAction(sender: UIButton) {
let taggedTokens = tagger.tagWordsInText(inputTextField.text)
guard let text = inputTextField.text else { return }
let taggedTokens = tagger.tagWordsInText(text)
outputTextview.text = "\(taggedTokens)"
dismissKeyboard()
}
View
@@ -11,4 +11,4 @@ EXTERNAL SOURCES:
SPEC CHECKSUMS:
Parsimmon: 9fe1f416f5d0c63c28ba14bd4f1bbbfec458a7d3
COCOAPODS: 0.36.3
COCOAPODS: 0.39.0

0 comments on commit fc6bbad

Please sign in to comment.