Commit ·
f2b6412
1
Parent(s): 78d9025
Upload 9 files
Browse files- app.py +68 -0
- construction_prediction/__pycache__/constants.cpython-310.pyc +0 -0
- construction_prediction/__pycache__/construction_calculator.cpython-310.pyc +0 -0
- construction_prediction/constants.py +16 -0
- construction_prediction/construction_calculator.py +41 -0
- models/fontanka_word2vec.bin +3 -0
- models/librusec_word2vec.bin +3 -0
- models/nplus1_word2vec.bin +3 -0
- models/stihi_ru_word2vec.bin +3 -0
app.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from construction_prediction.constants import load_w2v
|
| 3 |
+
from construction_prediction.construction_calculator import get_collocates_for_word_type
|
| 4 |
+
|
| 5 |
+
st.title('Калькулятор')
|
| 6 |
+
form = st.form('Form')
|
| 7 |
+
target_word = form.text_input(label='Введите целевое слово:',
|
| 8 |
+
placeholder='Введите целевое слово',
|
| 9 |
+
label_visibility='collapsed'
|
| 10 |
+
)
|
| 11 |
+
target_word_pos = form.selectbox(label='Укажите часть речи целевого слова:',
|
| 12 |
+
options=['ADJ', 'ADVB', 'COMP', 'CONJ', 'GRND',
|
| 13 |
+
'INFN', 'INTJ', 'NOUN', 'NPRO', 'NUMR',
|
| 14 |
+
'None', 'PRCL', 'PRED', 'PREP', 'PRTF',
|
| 15 |
+
'PRTS', 'VERB'],
|
| 16 |
+
index=None,
|
| 17 |
+
placeholder='Укажите часть речи целевого слова',
|
| 18 |
+
label_visibility='collapsed'
|
| 19 |
+
)
|
| 20 |
+
current_model = form.selectbox(label='MODEL',
|
| 21 |
+
options=['MODEL 1: nplus', 'MODEL 2: fontanka',
|
| 22 |
+
'MODEL 3: librusec', 'MODEL 4: stihi_ru'],
|
| 23 |
+
index=None,
|
| 24 |
+
placeholder='Выберите модель подбора коллокатов',
|
| 25 |
+
label_visibility='collapsed'
|
| 26 |
+
)
|
| 27 |
+
restrict_vocab = form.text_area(label='Restrict vocab',
|
| 28 |
+
value='',
|
| 29 |
+
placeholder='Restrict vocab',
|
| 30 |
+
label_visibility='collapsed'
|
| 31 |
+
)
|
| 32 |
+
collocate_number = form.number_input(label='Количество коллокатов в выдаче:',
|
| 33 |
+
min_value=1,
|
| 34 |
+
step=1,
|
| 35 |
+
value=10,
|
| 36 |
+
format='%i',
|
| 37 |
+
placeholder='Количество коллокатов в выдаче',
|
| 38 |
+
# label_visibility='collapsed'
|
| 39 |
+
)
|
| 40 |
+
form_button = form.form_submit_button('Запустить')
|
| 41 |
+
|
| 42 |
+
if form_button:
|
| 43 |
+
if not target_word:
|
| 44 |
+
st.error('Вы не ввели целевое слово')
|
| 45 |
+
st.stop()
|
| 46 |
+
if not target_word_pos:
|
| 47 |
+
st.error('Вы не указали часть речи целевого слова')
|
| 48 |
+
st.stop()
|
| 49 |
+
if not current_model:
|
| 50 |
+
st.error('Вы не выбрали модель')
|
| 51 |
+
st.stop()
|
| 52 |
+
|
| 53 |
+
if current_model == 'MODEL 1: nplus':
|
| 54 |
+
model = load_w2v('models/nplus1_word2vec.bin')
|
| 55 |
+
elif current_model == 'MODEL 2: fontanka':
|
| 56 |
+
model = load_w2v('models/fontanka_word2vec.bin')
|
| 57 |
+
elif current_model == 'MODEL 3: librusec':
|
| 58 |
+
model = load_w2v('models/librusec_word2vec.bin')
|
| 59 |
+
else:
|
| 60 |
+
model = load_w2v('models/stihi_ru_word2vec.bin')
|
| 61 |
+
|
| 62 |
+
output = get_collocates_for_word_type(model=model,
|
| 63 |
+
word=target_word,
|
| 64 |
+
target_pos=target_word_pos,
|
| 65 |
+
topn=collocate_number,
|
| 66 |
+
restrict_vocab=restrict_vocab.split()
|
| 67 |
+
)
|
| 68 |
+
st.write(output)
|
construction_prediction/__pycache__/constants.cpython-310.pyc
ADDED
|
Binary file (817 Bytes). View file
|
|
|
construction_prediction/__pycache__/construction_calculator.cpython-310.pyc
ADDED
|
Binary file (1.32 kB). View file
|
|
|
construction_prediction/constants.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from pymorphy2 import MorphAnalyzer
|
| 3 |
+
from gensim.models import KeyedVectors
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
@st.cache_resource(show_spinner="Загружаю модель поиска коллокатов")
|
| 7 |
+
def load_w2v(model_path):
|
| 8 |
+
_w2v_model = KeyedVectors.load_word2vec_format(model_path, binary=True)
|
| 9 |
+
return _w2v_model
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
@st.cache_resource(show_spinner="Загружаю морфологический анализатор")
|
| 13 |
+
def load_morph():
|
| 14 |
+
_morph = MorphAnalyzer(lang='ru')
|
| 15 |
+
return _morph
|
| 16 |
+
|
construction_prediction/construction_calculator.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from construction_prediction.constants import load_morph
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
morph = load_morph()
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def filter_results(model, word, target_pos, collocate_pos, topn, restrict_vocab):
|
| 8 |
+
collocates = []
|
| 9 |
+
target_word = '_'.join((word, target_pos))
|
| 10 |
+
for coll, similarity in model.similar_by_word(target_word, topn=topn, restrict_vocab=restrict_vocab):
|
| 11 |
+
try:
|
| 12 |
+
coll_word, pos = coll.split('_')
|
| 13 |
+
if pos == collocate_pos:
|
| 14 |
+
collocates.append((coll_word, similarity))
|
| 15 |
+
if len(collocates) == topn:
|
| 16 |
+
break
|
| 17 |
+
except ValueError:
|
| 18 |
+
continue
|
| 19 |
+
return collocates
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def get_collocates_for_word_type(model, word, target_pos, topn, restrict_vocab):
|
| 23 |
+
collocate_pos = 'NOUN' if target_pos == 'ADJ' else 'ADJ'
|
| 24 |
+
|
| 25 |
+
collocates = filter_results(model, word, target_pos, collocate_pos, topn * 100, restrict_vocab)
|
| 26 |
+
output = ''
|
| 27 |
+
for collocate_with_score in collocates[:topn]:
|
| 28 |
+
collocate = collocate_with_score[0]
|
| 29 |
+
similarity_score = round(collocate_with_score[1], 3)
|
| 30 |
+
noun = word if target_pos == 'NOUN' else collocate
|
| 31 |
+
adj = word if target_pos == 'ADJ' else collocate
|
| 32 |
+
try:
|
| 33 |
+
# Чтобы была конструкция, в которой один элемент склоняется
|
| 34 |
+
adj = morph.parse(adj)[0].inflect({morph.parse(noun)[0].tag.gender}).word
|
| 35 |
+
# Чтобы исключить результаты типа 'человечный человек'
|
| 36 |
+
if not adj[:3] == noun[:3]:
|
| 37 |
+
# if noun == 'день' and not 'днев' in adj and not 'недел' in adj:
|
| 38 |
+
output += f'\t{adj} {noun}: {similarity_score}\n\n'
|
| 39 |
+
except AttributeError:
|
| 40 |
+
continue
|
| 41 |
+
return output
|
models/fontanka_word2vec.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:df598857b57857eff856a002e7ec76d01e1f77d6bb8702e3418012376530536c
|
| 3 |
+
size 55259519
|
models/librusec_word2vec.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:cd5c7a940159eee9960923ab96b64677527cc71d9b7a83d10fc77c060b6a9efb
|
| 3 |
+
size 32123951
|
models/nplus1_word2vec.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:df63f6f39f0142ab8c21e46e12e2c2f410847e838315171e0690b6d130e2ffa8
|
| 3 |
+
size 5735163
|
models/stihi_ru_word2vec.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ea532b947e9a562aeabcc85157d14b8f660b13412af4b6a50f81128efaca5a4c
|
| 3 |
+
size 19397974
|