饮冰十年-人工智能-ArangoDB-03-AQL
上一篇:饮冰十年-人工智能-ArangoDB-02-AQL vs SQL
本课程的示例数据集基于小说和电视连续剧《三国演义》。它包括两种语言的性格特征、一些人物关系,以及一小部分地点。
ToDo:整体完成后补充
一、基本 CRUD 操作
1、创建集合
我们无法使用 AQL 创建集合,我们将使用 Web 界面创建集合。
插入记录
INSERT document INTO collectionName
1.1 插入一条文档
1.2 插入一条文档返回插入的值
1.3 插入多行文档
2、阅读文档
到目前为止,角色集合中有几个文档。我们可以再次使用FOR
循环检索它们。然而,这次我们用它来浏览集合中的所有文档,而不是数组:
FOR c IN Characters
RETURN c
2.1 通过id查找
RETURN DOCUMENT("Characters", "caocao") // --- or --- RETURN DOCUMENT("Characters/caocao")
2.2 通过ids查找
RETURN DOCUMENT("Characters",["liubei","guanyu"]) // --- or --- RETURN DOCUMENT(["Characters/liubei", "Characters/guanyu"])
3、更新文档
3.1 通过key更新
UPDATE documentKey WITH object IN collection
UPDATE "liubei" WITH {"title":"刘皇叔"} IN Characters
3.2 通过key值表达式更新
UPDATE keyExpression WITH object IN collection
UPDATE {"_key":"liubei"} WITH {"title":"昭烈帝"} IN Characters RETURN NEW
3.3 通过document更新
UPDATE document WITH object IN collection
FOR c IN Characters UPDATE c WITH { season: 1 } IN Characters
3.4 REPLACE 替换
UPDATE 局部修改,但其余部分没有被触及。要替换整个文档内容,您可以使用REPLACE。
REPLACE "liubei" WITH { "_key": "liubei", "name": "刘备", "faction": "蜀", "title": "蜀汉皇帝", "age": 63, "gender": "Male", "features": ["B", "D", "I", "J"] } IN Characters
4、删除文件
要从集合中完全删除文档,需要进行REMOVE
操作。它的工作原理与其他修改操作类似,但没有WITH
子句:
4.1 单独删除
REMOVE "liubei" IN Characters
4.2 循环删除(可加过滤条件)
FOR c IN Characters
REMOVE c IN Characters
二、查询
1、Filter 过滤
-
比较操作符:
- 等于:
==
- 不等于:
!=
- 小于:
<
- 大于:
>
- 小于等于:
<=
- 大于等于:
>=
- 等于:
-
范围查询:
- 在给定值集合中:
IN
-
字符串匹配:
- 模糊匹配:
LIKE
- 模糊匹配:
-
逻辑操作符:
- 逻辑与:
AND &&
- 逻辑或:
OR ||
- 逻辑非:
NOT !
- 逻辑与:
FOR i IN Characters FILTER (i.age >= 50 AND i.age<60) || i.name LIKE '曹%' FILTER i.faction IN ["魏","蜀"] RETURN i
同样支持深层查询
FOR i IN Characters FILTER i.features[0] IN ["A","B"] RETURN i
2、Sort 排序+Limit
FOR c IN Characters FILTER c.age //过滤掉没有年龄属性的数据 SORT c.faction desc, c.name LIMIT 2,7 RETURN { name: c.name, age: c.age }
import pyaudio from aip import AipSpeech from setting import APP_ID, API_KEY, SECRET_KEY client = AipSpeech(APP_ID, API_KEY, SECRET_KEY) # 获取默认输入设备索引 def get_default_input_device_index(): audio = pyaudio.PyAudio() for i in range(audio.get_device_count()): dev_info = audio.get_device_info_by_index(i) if dev_info['maxInputChannels'] > 0: return i return None # 录音函数 def record_audio(record_seconds): CHUNK = 1024 FORMAT = pyaudio.paInt16 CHANNELS = 1 RATE = 16000 audio = pyaudio.PyAudio() input_device_index = get_default_input_device_index() if input_device_index is None: print("找不到可用的音频输入设备") return stream = audio.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, input_device_index=input_device_index, frames_per_buffer=CHUNK) print("开始录音...") frames = [] for i in range(0, int(RATE / CHUNK * record_seconds)): data = stream.read(CHUNK) frames.append(data) print("录音结束.") stream.stop_stream() stream.close() audio.terminate() audio_buf = b''.join(frames) # 调用百度语音识别的 API,将音频数据转换为文字 result = client.asr(audio_buf, 'pcm', 16000, {'dev_pid': 1537}) print(result['result'][0]) # 主函数 def main(): record_seconds = 15 # 录音时长 record_audio(record_seconds) if __name__ == "__main__": main()