Browse Source

initial commit and working

master
boB Rudis 3 years ago
parent
commit
4c953f297d
No known key found for this signature in database GPG Key ID: 1D7529BE14E2BBA9
  1. 2
      .Rbuildignore
  2. 13
      DESCRIPTION
  3. 2
      LICENSE
  4. 21
      LICENSE.md
  5. 4
      NAMESPACE
  6. 13
      R/parts-of-speech.R
  7. 7
      R/swiftspeech-package.R
  8. BIN
      inst/lib/libswiftspeech.dylib
  9. BIN
      lib/libswiftspeech.dylib
  10. 20
      man/parts_of_speech.Rd
  11. 4
      man/swiftspeech.Rd
  12. 22
      src/Makevars
  13. 21
      src/init.c
  14. BIN
      src/libswiftspeech.dylib
  15. 6
      src/swiftspeech.h
  16. 40
      src/swiftspeech.swift

2
.Rbuildignore

@ -19,3 +19,5 @@
^CRAN-RELEASE$ ^CRAN-RELEASE$
^appveyor\.yml$ ^appveyor\.yml$
^tools$ ^tools$
^LICENSE\.md$
^lib$

13
DESCRIPTION

@ -1,6 +1,6 @@
Package: swiftspeech Package: swiftspeech
Type: Package Type: Package
Title: swiftspeech title goes here otherwise CRAN checks fail Title: Classify Parts of Speech Using Apple's CoreML and NaturalLanguage Libraries
Version: 0.1.0 Version: 0.1.0
Date: 2021-01-23 Date: 2021-01-23
Authors@R: c( Authors@R: c(
@ -8,17 +8,16 @@ Authors@R: c(
comment = c(ORCID = "0000-0001-5670-2640")) comment = c(ORCID = "0000-0001-5670-2640"))
) )
Maintainer: Bob Rudis <bob@rud.is> Maintainer: Bob Rudis <bob@rud.is>
Description: A good description goes here otherwise CRAN checks fail. Description: Classify Parts of Speech Using Apple's CoreML and NaturalLanguage Libraries.
URL: https://git.rud.is/hrbrmstr/swiftspeech URL: https://git.rud.is/hrbrmstr/swiftspeech
BugReports: https://git.rud.is/hrbrmstr/swiftspeech/issues BugReports: https://git.rud.is/hrbrmstr/swiftspeech/issues
Encoding: UTF-8 Encoding: UTF-8
License: AGPL License: MIT + file LICENSE
Suggests: Suggests:
covr, tinytest covr, tinytest
Depends: Depends:
R (>= 3.5.0) R (>= 3.6.0)
Imports:
httr,
jsonlite
Roxygen: list(markdown = TRUE) Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.1 RoxygenNote: 7.1.1
RequiresCompilation: yes
SystemRequirments: macOS; Swift

2
LICENSE

@ -0,0 +1,2 @@
YEAR: 2021
COPYRIGHT HOLDER: Bob Rudis

21
LICENSE.md

@ -0,0 +1,21 @@
# MIT License
Copyright (c) 2021 Bob Rudis
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
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

4
NAMESPACE

@ -1,4 +1,4 @@
# Generated by roxygen2: do not edit by hand # Generated by roxygen2: do not edit by hand
import(httr) export(parts_of_speech)
importFrom(jsonlite,fromJSON) useDynLib(swiftspeech, .registration = TRUE)

13
R/parts-of-speech.R

@ -0,0 +1,13 @@
#' Parts of speech
#'
#' @usage
#' parts_of_speech("The ripe taste of cheese improves with age.")
#' @param x words
#' @return (list) one vector with words, one with parts of speech
#' @export
#' @examples
#' parts_of_speech("The ripe taste of cheese improves with age.")
parts_of_speech <- function(x) {
res <- .Call("part_of_speech", x)
as.data.frame(stats::setNames(res, c("name", "tag")))
}

7
R/swiftspeech-package.R

@ -1,9 +1,8 @@
#' ... #' Classify Parts of Speech Using Apple's CoreML and NaturalLanguage Libraries
#' #'
#' @md #' @md
#' @name swiftspeech #' @name swiftspeech
#' @keywords internal #' @keywords internal
#' @author Bob Rudis (bob@@rud.is) #' @author Bob Rudis (bob@@rud.is)
#' @import httr #' @useDynLib swiftspeech, .registration = TRUE
#' @importFrom jsonlite fromJSON
"_PACKAGE" "_PACKAGE"

BIN
inst/lib/libswiftspeech.dylib

Binary file not shown.

BIN
lib/libswiftspeech.dylib

Binary file not shown.

20
man/parts_of_speech.Rd

