Skip to content

Instantly share code, notes, and snippets.

View ozgurshn's full-sized avatar

Ozgur Sahin ozgurshn

View GitHub Profile
@ozgurshn
ozgurshn / PartOfSpeech.swift
Created March 10, 2019 20:44
NLTagger part of speech tagging
import NaturalLanguage
let text = "Elinde güzel bir çiçek vardı."
let tagger = NLTagger(tagSchemes: [.lexicalClass])
tagger.string = text
let options: NLTagger.Options = [.omitPunctuation, .omitWhitespace]
tagger.enumerateTags(in: text.startIndex..<text.endIndex, unit: .word, scheme: .lexicalClass, options: options) { tag, tokenRange in
if let tag = tag {
print("\(text[tokenRange]): \(tag.rawValue)")
}
return true
@ozgurshn
ozgurshn / NamedEntityRecognition.swift
Created March 10, 2019 21:16
NamedEntityRecognition
import NaturalLanguage
let text = "The American Red Cross was established in Washington, D.C., by Clara Barton."
let tagger = NLTagger(tagSchemes: [.nameType])
tagger.string = text
let options: NLTagger.Options = [.omitPunctuation, .omitWhitespace, .joinNames]
let tags: [NLTag] = [.personalName, .placeName, .organizationName]
tagger.enumerateTags(in: text.startIndex..<text.endIndex, unit: .word, scheme: .nameType, options: options) { tag, tokenRange in
if let tag = tag, tags.contains(tag) {
import CreateML
import Foundation
// Read from CSV
var data = try MLDataTable(contentsOf: URL(fileURLWithPath: "/Users/ozgur/dataset/fortnite.csv"))
let parsingOptions = MLDataTable.ParsingOptions(containsHeader: true, delimiter: ",", comment: "", escape: "", doubleQuote: false, quote: "", skipInitialSpaces: false, missingValues: [], lineTerminator: "\r", selectColumns:["author","rating","review"], maxRows: nil, skipRows: 0)
var table = try MLDataTable(contentsOf: URL(fileURLWithPath: "/Users/ozgur/Downloads/fortnite.csv"), options: parsingOptions)
@ozgurshn
ozgurshn / AddNewColumn.swift
Last active March 29, 2019 20:28
Add new column to a data table.
import Foundation
import CreateML
// Read from CSV
var data = try MLDataTable(contentsOf: URL(fileURLWithPath: "/Users/ozgur/Documents/CreateMLFiles/englishwords.csv"))
let newColumn = data.map { row -> String in
guard
let score1 = row["score1"]?.doubleValue,
let score2 = row["score2"]?.doubleValue
@ozgurshn
ozgurshn / CheckSceneStability.swift
Created May 23, 2019 22:09
Scene stability iOS Vision
/// - Tag: CheckSceneStability
fileprivate func sceneStabilityAchieved() -> Bool {
// Determine if we have enough evidence of stability.
if transpositionHistoryPoints.count == maximumHistoryLength {
// Calculate the moving average.
var movingAverage: CGPoint = CGPoint.zero
for currentPoint in transpositionHistoryPoints {
movingAverage.x += currentPoint.x
movingAverage.y += currentPoint.y
}
@ozgurshn
ozgurshn / CaptureOutputWithSceneStability.swift
Created May 23, 2019 22:11
Capture output with scene stability iOS Vision
override func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
guard let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else {
return
}
guard previousPixelBuffer != nil else {
previousPixelBuffer = pixelBuffer
self.resetTranspositionHistory()
return
}
@ozgurshn
ozgurshn / SceneStabilityVC.swift
Created May 23, 2019 22:38
iOS Vision view controller with scene stability
/*
See LICENSE folder for this sample’s licensing information.
Abstract:
Implements the Vision view controller.
*/
import UIKit
import AVFoundation
import Vision
@ozgurshn
ozgurshn / sceneStabilityAchieved.swift
Created May 23, 2019 22:58
scene Stability Achieved check ios vision
if self.sceneStabilityAchieved() {
showDetectionOverlay(true)
if currentlyAnalyzedPixelBuffer == nil {
// Retain the image buffer for Vision processing.
currentlyAnalyzedPixelBuffer = pixelBuffer
analyzeCurrentImage()
}
} else {
showDetectionOverlay(false)
}
@ozgurshn
ozgurshn / ImageRegistrationResult.swift
Created May 23, 2019 22:59
VNImageTranslationAlignmentObservation
if let results = registrationRequest.results {
if let alignmentObservation = results.first as? VNImageTranslationAlignmentObservation {
let alignmentTransform = alignmentObservation.alignmentTransform
self.recordTransposition(CGPoint(x: alignmentTransform.tx, y: alignmentTransform.ty))
}
}