Skip to content

Commit a2557f8

Browse files
committed
Rename core submodule files to core.py
1 parent 13937e8 commit a2557f8

12 files changed

Lines changed: 164 additions & 128 deletions

File tree

pythainlp/corpus/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def corpus_db_path() -> str:
6161
return _CORPUS_DB_PATH
6262

6363

64-
from pythainlp.corpus.corpus import (
64+
from pythainlp.corpus.core import (
6565
download,
6666
get_corpus,
6767
get_corpus_db_detail,

pythainlp/soundex/__init__.py

Lines changed: 9 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -4,63 +4,18 @@
44
55
Has three systems to choose from: Udom83 (default), LK82, and MetaSound
66
"""
7+
8+
__all__ = [
9+
"soundex",
10+
"lk82",
11+
"metasound",
12+
"udom83",
13+
]
14+
715
from pythainlp.soundex.lk82 import lk82
816
from pythainlp.soundex.metasound import metasound
917
from pythainlp.soundex.udom83 import udom83
1018

11-
# Other Thai soundex systems (not implemented yet): Arun91, KSS97
12-
# [KSS97] https://linux.thai.net/~thep/soundex/soundex.html
13-
1419
DEFAULT_SOUNDEX_ENGINE = "udom83"
1520

16-
17-
def soundex(text: str, engine: str = DEFAULT_SOUNDEX_ENGINE) -> str:
18-
"""
19-
This function converts Thai text into phonetic code.
20-
21-
:param string text: word
22-
:param str engine: soundex engine
23-
:return: Soundex code
24-
:rtype: str
25-
26-
:Options for engine:
27-
* *udom83* (default) - Thai soundex algorithm proposed
28-
by Vichit Lorchirachoonkul [#udom83]_
29-
* *lk82* - Thai soundex algorithm proposed by
30-
Wannee Udompanich [#lk82]_
31-
* *metasound* - Thai soundex algorithm based on a combination
32-
of Metaphone and Soundex proposed by Snae & Brückner [#metasound]_
33-
34-
:Example:
35-
::
36-
37-
from pythainlp.soundex import soundex
38-
39-
soundex("ลัก"), soundex("ลัก", engine='lk82'), \\
40-
soundex("ลัก", engine='metasound')
41-
# output: ('ร100000', 'ร1000', 'ล100')
42-
43-
soundex("รัก"), soundex("รัก", engine='lk82'), \\
44-
soundex("รัก", engine='metasound')
45-
# output: ('ร100000', 'ร1000', 'ร100')
46-
47-
soundex("รักษ์"), soundex("รักษ์", engine='lk82'), \\
48-
soundex("รักษ์", engine='metasound')
49-
# output: ('ร100000', 'ร1000', 'ร100')
50-
51-
soundex("บูรณการ"), soundex("บูรณการ", engine='lk82'), \\
52-
soundex("บูรณการ", engine='metasound')
53-
# output: ('บ931900', 'บE419', 'บ551')
54-
55-
soundex("ปัจจุบัน"), soundex("ปัจจุบัน", engine='lk82'), \\
56-
soundex("ปัจจุบัน", engine='metasound')
57-
# output: ('ป775300', 'ป3E54', 'ป223')
58-
"""
59-
if engine == "lk82":
60-
_soundex = lk82
61-
elif engine == "metasound":
62-
_soundex = metasound
63-
else: # default, use "udom83"
64-
_soundex = udom83
65-
66-
return _soundex(text)
21+
from pythainlp.soundex.core import soundex

pythainlp/soundex/core.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Thai soundex
4+
5+
Has three systems to choose from: Udom83 (default), LK82, and MetaSound
6+
"""
7+
from pythainlp.soundex.lk82 import lk82
8+
from pythainlp.soundex.metasound import metasound
9+
from pythainlp.soundex.udom83 import udom83
10+
from pythainlp.soundex import DEFAULT_SOUNDEX_ENGINE
11+
12+
# Other Thai soundex systems (not implemented yet): Arun91, KSS97
13+
# [KSS97] https://linux.thai.net/~thep/soundex/soundex.html
14+
15+
16+
def soundex(text: str, engine: str = DEFAULT_SOUNDEX_ENGINE) -> str:
17+
"""
18+
This function converts Thai text into phonetic code.
19+
20+
:param string text: word
21+
:param str engine: soundex engine
22+
:return: Soundex code
23+
:rtype: str
24+
25+
:Options for engine:
26+
* *udom83* (default) - Thai soundex algorithm proposed
27+
by Vichit Lorchirachoonkul [#udom83]_
28+
* *lk82* - Thai soundex algorithm proposed by
29+
Wannee Udompanich [#lk82]_
30+
* *metasound* - Thai soundex algorithm based on a combination
31+
of Metaphone and Soundex proposed by Snae & Brückner [#metasound]_
32+
33+
:Example:
34+
::
35+
36+
from pythainlp.soundex import soundex
37+
38+
soundex("ลัก"), soundex("ลัก", engine='lk82'), \\
39+
soundex("ลัก", engine='metasound')
40+
# output: ('ร100000', 'ร1000', 'ล100')
41+
42+
soundex("รัก"), soundex("รัก", engine='lk82'), \\
43+
soundex("รัก", engine='metasound')
44+
# output: ('ร100000', 'ร1000', 'ร100')
45+
46+
soundex("รักษ์"), soundex("รักษ์", engine='lk82'), \\
47+
soundex("รักษ์", engine='metasound')
48+
# output: ('ร100000', 'ร1000', 'ร100')
49+
50+
soundex("บูรณการ"), soundex("บูรณการ", engine='lk82'), \\
51+
soundex("บูรณการ", engine='metasound')
52+
# output: ('บ931900', 'บE419', 'บ551')
53+
54+
soundex("ปัจจุบัน"), soundex("ปัจจุบัน", engine='lk82'), \\
55+
soundex("ปัจจุบัน", engine='metasound')
56+
# output: ('ป775300', 'ป3E54', 'ป223')
57+
"""
58+
if engine == "lk82":
59+
_soundex = lk82
60+
elif engine == "metasound":
61+
_soundex = metasound
62+
else: # default, use "udom83"
63+
_soundex = udom83
64+
65+
return _soundex(text)

pythainlp/spell/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@
1414

1515
DEFAULT_SPELL_CHECKER = NorvigSpellChecker()
1616

17-
from pythainlp.spell.spell import correct, spell
17+
from pythainlp.spell.core import correct, spell

pythainlp/summarize/__init__.py

Lines changed: 6 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,12 @@
11
# -*- coding: utf-8 -*-
22
"""
3-
Summarization
3+
Text summarization
44
"""
55

6-
from typing import List
6+
__all__ = [
7+
"summarize",
8+
]
79

8-
from pythainlp.tokenize import sent_tokenize
10+
DEFAULT_SUMMARIZE_ENGINE = "freq"
911

10-
from .freq import FrequencySummarizer
11-
12-
13-
def summarize(
14-
text: str, n: int, engine: str = "frequency", tokenizer: str = "newmm"
15-
) -> List[str]:
16-
"""
17-
This function summarizes text based on frequency of words.
18-
19-
Under the hood, this function first tokenize sentence from the given
20-
text with :func:`pythainlp.tokenize.sent_tokenize`.
21-
Then, computes frequencies of tokenized words
22-
(with :func:`pythainlp.tokenize.word_tokenize`) in all sentences
23-
and normalized with maximum word frequency. The words with normalized
24-
frequncy that are less than 0.1 or greater than 0.9 will be
25-
filtered out from frequency dictionary. Finally, it picks *n* sentences
26-
with highest sum of normalized frequency from all words
27-
in the sentence and also appear in the frequency dictionary.
28-
29-
:param str text: text to be summarized
30-
:param int n: number of sentences to be included in the summary
31-
:param str engine: text summarization engine (By default: *frequency*).
32-
There is only one engine currently.
33-
:param str tokenizer: word tokenizer engine name (refer to
34-
:func:`pythainlp.tokenize.word_tokenize`).
35-
By default, *engine* is set to *newmm*
36-
37-
:return: list of selected sentences
38-
:rtype: list[str]
39-
40-
:Example:
41-
::
42-
43-
from pythainlp.summarize import summarize
44-
45-
text = '''
46-
ทำเนียบท่าช้าง หรือ วังถนนพระอาทิตย์
47-
ตั้งอยู่บนถนนพระอาทิตย์ เขตพระนคร กรุงเทพมหานคร
48-
เดิมเป็นบ้านของเจ้าพระยามหาโยธา (ทอเรียะ คชเสนี)
49-
บุตรเจ้าพระยามหาโยธานราธิบดีศรีพิชัยณรงค์ (พญาเจ่ง)
50-
ต้นสกุลคชเสนี เชื้อสายมอญ เจ้าพระยามหาโยธา (ทอเรีย)
51-
เป็นปู่ของเจ้าจอมมารดากลิ่นในพระบาทสมเด็จพระจอมเกล้าเจ้าอยู่หัว
52-
และเป็นมรดกตกทอดมาถึง พระเจ้าบรมวงศ์เธอ กรมพระนเรศรวรฤทธิ์
53-
(พระองค์เจ้ากฤดาภินิหาร)
54-
ต่อมาในรัชสมัยพระบาทสมเด็จพระจุลจอมเกล้าเจ้าอยู่หัวโปรดเกล้าฯ
55-
ให้สร้างตำหนัก 2 ชั้น
56-
เป็นที่ประทับของพระเจ้าบรมวงศ์เธอ
57-
กรมพระนเรศวรฤทิธิ์และเจ้าจอมมารดา
58-
ต่อมาเรียกอาคารหลักนี้ว่า ตำหนักเดิม
59-
'''
60-
61-
summarize(text, n=1)
62-
# output: ['บุตรเจ้าพระยามหาโยธานราธิบดีศรีพิชัยณรงค์']
63-
64-
summarize(text, n=3)
65-
# output: ['บุตรเจ้าพระยามหาโยธานราธิบดีศรีพิชัยณรงค์',
66-
# 'เดิมเป็นบ้านของเจ้าพระยามหาโยธา',
67-
# 'เจ้าพระยามหาโยธา']
68-
"""
69-
sents = []
70-
71-
if engine == "frequency":
72-
sents = FrequencySummarizer().summarize(text, n, tokenizer)
73-
else: # if engine not found, return first n sentences
74-
sents = sent_tokenize(text, engine="whitespace+newline")[:n]
75-
76-
return sents
12+
from pythainlp.summarize.core import summarize

pythainlp/summarize/core.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Text summarization
4+
"""
5+
6+
7+
from typing import List
8+
9+
from pythainlp.summarize import DEFAULT_SUMMARIZE_ENGINE
10+
from pythainlp.summarize.freq import FrequencySummarizer
11+
from pythainlp.tokenize import sent_tokenize
12+
13+
14+
def summarize(
15+
text: str,
16+
n: int,
17+
engine: str = DEFAULT_SUMMARIZE_ENGINE,
18+
tokenizer: str = "newmm",
19+
) -> List[str]:
20+
"""
21+
This function summarizes text based on frequency of words.
22+
23+
Under the hood, this function first tokenize sentence from the given
24+
text with :func:`pythainlp.tokenize.sent_tokenize`.
25+
Then, computes frequencies of tokenized words
26+
(with :func:`pythainlp.tokenize.word_tokenize`) in all sentences
27+
and normalized with maximum word frequency. The words with normalized
28+
frequncy that are less than 0.1 or greater than 0.9 will be
29+
filtered out from frequency dictionary. Finally, it picks *n* sentences
30+
with highest sum of normalized frequency from all words
31+
in the sentence and also appear in the frequency dictionary.
32+
33+
:param str text: text to be summarized
34+
:param int n: number of sentences to be included in the summary
35+
:param str engine: text summarization engine (By default: *frequency*).
36+
There is only one engine currently.
37+
:param str tokenizer: word tokenizer engine name (refer to
38+
:func:`pythainlp.tokenize.word_tokenize`).
39+
By default, *engine* is set to *newmm*
40+
41+
:return: list of selected sentences
42+
:rtype: list[str]
43+
44+
:Example:
45+
::
46+
47+
from pythainlp.summarize import summarize
48+
49+
text = '''
50+
ทำเนียบท่าช้าง หรือ วังถนนพระอาทิตย์
51+
ตั้งอยู่บนถนนพระอาทิตย์ เขตพระนคร กรุงเทพมหานคร
52+
เดิมเป็นบ้านของเจ้าพระยามหาโยธา (ทอเรียะ คชเสนี)
53+
บุตรเจ้าพระยามหาโยธานราธิบดีศรีพิชัยณรงค์ (พญาเจ่ง)
54+
ต้นสกุลคชเสนี เชื้อสายมอญ เจ้าพระยามหาโยธา (ทอเรีย)
55+
เป็นปู่ของเจ้าจอมมารดากลิ่นในพระบาทสมเด็จพระจอมเกล้าเจ้าอยู่หัว
56+
และเป็นมรดกตกทอดมาถึง พระเจ้าบรมวงศ์เธอ กรมพระนเรศรวรฤทธิ์
57+
(พระองค์เจ้ากฤดาภินิหาร)
58+
ต่อมาในรัชสมัยพระบาทสมเด็จพระจุลจอมเกล้าเจ้าอยู่หัวโปรดเกล้าฯ
59+
ให้สร้างตำหนัก 2 ชั้น
60+
เป็นที่ประทับของพระเจ้าบรมวงศ์เธอ
61+
กรมพระนเรศวรฤทิธิ์และเจ้าจอมมารดา
62+
ต่อมาเรียกอาคารหลักนี้ว่า ตำหนักเดิม
63+
'''
64+
65+
summarize(text, n=1)
66+
# output: ['บุตรเจ้าพระยามหาโยธานราธิบดีศรีพิชัยณรงค์']
67+
68+
summarize(text, n=3)
69+
# output: ['บุตรเจ้าพระยามหาโยธานราธิบดีศรีพิชัยณรงค์',
70+
# 'เดิมเป็นบ้านของเจ้าพระยามหาโยธา',
71+
# 'เจ้าพระยามหาโยธา']
72+
"""
73+
sents = []
74+
75+
if engine == DEFAULT_SUMMARIZE_ENGINE:
76+
sents = FrequencySummarizer().summarize(text, n, tokenizer)
77+
else: # if engine not found, return first n sentences
78+
sents = sent_tokenize(text, engine="whitespace+newline")[:n]
79+
80+
return sents

pythainlp/tokenize/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
DEFAULT_SYLLABLE_DICT_TRIE = Trie(thai_syllables())
2525
DEFAULT_DICT_TRIE = DEFAULT_WORD_DICT_TRIE
2626

27-
from pythainlp.tokenize.tokenize import (
27+
from pythainlp.tokenize.core import (
2828
Tokenizer,
2929
sent_tokenize,
3030
subword_tokenize,

0 commit comments

Comments
 (0)