Skip to content

Instantly share code, notes, and snippets.

@julien-c
Created August 30, 2017 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save julien-c/136ef6a3a1868559ae6a754e4d40d8ee to your computer and use it in GitHub Desktop.
Save julien-c/136ef6a3a1868559ae6a754e4d40d8ee to your computer and use it in GitHub Desktop.
Nlp Swift
func handleSentence(s: [Token]) {
let text = s.map { $0.text }.joined(separator: " ")
tagger.string = text
var charIdx = 0
var prevDescr = "O"
for tok in s {
let iosTag = tagger.tag(at: charIdx, unit: .word, scheme: .nameType, tokenRange: nil)
let iosDescr = iosTag?.canonical ?? "O"
let outputLine = "\(tok.text) \(tok.goldTag) \(iob(iosDescr, prevDescr))"
output.append(outputLine)
charIdx += tok.text.utf16.count + 1 /// Include single space.
prevDescr = iosDescr
}
output.append("")
}
func handleFile(content: [String]) {
var currSentence: [Token] = []
for x in content where !x.starts(with: "-DOCSTART-") {
if x == "" {
handleSentence(s: currSentence)
currSentence = []
} else {
let objs = x.split(separator: " ")
let text = String(objs[0])
let goldTag = String(objs[objs.count - 1])
currSentence.append(Token(text: text, goldTag: goldTag))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment