Skip to content

Commit

Permalink
wikionary to ruslingua
Browse files Browse the repository at this point in the history
  • Loading branch information
DedInc committed Jan 10, 2024
1 parent bf43e39 commit 5f1785d
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 126 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2021 Vladislav Zenkevich
Copyright (c) 2024 Vladislav Zenkevich

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
74 changes: 62 additions & 12 deletions README.MD
Original file line number Diff line number Diff line change
@@ -1,18 +1,68 @@
<h1 align="center">wikionary - Модуль для поиска Синонимов, Антонимов и т.д.</h1>
# RusLingua 📚

<br>
RusLingua is a Python library for retrieving various linguistic information about Russian words. It provides a simple API to get synonyms, antonyms, word associations, cognate words, and definitions.

<h1 align="center"> -Как это работает?- </h1>
## Features

- Get synonyms of a word 👥
- Get antonyms of a word 👎
- Get word associations 💭
- Get cognate words (words with a common root) 🌳
- Get definitions from dictionaries 📖

## Quickstart

```python
from ruslingua import RusLingua

ruslingua = RusLingua()

synonyms = ruslingua.get_synonyms('дом')
antonyms = ruslingua.get_antonyms('дом')
associations = ruslingua.get_associations('дом')
cognates = ruslingua.get_cognate_words('дом')
definition = ruslingua.get_definition('дом')

print(synonyms)
print(antonyms)
print(associations)
print(cognates)
print(definition)
```

## Installation

```
pip install ruslingua
```

## Usage

Import the RusLingua class and instantiate it:

```python
import wikionary as w
from ruslingua import RusLingua

ruslingua = RusLingua()
```

Then call the methods with a word to get the linguistic information:

```python
synonyms = ruslingua.get_synonyms('дом')
antonyms = ruslingua.get_antonyms('дом')
associations = ruslingua.get_associations('дом')
cognates = ruslingua.get_cognate_words('дом')
definition = ruslingua.get_definition('дом')
```

The methods return lists of strings.

## Credits

print(w.getSynonims('привет')) #Найти синонимы к слову привет
print(w.getAntonyms('привет')) #Найти антонимы к слову привет
print(w.getPhraseologs('привет')) #Найти фразеологизмы к слову привет
print(w.getHyperonims('привет')) #Найти гиперонимы к слову привет
print(w.getAssociations('привет')) #Найти ассоциации к слову привет
print(w.getRandomWord()) #Найти случайное слово
RusLingua retrieves data from various sources:

print(w.inflectWord('привет')) #Сделать разбор по падежам
```
- [jeck.ru](https://jeck.ru) - synonyms 👥
- [razbiraem-slovo.ru](https://razbiraem-slovo.ru) - antonyms 👎 and cognate words 🌳
- [wordassociations.net](https://wordassociations.net) - word associations 💭
- [gramota.ru](https://gramota.ru) - definitions 📖
1 change: 1 addition & 0 deletions ruslingua/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .ruslingua import RusLingua
59 changes: 59 additions & 0 deletions ruslingua/ruslingua.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import requests
from lxml import html
from urllib.parse import unquote

class RusLingua:
def __init__(self):
self.user_agent = ('Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 '
'(KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36')
self.headers = {'User-Agent': self.user_agent}

def get_html_tree(self, url):
response = requests.get(url, headers=self.headers)
return html.fromstring(response.content)

def get_synonyms(self, word):
synonyms = []
tree = self.get_html_tree(
f'https://jeck.ru/tools/SynonymsDictionary/{word}')

for url in tree.xpath('//td/a/@href'):
if '+' not in url:
parts = url.split('Dictionary/')
if len(parts) > 1:
synonyms.append(unquote(parts[1]))

return synonyms

def get_antonyms(self, word):
tree = self.get_html_tree(f'https://razbiraem-slovo.ru/antonyms/{word}')
elements = tree.xpath("//*[contains(@href, 'antonyms')][count(@class)=0]")

antonyms = [element.text.strip() for element in elements if element.text.strip()]
return antonyms

def get_cognate_words(self, word):
tree = self.get_html_tree(f'https://razbiraem-slovo.ru/odnokorennye/{word}')
elements = tree.xpath("//*[contains(@href, 'po-sostavu')][count(@class)=1]")

cognates = [element.attrib['href'].split('/')[-1] for element in elements if 'разбор' in element.text]
return cognates

def get_associations(self, word):
associations = []
tree = self.get_html_tree(
f'https://wordassociations.net/ru/ассоциации-к-слову/{word}')

for url in tree.xpath('//li/a/@href'):
if 'D1%83/' in url:
associations.append(unquote(url.split('D1%83/')[1]).lower())

return associations

def get_definition(self, word):
tree = self.get_html_tree(
f'https://gramota.ru/poisk?query={word}&mode=slovari&l=1&dicts[]=42')

full_text = ''.join(tree.xpath("//div[contains(@class, 'description')]//text()"))

return full_text.strip()
12 changes: 6 additions & 6 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@
long_description = f.read()

setup(
name="wikionary",
version="1.2.1",
name="ruslingua",
version="1.0.0",
author="Maehdakvan",
author_email="visitanimation@google.com",
description="Модуль для поиска Синонимов, Антонимов и т.д.",
description="",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/DedInc/wikionary",
url="https://github.com/DedInc/ruslingua",
project_urls={
"Bug Tracker": "https://github.com/DedInc/wikionary/issues",
"Bug Tracker": "https://github.com/DedInc/ruslingua/issues",
},
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
packages=find_packages(),
install_requires=['requests', 'lxml', 'pymorphy2'],
install_requires=['requests', 'lxml'],
python_requires=">=3.6"
)
1 change: 0 additions & 1 deletion wikionary/__init__.py

This file was deleted.

106 changes: 0 additions & 106 deletions wikionary/wikionary.py

This file was deleted.

0 comments on commit 5f1785d

Please sign in to comment.