443814d65674268bfe39beddcfa47647
macOS SwiftUI 知识图谱之WordNet解析与查询全功能语料库

实战需求

macOS SwiftUI 知识图谱之WordNet解析与查询全功能语料库

本文价值与收获

看完本文后,您将能够作出下面的界面

macOS SwiftUI 知识图谱之WordNet解析与查询全功能语料库

macOS SwiftUI 知识图谱之WordNet解析与查询全功能语料库

macOS SwiftUI 知识图谱之WordNet解析与查询全功能语料库f

macOS SwiftUI 知识图谱之WordNet解析与查询全功能语料库f

看完本文您将掌握的技能

1、什么是WordNet

WordNet是由Princeton 大学的心理学家,语言学家和计算机工程师联合设计的一种基于认知语言学的英语词典。它不是光把单词以字母顺序排列,而且按照单词的意义组成一个“单词的网络”。

2、SwiftUI 实现查询与检索

3、SwiftUI 内容显示与逐级显示


实战代码

1、 主界面

import SwiftUI

struct Result:Hashable, Codable, Identifiable {
    public var noun: String
    public var id: Int

    init(id: Int, noun: String) {
        self.id = id
        self.noun = noun
    }

    static func == (lhs: Result, rhs: Result) -> Bool {
        return lhs.id == rhs.id && lhs.noun == rhs.noun
    }
}

struct ResultView: View {
    @EnvironmentObject var searchData: SearchData
    var result: Result
    var contentView: ContentView

    init(_ result: Result, contentView: ContentView) {
        self.result = result
        self.contentView = contentView
    }

    var body: some View {
        let noun = result.noun.replacingOccurrences(of: "_", with: " ")
        Button(noun) {
            searchData.searchString = noun
            contentView.search()
        }
    }
}

struct ContentView: View {
    @EnvironmentObject var searchData: SearchData
    @State private var ancestors:[Result] = []
    @State private var descendants:[Result] = []

    init() {

    }

    var body: some View {
        VStack {
            HStack {
                Text("Search Term: ")
                TextField(
                        "Enter a noun",
                        text: $searchData.searchString
                    ) { isEditing in
                    } onCommit: {
                        self.search()
                    }.disableAutocorrection(true)
                Button(LocalizedStringKey("Search")) {
                    self.search()
                }
            }.padding(EdgeInsets(top: 10, leading: 10, bottom: 10, trailing: 10))

            HStack {
                VStack {
                    Text("Ancestors (\(ancestors.count))").fontWeight(.bold)
                    List{
                        ForEach(ancestors, id: \.self) { result in
                            ResultView(result, contentView: self)
                        }
                    }
                }
                VStack {
                    Text("Children (\(descendants.count))").fontWeight(.bold)
                    List {
                        ForEach(descendants, id: \.self) { result in
                            ResultView(result, contentView: self)
                        }
                    }
                }
            }.padding(EdgeInsets(top: 10, leading: 10, bottom: 10, trailing: 10))
        }
        .frame(width: 600, height: 400, alignment: .leading)

    }

    internal func search() {
        ancestors = searchData.wordNet.ancestors(word: searchData.searchString.components(separatedBy: .whitespaces).joined(separator: "_")).map { (key: Int, value: String) in
            return Result(id: key, noun: value)
        }
        descendants = searchData.wordNet.descendants(word: searchData.searchString.components(separatedBy: .whitespaces).joined(separator: "_")).map { (key: Int, value: String) in
            return Result(id: key, noun: value)
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView().environmentObject(SearchData("flower"))
    }
}

2、搜索组件

import Foundation

class SearchData: ObservableObject {
    @Published var searchString: String = ""
    @Published var wordNet = WordNet(synsets: "synsets.txt", hypernyms: "hypernyms.txt")

    init() {

    }
    init(_ string: String) {
        self.searchString = string
    }
}
top Created with Sketch.