Coder Social home page Coder Social logo

dialog-reflection's Introduction

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 [email protected]: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)
# => 田中さんなんですね。

dialog-reflection's People

Contributors

sadahry avatar

Watchers

 avatar

Forkers

kuritaro1122

dialog-reflection's Issues

How to Use を書く

  • optionによる語尾調整
  • overrideでのロジックの切替
  • モデルの切替(推奨しない)

2語以上のVALID/INVALID/CANCEL条件

「のに」「かも」といった語が
形態素解析では複数に分かち書きされてしまう。

よって2語以上でVALID/INVALID/CANCEL条件を
組めるようにすることで制御の幅が広がる

ユーザー辞書の活用

ユーザー辞書の追加は可能だが、spaCyモデルの再学習が必要
(固有名詞の場合はその限りではない)

e.g. ユーザー辞書に副助詞「かも」を適用

% ginza -m ja_core_news_sm
そういうことかも
# text = そういうことかも
1       そう    そう    ADV     副詞    _       2       advmod  _       SpaceAfter=No|Reading=ソウ
2       いう    いう    VERB    動詞-一般       _       3       acl     _       SpaceAfter=No|Inf=五段-ワア行,連体形-一般|Reading=イウ
3       こと    こと    NOUN    名詞-普通名詞-一般      _       0       root    _       SpaceAfter=No|Reading=コト
4       かも    かも    ADP     助詞-副助詞     _       3       case    _       SpaceAfter=No|Reading=カモ

難しいのかも
# text = 難しいの
1       難しい  難しい  ADJ     形容詞-一般     _       0       root    _       SpaceAfter=No|Inf=形容詞,連体形-一般|Reading=ムズカシイ
2       の      の      ADP     助詞-準体助詞   _       1       case    _       SpaceAfter=No|Reading=ノ

# text = かも
1       かも    かも    PROPN   助詞-副助詞     _       0       root    _       SpaceAfter=No|Reading=カモ

ちょっと判断つかないかも
# text = ちょっと判断つかないかも
1       ちょっと        ちょっと        ADV     副詞    _       3       advmod  _       SpaceAfter=No|Reading=チョット
2       判断    判断    VERB    名詞-普通名詞-サ変可能  _       3       obl     _       SpaceAfter=No|Reading=ハンダン
3       つか    つく    VERB    動詞-非自立可能 _       0       root    _       SpaceAfter=No|Inf=五段-カ行,未然形-一般|Reading=ツカ
4       ない    ない    AUX     助動詞  _       3       aux     _       SpaceAfter=No|Inf=助動詞-ナイ,終止形-一般|Reading=ナイ
5       かも    かも    PART    助詞-副助詞     _       3       mark    _       SpaceAfter=No|Reading=カモ

敬語の変換

_cut_suffix のロジックでは意味のある文字列を削除しないため、
それ以前の単語に含まれる敬語がそのまま返答文に含まれてしまう。

e.g., 行ってきました -> x: 行ってきましたんですね

def _cut_suffix(self, tokens: spacy.tokens.Span) -> spacy.tokens.Span:

よって、以前の単語に含まれる敬語を削除するロジックを実装する。

実装

root以降に「です」「ます」を含む場合に変換する
(e.g., 行ってきました -> 行ってきたんですね)

また敬語となる動詞等(e.g., いらっしゃる)には、今回対応しない。
理由は、敬語のままのほうがいいケースもあるため判断が困難であるため。
(e.g., いらっしゃいの掛け声が苦手で -> 来なさいの掛け声が苦手なんですね)

None の ハンドリング

Katsuyoの種類によっては対応のない活用形が存在する
e.g. 意志推量「う」の未然形

こういったパターンへの対処漏れが発生したとき全部exceptionだと
取り回ししづらくなるため、

NoneのTypeErrorが発生してもwarningで済むよう調整する

>>> try:
...     "a" + None
... except TypeError as e:
...     print(e)
... 
can only concatenate str (not "NoneType") to str

方言対応

  • すべての方言が辞書に含まれている訳ではないため辞書の追加が必要
  • 辞書の追加には係り受け対応が必要
    • #2 を先に対応する必要がある

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.