A library for dialog systems that attempt to respond to messages as Reflective Listening.
Project description
Dialog Reflection
A library for dialog systems that attempt to respond to messages as Reflective Listening.
Demo
Need git
and poetry
to run this demo.
$ git clone git@github.com:sadahry/dialog-reflection.git
$ cd dialog-reflection
$ poetry install
$ poetry run python examples/interactive_ja.py
Install
Need python
>= 3.10
$ pip install dialog-reflection
How to Use (Japanese)
SpaCyの日本語モデルのインストールが必要
ja_ginza==5.1.2
でテスト済- 他モデルの利用は可能だが推奨しない
- 係り受けや形態素解析によって不整合があるため利用前にテストを推奨
$ pip install ja_ginza==5.1.2
実行例
from dialog_reflection.lang.ja.reflector import JaSpacyReflector
refactor = JaSpacyReflector(model="ja_ginza")
message = "今日は旅行へ行った"
reflection_text = refactor.reflect(message)
print(reflection_text)
# => 旅行へ行ったんですね。
Builderを使う例
from dialog_reflection.lang.ja.reflection_text_builder import (
JaSpacyPlainReflectionTextBuilder,
)
import spacy
nlp = spacy.load("ja_ginza")
builder = JaSpacyPlainReflectionTextBuilder()
message = "今日は旅行へ行った"
doc = nlp(message)
# some code...
reflection_text = builder.build(doc)
print(reflection_text)
# => 旅行へ行ったんですね。
語尾の調整
op
を変更することで語尾を調整可能
from dialog_reflection.lang.ja.reflector import JaSpacyReflector
from dialog_reflection.lang.ja.reflection_text_builder import (
JaSpacyPlainReflectionTextBuilder,
)
from dialog_reflection.lang.ja.reflection_text_builder_option import (
JaSpacyPlainReflectionTextBuilderOption,
)
refactor = JaSpacyReflector(
model="ja_ginza",
builder=JaSpacyPlainReflectionTextBuilder(
op=JaSpacyPlainRelflectionTextBuilderOption(
fn_last_token_taigen=lambda token: token.text + "なんだね。",
fn_last_token_yougen=lambda token: token.lemma_ + "んだね。",
)
),
)
message = "今日は旅行へ行った"
reflection_text = refactor.reflect(message)
print(reflection_text)
# => 旅行へ行ったんだね。
その他設定項目は reflection_text_builder_option.py を確認してください
ロジックのカスタマイズ
JaSpacyPlainReflectionTextBuilder
を override することでロジックをカスタマイズ可能
from dialog_reflection.reflection_cancelled import (
ReflectionCancelled,
)
from dialog_reflection.cancelled_reason import (
NoValidSentence,
)
from dialog_reflection.lang.ja.reflector import JaSpacyReflector
from dialog_reflection.lang.ja.reflection_text_builder import (
JaSpacyPlainReflectionTextBuilder,
)
import spacy
class CustomReflectionTextBuilder(JaSpacyPlainReflectionTextBuilder):
def extract_tokens(self, doc: spacy.tokens.Doc) -> spacy.tokens.Span:
propn_token = next(filter(lambda token: token.pos_ == "PROPN", doc), None)
if propn_token is None:
raise ReflectionCancelled(reason=NoValidSentence(doc=doc))
if propn_token.dep_ in ["compound", "numpound"]:
return doc[propn_token.i : propn_token.head.i + 1]
return doc[propn_token.i : propn_token.i + 1]
refactor = JaSpacyReflector(
model="ja_ginza",
builder=CustomReflectionTextBuilder(),
)
message = "今日は田中さんと旅行へ行った"
reflection_text = refactor.reflect(message)
print(reflection_text)
# => 田中さんなんですね。
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
dialog-reflection-0.1.2.tar.gz
(22.4 kB
view details)
Built Distribution
File details
Details for the file dialog-reflection-0.1.2.tar.gz
.
File metadata
- Download URL: dialog-reflection-0.1.2.tar.gz
- Upload date:
- Size: 22.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.2.1 CPython/3.10.7 Darwin/22.1.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0b5a17fcf89002b67beb0c878978f9c62d6e9fdf68af7f9a77bd3b610bdb528d |
|
MD5 | da5c735e68abbae6d2b72b6f24853bbc |
|
BLAKE2b-256 | 64538b8c83fb2060cb182e37900f12c0c9d933507993a1afb21f6c9d8f10c2e0 |
File details
Details for the file dialog_reflection-0.1.2-py3-none-any.whl
.
File metadata
- Download URL: dialog_reflection-0.1.2-py3-none-any.whl
- Upload date:
- Size: 24.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.2.1 CPython/3.10.7 Darwin/22.1.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a8ebd77faef59bee3f518a8ec319ab4fccfa08282d8529cffb683a0ba50b7ce2 |
|
MD5 | a2781c4497ab6e87e78b94df33c8f0a1 |
|
BLAKE2b-256 | de8057e7c0efd6ee8716b8cc27f899d732328ca1186f8cc46ec3e6cafb5fb61e |