@ -0,0 +1,20 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/parts-of-speech.R
\name{parts_of_speech}
\alias{parts_of_speech}
\title{Parts of speech}
\usage{
parts_of_speech("The ripe taste of cheese improves with age.")
}
\arguments{
\item{x}{words}
}
\value{
(list) one vector with words, one with parts of speech
}
\description{
Parts of speech
}
\examples{
parts_of_speech("The ripe taste of cheese improves with age.")
}

4
man/swiftspeech.Rd

@ -4,9 +4,9 @@
\name{swiftspeech} \name{swiftspeech}
\alias{swiftspeech} \alias{swiftspeech}
\alias{swiftspeech-package} \alias{swiftspeech-package}
\title{...} \title{Classify Parts of Speech Using Apple's CoreML and NaturalLanguage Libraries}
\description{ \description{
A good description goes here otherwise CRAN checks fail. Classify Parts of Speech Using Apple's CoreML and NaturalLanguage Libraries.
} }
\seealso{ \seealso{
Useful links: Useful links:

22
src/Makevars

@ -0,0 +1,22 @@
SWIFTC = /usr/bin/swiftc
INSTALL_NAME_TOOL = /usr/bin/install_name_tool
SWIFTLIB = libswiftspeech.dylib
LIBDIR = ../inst/lib
DEVLIB = ../lib
PKG_LIBS = -lswiftspeech -L../inst/lib
all: $(SHLIB) swiftLibrary
swiftLibrary: $(SWIFTLIB)
-@if test ! -e $(LIBDIR); then mkdir -p $(LIBDIR); fi
-@if test ! -e $(DEVLIB); then mkdir -p $(DEVLIB); fi
cp $(SWIFTLIB) $(LIBDIR)
cp $(SWIFTLIB) $(DEVLIB)
$(SWIFTLIB): $(OBJECTS)
$(SHLIB_CXXLD) -o $(SWIFTLIB) $^ $(SHLIB_CXXLDFLAGS) $(LDFLAGS) $(ALL_LIBS)
$(SWIFTC) -I /Library/Frameworks/R.framework/Headers -F/Library/Frameworks -framework R -framework CoreML -framework NaturalLanguage -import-objc-header swiftspeech.h -emit-library swiftspeech.swift
$(INSTALL_NAME_TOOL) -change $(SWIFTLIB) @loader_path/../lib/$(SWIFTLIB) swiftspeech.so
clean:
rm -Rf $(SHLIB) $(SWIFTLIB) $(OBJECTS) $(DEVLIB)/$(SWIFTLIB)

21
src/init.c

@ -0,0 +1,21 @@
#include <R.h>
#include <Rinternals.h>
#include <stdlib.h> // for NULL
#include <R_ext/Rdynload.h>
/* .Call calls */
extern SEXP hey();
extern SEXP plus_one(SEXP);
extern SEXP part_of_speech(SEXP);
static const R_CallMethodDef CallEntries[] = {
{"hey", (DL_FUNC) &hey, 0},
{"plus_one", (DL_FUNC) &plus_one, 1},
{"part_of_speech", (DL_FUNC) &part_of_speech, 1},
{NULL, NULL, 0}
};
void R_init_daybreak(DllInfo *dll) {
R_registerRoutines(dll, NULL, CallEntries, NULL, NULL);
R_useDynamicSymbols(dll, FALSE);
}

BIN
src/libswiftspeech.dylib

Binary file not shown.

6
src/swiftspeech.h

@ -0,0 +1,6 @@
#define USE_RINTERNALS
#include <R.h>
#include <Rinternals.h>
const char* R_CHAR(SEXP x);

40
src/swiftspeech.swift

@ -0,0 +1,40 @@
import NaturalLanguage
import CoreML
extension Array where Element == String {
var SEXP: SEXP? {
let charVec = Rf_protect(Rf_allocVector(SEXPTYPE(STRSXP), count))
defer { Rf_unprotect(1) }
for (idx, elem) in enumerated() { SET_STRING_ELT(charVec, idx, Rf_mkChar(elem)) }
return(charVec)
}
}
@_cdecl("part_of_speech")
public func part_of_speech(_ x: SEXP) -> SEXP {
let text = String(cString: R_CHAR(STRING_ELT(x, 0)))
let tagger = NLTagger(tagSchemes: [.lexicalClass])
tagger.string = text
let options: NLTagger.Options = [.omitPunctuation, .omitWhitespace]
var txts = [String]()
var tags = [String]()
tagger.enumerateTags(in: text.startIndex..<text.endIndex, unit: .word, scheme: .lexicalClass, options: options) { tag, tokenRange in
if let tag = tag {
txts.append("\(text[tokenRange])")
tags.append("\(tag.rawValue)")
}
return true
}
let out = Rf_protect(Rf_allocVector(SEXPTYPE(VECSXP), 2))
SET_VECTOR_ELT(out, 0, txts.SEXP)
SET_VECTOR_ELT(out, 1, tags.SEXP)
Rf_unprotect(1)
return(out!)
}
Loading…
Cancel
Save