3EOFNRQF35M4SFEGGW7FH7DZJ4DAKNC2QB5VD7GCYJ2VBWI3QYXQC
/* Begin XCRemoteSwiftPackageReference section */
43A25D6A2943A9D20065D0E3 /* XCRemoteSwiftPackageReference "swift-argument-parser" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/apple/swift-argument-parser.git";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 1.0.0;
};
};
/* End XCRemoteSwiftPackageReference section */
/* Begin XCSwiftPackageProductDependency section */
43A25D6B2943A9D20065D0E3 /* ArgumentParser */ = {
isa = XCSwiftPackageProductDependency;
package = 43A25D6A2943A9D20065D0E3 /* XCRemoteSwiftPackageReference "swift-argument-parser" */;
productName = ArgumentParser;
};
/* End XCSwiftPackageProductDependency section */
struct App {
static func main() {
struct App: ParsableCommand {
@Argument(help: "The text you want to analyze")
var input: [String]
@Flag(help: "Show detected language.")
var detectLanguage = false
@Flag(help: "Prints the input's sentiment valence.")
var sentimentAnalysis = false
@Flag(help: "Shows the stem form of each word.")
var toLemmatize = false
@Flag(help: "Prints alternative words for each word in the input.")
var alternatives = false
@Flag(help: "Prints names of people, places, and organizations in the input.")
var names = false
mutating func run() {
if detectLanguage == false && sentimentAnalysis == false && toLemmatize == false && alternatives == false && names == false {
detectLanguage = true
sentimentAnalysis = true
toLemmatize = true
alternatives = true
names = true
}
let text: String = CommandLine.arguments.dropFirst().joined(separator: " ")
print(text)
print()
let sentiment: Double = sentiment(for: text)
print("Sentiment analysis: \(sentiment)")
let text = input.joined(separator: " ")
if detectLanguage {
let language = NLLanguageRecognizer.dominantLanguage(for: text) ?? .undetermined
print()
print("Detected language: \(language.rawValue)")
print(text)
}
if sentimentAnalysis {
let sentiment: Double = sentiment(for: text)
print()
print("Sentiment analysis: \(sentiment)")
}
let lemma = lemmatize(string: text)
print()
print("Foun the following germane words:")
for word in lemma {
let embeddings = embeddings(for: word)
print("\t\(word): ", embeddings.formatted(.list(type: .and)))
if names {
let entities = entities(for: text)
print()
print("Found the following entities:")
for entity in entities { print("\t", entity) }
}
lazy var lemma = lemmatize(string: text)
if toLemmatize {
print()
print("Found the following lemma:")
print("\t", lemma.formatted(.list(type: .and)))
}
if alternatives {
print()
print("Found the following alternatives:")
for word in lemma {
let embeddings = embeddings(for: word)
print("\t\(word): ", embeddings.formatted(.list(type: .and)))
}
}
return true
}
return results
}
func entities(for string: String) -> [String] {
let tagger = NLTagger(tagSchemes: [.nameType])
tagger.string = string
var results = [String]()
tagger.enumerateTags(in: string.startIndex..<string.endIndex, unit: .word, scheme: .nameType, options: .joinNames) { tag, range in
guard let tag = tag else { return true }
let match = String(string[range])
switch tag {
case .organizationName:
results.append("Organization: \(match)")
case .personalName:
results.append("Person: \(match)")
case .placeName:
results.append("Place: \(match)")
default:
break