Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce more Swift components #13

Merged
merged 4 commits into from
Feb 21, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Parsimmon/Example/ClassifierViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
//

#import "ClassifierViewController.h"
#import "Parsimmon.h"
#import "Parsimmon-Swift.h"

@interface ClassifierViewController ()
Expand Down
6 changes: 3 additions & 3 deletions Parsimmon/Example/TaggerViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
//

#import "TaggerViewController.h"
#import "Parsimmon.h"
#import "Parsimmon-Swift.h"

@interface TaggerViewController ()
@property (weak, nonatomic) IBOutlet UITextField *inputTextField;
@property (weak, nonatomic) IBOutlet UITextView *outputTextView;

@property (strong, nonatomic) ParsimmonTagger *tagger;
@property (strong, nonatomic) Tagger *tagger;
@end

@implementation TaggerViewController
Expand All @@ -22,7 +22,7 @@ - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.tagger = [[ParsimmonTagger alloc] init];
self.tagger = [[Tagger alloc] init];

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
Expand Down
94 changes: 49 additions & 45 deletions Parsimmon/Parsimmon.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

34 changes: 34 additions & 0 deletions Parsimmon/Parsimmon/Analyzer.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// Enumerator.swift
// Parsimmon
//
// Created by Jordan Kay on 2/19/15.
//
//

import Foundation

typealias Pair = (String, String)

protocol Analyzer {
var seed: Seed { get }
var scheme: String { get }
}

internal func analyze(analyzer: Analyzer, text: String, options: NSLinguisticTaggerOptions?) -> [Pair] {
var pairs: [Pair] = []

let range = NSRange(location: 0, length: count(text))
let options = options ?? analyzer.seed.linguisticTaggerOptions
let tagger = analyzer.seed.linguisticTaggerWithOptions(options)

tagger.string = text
tagger.enumerateTagsInRange(range, scheme: analyzer.scheme, options: options) { (tag: String?, tokenRange, range, stop) in
if let tag = tag {
let token = (text as NSString).substringWithRange(tokenRange)
let pair = (token, tag)
pairs.append(pair)
}
}
return pairs
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
// ParsimmonTagger.h
//
// Copyright (c) 2013 Ayaka Nonaka
// Tokenizer.swift
//
// Copyright (c) 2015 Ayaka Nonaka
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand All @@ -20,24 +20,28 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#import <Foundation/Foundation.h>
#import "ParsimmonSeed.h"
import Foundation

public class Lemmatizer: NSObject, Analyzer {
let seed: Seed

var scheme: String {
return NSLinguisticTagSchemeLemma
}

@interface ParsimmonTagger : ParsimmonSeed
init(seed: Seed = Seed()) {
self.seed = seed
}

/**
Returns the tagged tokens for the input text, omitting any whitespace, punctuation, and other symbols.
@param text The text to tag
@return The tagged tokens
*/
- (NSArray *)tagWordsInText:(NSString *)text;
override convenience init() {
self.init(seed: Seed())
}

/**
Returns the tagged tokens for the input text using the specified linguistic tagger options.
@param text Text to tag
@param options Linguistic tagger options
@return The tagged tokens
*/
- (NSArray *)tagText:(NSString *)text options:(NSLinguisticTaggerOptions)options;
func lemmatizeWordsInText(text: String) -> [String] {
return lemmatizeText(text, options: nil)
}

@end
func lemmatizeText(text: String, options: NSLinguisticTaggerOptions?) -> [String] {
return analyze(self, text, options).map { (token, lemma) in lemma }
}
}
6 changes: 3 additions & 3 deletions Parsimmon/Parsimmon/NaiveBayesClassifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,19 @@ public class NaiveBayesClassifier: NSObject {
public typealias Word = String
public typealias Category = String

private let tokenizer: ParsimmonTokenizer
private let tokenizer: Tokenizer

private var categoryOccurrences: [Category: Int] = [:]
private var wordOccurrences: [Word: [Category: Int]] = [:]
private var trainingCount = 0
private var wordCount = 0

public init(tokenizer: ParsimmonTokenizer) {
public init(tokenizer: Tokenizer) {
self.tokenizer = tokenizer
}

public convenience override init() {
self.init(tokenizer: ParsimmonTokenizer())
self.init(tokenizer: Tokenizer())
}

// MARK: - Training
Expand Down
1 change: 0 additions & 1 deletion Parsimmon/Parsimmon/Parsimmon-Bridging-Header.h
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
#import "ParsimmonSeed.h"
49 changes: 0 additions & 49 deletions Parsimmon/Parsimmon/ParsimmonLemmatizer.m

This file was deleted.

55 changes: 0 additions & 55 deletions Parsimmon/Parsimmon/ParsimmonSeed.h

This file was deleted.

59 changes: 0 additions & 59 deletions Parsimmon/Parsimmon/ParsimmonSeed.m

This file was deleted.

Loading