gpt4 book ai didi

java - 在java应用程序中将单词映射到其同义词

转载 作者:行者123 更新时间:2023-11-30 04:20:53 25 4
gpt4 key购买 nike

我正在开发一个单词同义词应用程序,但遇到了挑战。挑战/问题是如何在我的应用程序数据库中映射单词及其同义词?

最佳答案

同义词是可交换的,但不是可传递的。

我找不到例子,所以下面是人造的:

bank
synonym(bank) = money-institute => synonym(money-institute) = bank
synonym(bank) = place-to-sit => synonym(money-institute) = bank

place-to-sit:
synonym(place-to-sit) = bank
synonym(place-to-sit) = chair

NOT synonym(chair) = money-institute

这意味着您只需要一个没有顺序的对列表,并且两个都是另一个的同义词,反之亦然。还有更多您不需要做的事情。

public class SynonymTable {

private Map<Word, Set<Word>> synonymTable = new HashMap<>();

public Set<Word> getSynonyms(Word word) {
return synonymTable.get(word);
}

public void registerSynonym(Word word, Word... synonymsOfWord) {
for (Word syn : synonymsOfWord) {
putSynonymTable(word, syn); // synonym(word) = syn
putSynonymTable(syn, word); // synonym(syn) = word
}
}

private void putSynonymTable(Word word, Word synonymOfWord) {
Set syns = synonymTable.get(word);
if (syns == null) {
syns = new TreeSet<Word>();
synonymTable.put(word, syns);
}
syns.add(synonymOfWord);
}
...
<小时/>

数据库变体

CREATE TABLE TWord (
wordId INT AUTO_INCR,
wordText VARCHAR(80),
INDEX(wordText),
PRIMARY KEY(wordId)
);
CREATE TABLE TSynonym (
wordFk INT NOT NULL,
synonymFk INT NOT NULL,
CONSTRAINT FOREIGN KEY (wordFk) REFERENCES TWord(wordId),
CONSTRAINT FOREIGN KEY (synonymFk) REFERENCES TWord(wordId),
PRIMARY KEY(wordFk, synonymFk)
);

关于java - 在java应用程序中将单词映射到其同义词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17091185/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com