erlang学习:用ETS和DETS存储数据2

今日学习创建一个ETS表

下面是代码实现

erlang 复制代码
-module(lib_trigrams).

-export([make_ets_set/0, make_ets_ordered_set/0]).
for_each_trigram_in_the_english_language(F, A0) ->
  {ok, Bin0} = file:read_file("test.txt"),
  Bin = zlib:gunzip(Bin0),
  scan_word_list(binary_to_list(Bin), F, A0).


scan_word_list([], _, A) ->
  A;
scan_word_list(L, F, A) ->
  {word, L1} = get_next_word(L, []),
  A1 = scan_trigrams([$\s | word], F, A),
  scan_word_list(L1, F, A1).

get_next_word([$\r, $\n | T], L) ->
  {lists:reverse([$\s | L]), T};
get_next_word([H | T], L) ->
  get_next_word(T, [H | L]);
get_next_word([], L) ->
  {lists:reverse([$\s | L]), []}.


scan_trigrams([X, Y, Z], F, A) ->
  F([X, Y, Z], A);
scan_trigrams([X, Y, Z | T], F, A) ->
  A1 = F([X, Y, Z], A),
  scan_trigrams([Y, Z | T], F, A1);
scan_trigrams(_, _, A) ->
  A.


make_ets_ordered_set() -> make_a_set(ordered_set, "trigramsOS.tab").
make_ets_set() -> make_a_set(set, "trigramsS.tab").


make_a_set(Type, FileName) ->
  Tab = ets:new(table, [Type]),
  F = fun(Str, _) -> ets:insert(Tab, {list_to_binary(Str)}) end,
  for_each_trigram_in_the_english_language(F, 0),
  ets:tab2file(Tab, FileName),
  Size = ets:info(Tab, size),
  ets:delete(Tab),
  Size.

书上代码中的reverse没有lists,因此会报错,需要改为lists:reverse

搞了个txt文本之后报错为数据异常,有点没搞明白txt文档里面要存什么数据

相关推荐
一隅论数智1 天前
给AI一张“业务概念地图“:本体如何从哲学走向企业智能
大数据·人工智能·经验分享·笔记·学习·学习方法·政务
XiHongShi20161 天前
STM32F407 RTC定时器例程,建议保存
stm32·单片机·学习
爱吃苹果的日记本1 天前
离散数学第六课
学习·离散数学
AI职业加油站1 天前
AI智能体应用工程师证书:政策红利下的职业新风口
大数据·运维·人工智能·学习·职场发展
旖旎夜光1 天前
力控面试题 01.01: 判定字符是否唯一(位运算) —— 题解
c++·学习·算法·leetcode·力控
奇思妙想聪明勤奋的小羊1 天前
DeepAgents第5章:子Agent 与上下文隔离—让 Agent学会委派
人工智能·python·学习·语言模型
霍霍的袁1 天前
【C++】map 和 set 的使用 | 从用法到底层
开发语言·c++·学习·visual studio
彧azz1 天前
Linux 环境下 Redis 学习总结:数据类型、持久化、锁、事务、主从与缓存问题
linux·redis·笔记·学习·面试
我爱cope1 天前
【操作系统 | 计算机硬件:CPU、内存与 I/O 如何支撑操作系统?】
学习·操作系统