Created
August 30, 2017 13:57
-
-
Save julien-c/136ef6a3a1868559ae6a754e4d40d8ee to your computer and use it in GitHub Desktop.
Nlp Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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