我有 5 GB 的 txt 文件,其中包含 2,200,000 条单词记录及其数字向量。
当用户在我的 Web 应用程序的 txt 框中输入查询时,我希望在文本文件中搜索单词并在 txt 文件中找到结果向量。
这是一个常量 txt 文件,我不想向其附加任何内容
现在我最好使用这个 txt 文件或使用数据库,因为它是一个 Web 应用程序,响应时间对我来说很重要。
txt 文件是 GLOVE(单词表示的全局向量)。
每条记录都是这样的:
the 0.418 0.24968 -0.41242 0.1217 0.34527 -0.044457 -0.49688 -0.17862 -0.00066023 -0.6566 0.27843 -0.14767 -0.55677 0.14658 -0.0095095 0.011658 0.10204 -0.12792 -0.8443 -0.12181 -0.016801 -0.33279 -0.1552 -0.23131 -0.19181 -1.8823 -0.76746 0.099051 -0.42125 -0.19526 4.0071 -0.18594 -0.52287 -0.31681 0.00059213 0.0074449 0.17778 -0.15897 0.012041 -0.054223 -0.29871 -0.15749 -0.34758 -0.045637 -0.44251 0.18785 0.0027849 -0.18411 -0.11514 -0.78581
最佳答案
可能非常适合 SQLite。
创建一个包含 2 列的表 - 'word' 和 'vector',导入数据,在 'word' 上创建索引,然后就完成了。
https://sqlite.org/whentouse.html
演示
生成文件
python
with open(r'c:\Temp\glove.txt','w') as f:
for i in range(0,13000000):
f.write('{}|{}\n'.format(1000001+i,'0.418 0.24968 -0.41242 0.1217 0.34527 -0.044457 -0.49688 -0.17862 -0.00066023 -0.6566 0.27843 -0.14767 -0.55677 0.14658 -0.0095095 0.011658 0.10204 -0.12792 -0.8443 -0.12181 -0.016801 -0.33279 -0.1552 -0.23131 -0.19181 -1.8823 -0.76746 0.099051 -0.42125 -0.19526 4.0071 -0.18594 -0.52287 -0.31681 0.00059213 0.0074449 0.17778 -0.15897 0.012041 -0.054223 -0.29871 -0.15749 -0.34758 -0.045637'))
检查文件大小
命令
c:\Temp>dir glove.txt
Directory of c:\Temp
12/04/2016 12:26 PM 5,204,000,001 glove.txt
1 File(s) 5,204,000,001 bytes
创建SQLite数据库+加载数据+建立索引
c:\Temp>sqlite3 glove.db
SQLite version 3.15.1 2016-11-04 12:08:49
Enter ".help" for usage hints.
sqlite> create table glove (word text,vector text);
sqlite> .import 'c:\Temp\glove.txt' glove
sqlite> create unique index glove_un_word on glove(word);
搜索
sqlite> .timer on
sqlite> select vector from glove where word = '7654321';
0.418 0.24968 -0.41242 0.1217 0.34527 -0.044457 -0.49688 -0.17862 -0.00066023 -0.6566 0.27843 -0.14767 -0.55677 0.14658 -0.0095095 0.011658 0.10204 -0.12792 -0.8443 -0.12181 -0.016801 -0.33279
0059213 0.0074449 0.17778 -0.15897 0.012041 -0.054223 -0.29871 -0.15749 -0.34758 -0.045637
Run Time: real 0.004 user 0.000000 sys 0.000000
sqlite>
关于c# - 如何存储 Web 应用程序用于单词搜索的 5GB 文本文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40957134/