An explainable styler for the Korean language
Project description
Politely
| A rule-based politeness styler for the Korean language |
|---|
Quick Start ๐
1๏ธโฃ Install politely
pip3 install politely
2๏ธโฃ Split your text into sentences
Split your text into sentences with whatever tools you prefer. Here, we use kiwipiepy for the sake of demonstration:
from kiwipiepy import Kiwi
# an excerpt from ๋๋ฐฑ๊ฝ (๊น์ ์ )
text = """์์๋ฆฌ๋ฅผ ๋๋ฃจ ๋์ด๋๋ค๊ฐ ๋จ์ด ๋ค์๊น๋ด ์์ผ๋ก ์
์ ํ์ด๋ง๊ณ ๋ ๊ทธ ์์์ ๊น๊น๋๋ค. ๋ณ๋ก ์ฐ์ค์ธ ๊ฒ๋ ์๋๋ฐ ๋ ์จ๊ฐ ํ๋ฆฌ๋๋ ์ด ๋์ ๊ณ์ง์ ๊ฐ ๋ฏธ์ณค๋ ํ๊ณ ์์ฌํ์๋ค.
๊ฒ๋ค๊ฐ ์กฐ๊ธ ๋ค์๋ ์ ์ง๊ป๋ฅผ ํ ๊ธํ ๊ธ ๋์๋ณด๋๋ ํ์ฃผ์น๋ง์ ์์ผ๋ก ๊ผ๋ ๋ฐ๋ฅธ์์ ๋ฝ์์ ๋์ ํฑ๋ฐ์ผ๋ก ๋ถ์ฅ ๋ด๋ฏธ๋ ๊ฒ์ด๋ค.
์ธ์ ๊ตฌ์ ๋์ง ๋์ด ๊น์ด ํฑ ๋ผ์น๋ ๊ตต์ ๊ฐ์ ์ธ ๊ฐ๊ฐ ์์ ๋ฟ๋ฏ์ด ์ฅ์๋ค. "๋ ์ง์ ์ด๊ฑฐ ์์ง?" ํ๊ณ ์์์๋ ํฐ์๋ฆฌ๋ฅผ ํ๊ณ ๋ ์ ๊ฐ ์ค ๊ฒ์ ๋จ์ด ์๋ฉด์ ํฐ์ผ๋ ํ
๋ ์ฌ๊ธฐ์ ์ผ๋ฅธ ๋จน์ด ๋ฒ๋ฆฌ๋๋ค.
๊ทธ๋ฆฌ๊ณ ๋ ํ๋ ์๋ฆฌ๊ฐ, "๋ ๋ด๊ฐ์๊ฐ ๋ง์๋จ๋ค." "๋ ๊ฐ์ ์ ๋จน๋๋ค. ๋๋ ๋จน์ด๋ผ." ๋๋ ๊ณ ๊ฐ๋ ๋๋ฆฌ์ง ์๊ณ ์ผํ๋ ์์ผ๋ก ๊ทธ ๊ฐ์๋ฅผ ๋๋ก ์ด๊นจ ๋๋จธ๋ก ์ฅ ๋ฐ์ด ๋ฒ๋ ธ๋ค.
๊ทธ๋ฌ๋๋ ๊ทธ๋๋ ๊ฐ๋ ๊ธฐ์์ด ์๊ณ , ๋ฟ๋ง ์๋๋ผ ์๊ทผ์๊ทผํ๊ณ ์ฌ์์น ์๊ฒ ์จ์๋ฆฌ๊ฐ ์ ์ ๊ฑฐ์น ์ด์ง๋ค. ์ด๊ฑด ๋ ๋ญ์ผ ์ถ์ด์ ๊ทธ๋์์ผ ๋น๋ก์ ๋์๋ค๋ณด๋ ๋๋ ์ฐธ์ผ๋ก ๋๋๋ค.
์ฐ๋ฆฌ๊ฐ ์ด ๋๋ค์ ๋ค์ด์จ ๊ฒ์ ๊ทผ ์ผ๋
์งธ ๋์ด์ค์ง๋ง ์ฌํ๊ป ๊ฐ๋ฌด์ก์กํ ์ ์์ด์ ์ผ๊ตด์ด ์ด๋ ๊ฒ๊น์ง ํ๋น๋ฌด์ฒ๋ผ ์๋นจ๊ฐ์ง ๋ฒ์ด ์์๋ค.
๊ฒ๋ค๊ฐ ๋์ ๋
์ ์ฌ๋ฆฌ๊ณ ํ์ฐธ ๋๋ฅผ ์๋ ๊ฒ ์์๋ณด๋๋ ๋์ค์๋ ๋๋ฌผ๊น์ง ์ด๋ฆฌ๋ ๊ฒ์ด ์๋๋.
๊ทธ๋ฆฌ๊ณ ๋ฐ๊ตฌ๋๋ฅผ ๋ค์ ์ง์ด๋ค๋๋ ์ด๋ฅผ ๊ผญ ์
๋ฌผ๊ณ ๋ ์์ด์ง ๋ฏ ์๋น ์ง ๋ฏ ๋
ผ๋์ผ๋ก ํกํ๊ฒ ๋ฌ์๋๋ ๊ฒ์ด๋ค."""
kiwi = Kiwi()
sents = [sent.text.strip() for sent in kiwi.split_into_sents(text)]
3๏ธโฃ Speak politely with Styler
Instantiate an object of Styler, and style your sentences in a polite or formal manner with it:
from politely import Styler
from pprint import pprint
styler = Styler()
pprint(" ".join([styler(sent, 1) for sent in sents])) # 1 = polite
('์์๋ฆฌ๋ฅผ ๋๋ฃจ ๋์ด๋๋ค๊ฐ ๋จ์ด ๋ค์๊น ๋ด ์์ผ๋ก ์
์ ํ์ด๋ง๊ณ ๋ ๊ทธ ์์์ ๊น๊น๋์. ๋ณ๋ก ์ฐ์ค์ธ ๊ฒ๋ ์๋๋ฐ ๋ ์จ๊ฐ ํ๋ฆฌ๋๋ ์ด ๋์ '
'๊ณ์ง์ ๊ฐ ๋ฏธ์ณค๋ ํ๊ณ ์์ฌํ์ด์. ๊ฒ๋ค๊ฐ ์กฐ๊ธ ๋ค์๋ ์ ์ง๊ป๋ฅผ ํ ๊ธํ ๊ธ ๋์๋ณด๋๋ ํ์ฃผ์น๋ง์ ์์ผ๋ก ๊ผ๋ ๋ฐ๋ฅธ ์์ ๋ฝ์์ ์ ํฑ '
'๋ฐ์ผ๋ก ๋ถ์ฅ ๋ด๋ฏธ๋ ๊ฒ์์. ์ธ์ ๊ตฌ์ ๋์ง ๋์ด ๊น์ด ํฑ ๋ผ์น๋ ๊ตต์ ๊ฐ์ ์ธ ๊ฐ๊ฐ ์์ ๋ฟ๋ฏ์ด ์ฅ์์ด์. "๋ ์ง์๋ ์ด ๊ฑฐ ์์ฃ ? '
'"ํ๊ณ ์์ ์๋ ํฐ ์๋ฆฌ๋ฅผ ํ๊ณ ๋ ์ ๊ฐ ์ค ๊ฒ์ ๋จ์ด ์๋ฉด์ ํฐ์ผ ๋ ํ
๋ ์ฌ๊ธฐ์ ์ผ๋ฅธ ๋จน์ด ๋ฒ๋ฆฌ๋์. ๊ทธ๋ฆฌ๊ณ ๋ ํ๋ ์๋ฆฌ๊ฐ,"๋น์ '
'๋ด ๊ฐ์๊ฐ ๋ง์์ด์. ""๋ ๊ฐ์ ์ ๋จน์ด์. ๋น์ ์ด๋ ๋จน์ด์. "์ ๋ ๊ณ ๊ฐ๋ ๋๋ฆฌ์ง ์๊ณ ์ผํ๋ ์์ผ๋ก ๊ทธ ๊ฐ์๋ฅผ ๋๋ก ์ด๊นจ ๋๋จธ๋ก ์ฅ '
'๋ฐ์ด ๋ฒ๋ ธ์ด์. ๊ทธ๋ฌ๋๋ ๊ทธ๋๋ ๊ฐ๋ ๊ธฐ์์ด ์๊ณ , ๋ฟ๋ง ์๋๋ผ ์๊ทผ์๊ทผํ๊ณ ์ฌ์ํ์ง ์๊ฒ ์จ์๋ฆฌ๊ฐ ์ ์ ๊ฑฐ์น ์ด์ ธ์. ์ด๊ฑฐ๋ ๋ ๋ญ์ผ '
'์ถ์ด์ ๊ทธ ๋์์ผ ๋น๋ก์ ๋์๋ค๋ณด๋ ์ ๋ ์ฐธ์ผ๋ก ๋๋์ด์. ์ ํฌ๊ฐ ์ด ๋๋ค์ ๋ค์ด์จ ๊ฒ์ ๊ทผ ์ผ ๋
์งธ ๋ผ ์ค์ง๋ง ์ฌํ๊ป ๊ฐ๋ฌด์ก์กํ ์ ์๋ '
'์ด์ ์ผ๊ตด์ด ์ด๋ ๊ฒ๊น์ง ํ๋น๋ฌด์ฒ๋ผ ์๋นจ๊ฐ์ง ๋ฒ์ด ์์์ด์. ๊ฒ๋ค๊ฐ ๋์ ๋
์ ์ฌ๋ฆฌ๊ณ ํ์ฐธ ์ ๋ฅผ ์๋ ๊ฒ ์์๋ณด๋๋ ๋์ค์๋ ๋๋ฌผ๊น์ง ์ด๋ฆฌ๋ '
'๊ฒ ์๋
์. ๊ทธ๋ฆฌ๊ณ ๋ฐ๊ตฌ๋๋ฅผ ๋ค์ ์ง์ด ๋ค๋๋ ์ด๋ฅผ ๊ผญ ์
๋ฌผ๊ณ ๋ ์์ด์ง ๋ฏ ์๋น ์ง ๋ฏ ๋
ผ๋์ผ๋ก ํกํ๊ฒ ๋ฌ์๋๋ ๊ฒ์์.')
pprint(" ".join([styler(sent, 2) for sent in sents])) # 2 = formal
('์์๋ฆฌ๋ฅผ ๋๋ฃจ ๋์ด๋๋ค๊ฐ ๋จ์ด ๋ค์๊น ๋ด ์์ผ๋ก ์
์ ํ์ด๋ง๊ณ ๋ ๊ทธ ์์์ ๊น๊น๋๋๋ค. ๋ณ๋ก ์ฐ์ค์ธ ๊ฒ๋ ์๋๋ฐ ๋ ์จ๊ฐ ํ๋ฆฌ๋๋ ์ด ๋์ '
'๊ณ์ง์ ๊ฐ ๋ฏธ์ณค๋ ํ๊ณ ์์ฌํ์ต๋๋ค. ๊ฒ๋ค๊ฐ ์กฐ๊ธ ๋ค์๋ ์ ์ง๊ป๋ฅผ ํ ๊ธํ ๊ธ ๋์๋ณด๋๋ ํ์ฃผ์น๋ง์ ์์ผ๋ก ๊ผ๋ ๋ฐ๋ฅธ ์์ ๋ฝ์์ ์ ํฑ '
'๋ฐ์ผ๋ก ๋ถ์ฅ ๋ด๋ฏธ๋ ๊ฒ๋๋ค. ์ธ์ ๊ตฌ์ ๋์ง ๋์ด ๊น์ด ํฑ ๋ผ์น๋ ๊ตต์ ๊ฐ์ ์ธ ๊ฐ๊ฐ ์์ ๋ฟ๋ฏ์ด ์ฅ์์ต๋๋ค. "๋ ์ง์๋ ์ด ๊ฑฐ '
'์์ต๋๊น? "ํ๊ณ ์์ ์๋ ํฐ ์๋ฆฌ๋ฅผ ํ๊ณ ๋ ์ ๊ฐ ์ค ๊ฒ์ ๋จ์ด ์๋ฉด์ ํฐ์ผ ๋ ํ
๋ ์ฌ๊ธฐ์ ์ผ๋ฅธ ๋จน์ด ๋ฒ๋ฆฌ๋๋๋ค. ๊ทธ๋ฆฌ๊ณ ๋ ํ๋ '
'์๋ฆฌ๊ฐ,"๋น์ ๋ด ๊ฐ์๊ฐ ๋ง์์ต๋๋ค. ""๋ ๊ฐ์ ์ ๋จน์ต๋๋ค. ๋น์ ์ด๋ ๋จน์ญ์์ค. "์ ๋ ๊ณ ๊ฐ๋ ๋๋ฆฌ์ง ์๊ณ ์ผํ๋ ์์ผ๋ก ๊ทธ ๊ฐ์๋ฅผ '
'๋๋ก ์ด๊นจ ๋๋จธ๋ก ์ฅ ๋ฐ์ด ๋ฒ๋ ธ์ต๋๋ค. ๊ทธ๋ฌ๋๋ ๊ทธ๋๋ ๊ฐ๋ ๊ธฐ์์ด ์๊ณ , ๋ฟ๋ง ์๋๋ผ ์๊ทผ์๊ทผํ๊ณ ์ฌ์ํ์ง ์๊ฒ ์จ์๋ฆฌ๊ฐ ์ ์ '
'๊ฑฐ์น ์ด์ง๋๋ค. ์ด๊ฑฐ๋ ๋ ๋ญ์ผ ์ถ์ด์ ๊ทธ ๋์์ผ ๋น๋ก์ ๋์๋ค๋ณด๋ ์ ๋ ์ฐธ์ผ๋ก ๋๋์ต๋๋ค. ์ ํฌ๊ฐ ์ด ๋๋ค์ ๋ค์ด์จ ๊ฒ์ ๊ทผ ์ผ ๋
์งธ ๋ผ '
'์ค์ง๋ง ์ฌํ๊ป ๊ฐ๋ฌด์ก์กํ ์ ์๋ ์ด์ ์ผ๊ตด์ด ์ด๋ ๊ฒ๊น์ง ํ๋น๋ฌด์ฒ๋ผ ์๋นจ๊ฐ์ง ๋ฒ์ด ์์์ต๋๋ค. ๊ฒ๋ค๊ฐ ๋์ ๋
์ ์ฌ๋ฆฌ๊ณ ํ์ฐธ ์ ๋ฅผ ์๋ ๊ฒ '
'์์๋ณด๋๋ ๋์ค์๋ ๋๋ฌผ๊น์ง ์ด๋ฆฌ๋ ๊ฒ ์๋๋๋ค. ๊ทธ๋ฆฌ๊ณ ๋ฐ๊ตฌ๋๋ฅผ ๋ค์ ์ง์ด ๋ค๋๋ ์ด๋ฅผ ๊ผญ ์
๋ฌผ๊ณ ๋ ์์ด์ง ๋ฏ ์๋น ์ง ๋ฏ ๋
ผ๋์ผ๋ก '
'ํกํ๊ฒ ๋ฌ์๋๋ ๊ฒ๋๋ค.')
4๏ธโฃ add_rules of your own
you can add your own rules with add_rules method. Use politely.SELF to keep MASK as-is. Use politely.NULL to remove MASK.
from politely import SELF, NULL
styler.add_rules(
{
"์ด๐ทVCP๐(?P<MASK>๋ค๐ทEF)": (
{SELF},
{"์์๐ทEF"}, # ์์ (X) ์์ (O)
{"์
๋๋ค๐ทEF"},
),
"(?P<MASK>์ด๐ทVCP)๐๋ค๐ทEF": (
{SELF},
{NULL}, # ์ง์ฐ๊ธฐ
{NULL} # ์ง์ฐ๊ธฐ
)
})
sent = "ํ๊ธ์ ํ๊ตญ์ ๊ธ์์ด๋ค."
print(styler(sent, 1))
ํ๊ธ์ ํ๊ตญ์ ๊ธ์์์.
Here is another example of adding rules.styler.logs['guess']['out'] keeps track of all the candidates with scores.
from politely import SELF
styler.add_rules(
{
r"(?P<MASK>(์๋น |์๋ฒ์ง|์๋ฒ๋)๐ทNNG)": (
{f"์๋น ๐ทNNG"},
{f"์๋ฒ์ง๐ทNNG", f"์๋ฒ๋๐ทNNG"},
{f"์๋ฒ์ง๐ทNNG", f"์๋ฒ๋๐ทNNG"}
),
r"(์๋น |์๋ฒ์ง|์๋ฒ๋)๐ทNNG๐(?P<MASK>\S+?๐ทJKS)": (
{SELF}, # no change, replace with the original
{f"๊ป์๐ทJKS"},
{f"๊ป์๐ทJKS"}
),
r"(?P<MASK>แซ๋ค๐ทEF)": (
{SELF}, # no change, replace with the original
{"์๐ทEP๐์ด์๐ทEF"},
{"์๐ทEP๐์ต๋๋ค๐ทEF"},
)
}
)
sent = "์๋น ๊ฐ ์ ์ค์ ๋ค์ด๊ฐ๋ค."
print(styler(sent, 1))
from pprint import pprint
pprint(styler.logs['guess']['out'])
์๋ฒ์ง๊ป์ ์ ์ค์ ๋ค์ด๊ฐ์
์.
[(['์๋ฒ์ง๐ทNNG', '๊ป์๐ทJKS', '์ ์ค๐ทNNG', '์๐ทJKB', '๋ค์ด๊ฐ๐ทVV', '์๐ทEP', '์ด์๐ทEF', '.๐ทSF'],
0.0125),
(['์๋ฒ๋๐ทNNG', '๊ป์๐ทJKS', '์ ์ค๐ทNNG', '์๐ทJKB', '๋ค์ด๊ฐ๐ทVV', '์๐ทEP', '์ด์๐ทEF', '.๐ทSF'],
0.0125)]
5๏ธโฃ Take contexts into account with language models
heuristic scorer is fast, but it is a purely rule-based scorer. It thus falls short at taking context into account:
styler = Styler(scorer="heuristic")
print("##### lm์ ์ฐ์ง ์๋ ๊ฒฝ์ฐ ๋งฅ๋ฝ ๊ณ ๋ ค X ######")
print(styler("๋ด์ผ ์ ๋ ๊ฐ์ด ์ ์ฌ ๋จน์ด์.", 0))
##### lm์ ์ฐ์ง ์๋ ๊ฒฝ์ฐ ๋งฅ๋ฝ ๊ณ ๋ ค X ######
๋ด์ผ ๋๋ ๊ฐ์ด ์ ์ฌ ๋จน์ด.
sbg scorer (SkipBigram) is a bit slower, but does take context into account.
# Pytorch is required to use GPT2 scorer. Install the version that fits your environment.
pip3 install torch
from politely.modeling_gpt2_scorer import GPT2Scorer
styler = Styler(scorer="sbg") # uses GPT2Scorer by default
print("##### lm์ ์ฐ๋ ๊ฒฝ์ฐ ๋งฅ๋ฝ ๊ณ ๋ ค O ######")
print(styler("๋ด์ผ ์ ๋ ๊ฐ์ด ์ ์ฌ ๋จน์ด์.", 0))
##### lm์ ์ฐ๋ ๊ฒฝ์ฐ ๋งฅ๋ฝ ๊ณ ๋ ค O ######
๋ด์ผ ๋๋ ๊ฐ์ด ์ ์ฌ ๋จน์. # ๊ถ์ ๊ฐ ์๋ ์ฒญ์ ์ด๋ฏ๋ก ์ด๊ฒ ๋ง์
gpt2 scorer is the most accurate, but it is also the slowest. GPU support will be added in the future,
but as of right now, this is an experimental option. If you want to use this as the scorer, you first need to install the optional dependencies for gpt2 scorer:
# need to install optional dependency
pip3 install "politely[gpt2]"
styler = Styler(scorer="gpt2")
print("##### lm์ ์ฐ๋ ๊ฒฝ์ฐ ๋งฅ๋ฝ ๊ณ ๋ ค O ######")
print(styler("๋ด์ผ ์ ๋ ๊ฐ์ด ์ ์ฌ ๋จน์ด์.", 0))
##### lm์ ์ฐ๋ ๊ฒฝ์ฐ ๋งฅ๋ฝ ๊ณ ๋ ค O ######
๋ด์ผ ์ ๋ ๊ฐ์ด ์ ์ฌ ๋จน์ผ๋ฌ ๊ฐ์. # ๊ถ์ ์ด๋ฏ๋ก ์ด๊ฒ ๋ง์
Hosting the interactive demo
You can either host the interactive demo locally (You have to setup your own OPENAI_API_KEY)
export OPENAI_API_KEY = ...
# host the demo via streamlit
streamlit run main_streamlit.py
| Or just visit the demo we are hosting for you |
|---|
By whom? ๐
- funded by: Faculty of Oriental Studies at the University of Oxford
- led & developed by: Jieun Kiaer (Associate Professor of Korean Language and Linguistics at the University of Oxford)
- co-developed by: Research assistant Eu-Bin KIM (Msc. in Applied Linguistics at the University of Oxford, Bsc. in AI at the University of Manchester )
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file politely-4.1.0.tar.gz.
File metadata
- Download URL: politely-4.1.0.tar.gz
- Upload date:
- Size: 14.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.1 CPython/3.12.2 Darwin/23.1.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a3c3549521ca36f7f355acb6ae366b7a9461b4d6c82b8d09f388135aec8ab93d
|
|
| MD5 |
91eb4e2dc1a2138457d9fd4a480c5e97
|
|
| BLAKE2b-256 |
25eaa4d84a87a589a16de32ac3a43f71c20ba90995e7d900b7b2fee8473bea02
|
File details
Details for the file politely-4.1.0-py3-none-any.whl.
File metadata
- Download URL: politely-4.1.0-py3-none-any.whl
- Upload date:
- Size: 13.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.1 CPython/3.12.2 Darwin/23.1.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
95f11aae13899cae9f34d246ecd9345aeb0f5aaa4de9ad7beb3911f594e073c5
|
|
| MD5 |
ff2d31f13908ff846f7a558c523a2196
|
|
| BLAKE2b-256 |
ac84cc2010d0f9ff451f1243635044f1910ad850cf706decb62546125d535d97
|