Welcome to VerbCalc’s documentation!

1. Introduction

VerbCalc is a Python library that allows to perform calculations based on human speech.

2. Installation

$ pip install verbcalc

3. Getting started

VerbCalc library is very simple to use! All you have to do in order to use it, is to import and invoke calculate function.

import verbcalc
x = verbcalc.calculate('2 plus 2')
print(x) # 4

4. API Documentation

4.1. verbcalc.core.translator.translator module

Translator module allows to change natural language to mathematical notation.

verbcalc.core.translator.translator.translate(sentence: str, symbols: ~verbcalc.core.translator.symbols.Symbols = <verbcalc.core.translator.symbols.Symbols object>) str

Translates maths related words into their symbols.

Parameters
  • sentence – Sentence to be converted.

  • symbols – Instance of symbols to match the words from.

Returns

Converted sentence.

4.2. verbcalc.core.translator.symbols module

Handles symbol storage.

class verbcalc.core.translator.symbols.Symbols(path_to_phrasings: str = '../../data/phrasings.json')

Bases: object

Hint

You can edit lists like additions, subtractions etc. to make a library’s translation to other language (human language, not programming language) or implement any other custom behaviour you desire.

Stores lists with words corresponding to given symbols.

Parameters

path_to_phrasings – Stores path to .json file that has phrasing values.

symbol_dictionary

Stores all the symbol keys and their corresponding values.

4.3. verbcalc.core.dispatcher module

Dispatches single step operations in correct order.

class verbcalc.core.dispatcher.Dispatcher

Bases: object

Dispatches single step operations according to given rules.

arithmetic_operations

Dictionary containing string representation of arithmetic operators along with their callables.

functions

Dictionary containing known functions along with their callables.

dispatch(tokens: list) float

Dispatches all the operations.

Parameters

tokens – List containing all the tokens.

Raises
  • ZeroDivisionError – Calculation requires dividing by zero, which makes small puppies die.

  • InvalidExpressionException – Raised when expression contains stuff like [‘2’, ‘4’] or [‘-’, ‘/’]

Returns

Result of calculation.

exception verbcalc.core.dispatcher.InvalidExpressionException

Bases: Exception

Raised when expression is invalid (for example contains 2 arithmetic operators in a row).

4.4. verbcalc.core.calculate module

Allows making calculations.

verbcalc.core.calculate.calculate(sentence: str, dispatcher: ~verbcalc.core.dispatcher.Dispatcher = <verbcalc.core.dispatcher.Dispatcher object>, path_to_answers: str = '../data/answers.json', silent: bool = False) str

Calculates result from sentence.

Parameters
  • sentence – Sentence to take calculation from.

  • dispatcher – Dispatcher object to use, if none provided it will use default one.

  • path_to_answers – Contains path to the answers .json file.

  • silent – Returns only the answer instead of an entire answer sentence

Returns

Calculated sentence.

4.5. verbcalc.core.answers module

5. License

BSD 3-Clause License

Copyright (c) 2021, Eryk Piasecki All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

  3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

6. Attributions

Thanks to Kinga Stefaniak for the project logo.

7. Security

7.1. Supported versions

Soon!

7.2. Reporting a Vulnerability

If you have found vulnerability in the code, let me know at: da3aab79-4378-4321-a726-67c4bb1f17ea@aleeas.com

8. Support

If you got stuck, or got any question regarding the project, feel free to email me at da3aab79-4378-4321-a726-67c4bb1f17ea@aleeas.com