常用耦合词的数据库结构

时间:2019-05-27 17:54:31

标签: database data-structures

我需要创建一个数据库来存储“ 最常见的配对词”。 我将向我的软​​件提供大量文本(主要是书籍和文章),将其分成段落并以某种方式存储。 之后,用户应该能够搜索一个单词并检查在搜索到的单词的段落中出现最多的单词。

示例:用户搜索“ flower”,系统应返回如下内容。

Search word: "flower"

Most common matches with "flower":
1. "red" appeared 4918 times in a paragraph with "flower"
2. "white" appeared 3502 times in a paragraph with "flower"
3. "fresh" appeared 2501 times in a paragraph with "flower"
4. "scented" appeared 2499 times in a paragraph with "flower"
...and so on

在实现像样的速度查询和小的存储需求的情况下,实现这种功能的最佳数据库结构是什么?

这可能是一张带有单词对和出现次数排序表的表格吗? 2表存储单词words_id和第二个表存储word1_id,word2_id,count更好吗? 是否存在另一种解决此类问题的常用方法?

1 个答案:

答案 0 :(得分:0)

我建议不要存储配对。这将导致二次空间复杂性。如果您的段落长为100个字,那么大约有5,000个字对,因此您将存储5,000条记录,而不是100条,增加了50倍。相反,您可能有一个private BroadcastReceiver Receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); if(action.equals(BluetoothDevice.ACTION_PAIRING_REQUEST)){ try { int type = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, BluetoothDevice.ERROR); if (type == BluetoothDevice.PAIRING_VARIANT_PASSKEY_CONFIRMATION) { int keycode = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_KEY, 0); byte[] pinBytes = (""+keycode).getBytes("UTF-8"); device.setPin(pinBytes); ////device.setPairingConfirmation(true); ////Manifest.permission.BLUETOOTH_PRIVILEGED is needed, so we cannot use it directly or by java reflection ? abortBroadcast(); //// prevent the pop dialog } } catch (Exception e) { e.printStackTrace(); } } } }; 表和一个word表,其目的是将单词映射到段落。像这样:

text_index

在此极简示例中,当您扫描文本时,将依次生成parahraph ID。您当然可以对此进行改进,例如,如果需要能够将段落追溯到书籍,则可以通过添加带有文档ID的create table word ( id int not null auto_increment, form varchar(100) not null default '', primary key (id), unique key (form) ); create table text_index ( id int not null auto_increment, word_id int not null default 0, paragraph_id int not null default 0, primary key (id), key (paragraph_id), key (word_id) ); 表并将段落映射到文档来实现。

查询基本上说“给我所有与'flower'共享一个段落ID的单词和字数”:

document

以下是一些示例数据。我尚未在大型数据集上测试过该查询,但是使用适当的索引应该很快。

select w.form, count(*) as c
from text_index a
join text_index b on a.paragraph_id = b.paragraph_id
join word w on b.word_id = w.id
where a.word_id = 1
  and b.word_id != 1
group by b.word_id;