示例#1
0
def logarithm(words, wordsminus):
    """
        求对数
    :param words: 
    :param wordsminus: 
    :return: 
    """
    try:
        if "LG" in words or "LOG" in words:
            Lg = wordsminus.replace("LOG", "").replace("LG", "").replace(
                " ", "").replace("的", "")
            Lg = float(extract_number(Lg)[0])
            if Lg <= 0.0:
                return 'illegal math'
            lgbb = math.log(Lg)
            results = str(lgbb)
        elif "对数" in words:
            Logg = wordsminus.replace("以", "").replace("对数", "").replace(
                "的对数", "").replace(" ", "").replace("的", "")
            root = Logg.split("为底")
            rootaa = float(extract_number(root[0])[0])
            rootbb = float(extract_number(root[1])[0])
            if rootaa <= 0.0 or rootbb <= 0.0:
                return 'illegal math'
            rootcc = math.log(rootbb) / math.log(rootaa)
            results = str(rootcc)
        else:
            results = words
        return results
    except Exception as e:
        logger.info(str(e))
        return words
示例#2
0
def reagan(words, wordsminus):
    """
        求平方根,立方根,n次方根
    :param words: str, 原句
    :param wordsminus:str , 处理后的句子
    :return: 
    """
    try:
        if '根号' in words:
            reagan = wordsminus.replace("开", "").replace("根号",
                                                         "").replace("的", "")
            radicalaa = float(extract_number(reagan)[0])
            if radicalaa < 0.0:
                return 'illegal math'
            radicalbb = math.sqrt(radicalaa)
            results = str(radicalbb)
        elif "平方根" in words:
            reagan = wordsminus.replace("开", "").replace("平方根", "").replace(
                "平方", "").replace("的", "")
            reagan = extract_number(reagan)[0]
            squarerootaa = float(reagan)
            if squarerootaa < 0.0:
                return 'illegal math'
            squarerootbb = math.sqrt(squarerootaa)
            results = str(squarerootbb)
        elif "立方根" in words:
            reagan = wordsminus.replace("开", "").replace("立方根", "").replace(
                "立方", "").replace("的", "")
            reagan = extract_number(reagan)[0]
            squarerootaa = float(reagan)
            squarerootbb = math.pow(squarerootaa, 1.0 / 3)
            results = str(squarerootbb)
        elif "次方根" in words:
            reagan = wordsminus.replace("开", "").replace("次方根",
                                                         "").replace("次方", "")
            squareroot = reagan.split("的")
            squarerootaa = float(extract_number(squareroot[0])[0])
            squarerootbb = float(extract_number(squareroot[1])[0])
            if squarerootaa % 2 == 0 and squarerootbb < 0.0:
                return 'illegal math'
            squarerootcc = math.pow(squarerootaa, 1.0 / squarerootbb)
            results = str(squarerootcc)
        else:
            results = words
        return results
    except Exception as e:
        logger.info(str(e))
        return words
示例#3
0
def StringToCalculateOne(words):
    """
        简单句总调用
        求乘方,阶乘,指数,根式,三角函数,对数,最大最小公约数公倍数    
    :param words: str
    :return: str
    """
    try:
        res_reagan = reagan(words, words)  # 报错或不执行返回原来的数据
        res_power = power(words, words)
        # aa22 = triangle(complex[i], complex[i])
        res_logarithm = logarithm(words, words)
        rees_factorial = factorial(words, words)
        res_fraction = fraction(words, words)
        if (res_reagan != words):
            goal = res_reagan
        elif (res_power != words):
            goal = res_power
        # elif (aa22 != complex[i]):
        #     goal = aa22
        elif (res_logarithm != words):
            goal = res_logarithm
        elif (rees_factorial != words):
            goal = rees_factorial
        elif (res_fraction != words):
            goal = res_fraction
        else:
            oldwords = words.replace("的", "")
            oldwords = extract_number(oldwords)[0]
            goal = oldwords
        return goal
    except Exception as e:
        logger.info(str(e))
        return words
示例#4
0
def fractiontwo(words, wordsminus):
    """
        取分数
    :param words: 
    :param wordsminus: 
    :return: 
    """
    try:
        if "fenzhi" in words:
            fenzhi = wordsminus.replace("fenzhi",
                                        "/").replace(" ", "").replace("的", "")
            root = fenzhi.split("/")
            rootaa = float(extract_number(root[0])[0])
            rootbb = float(extract_number(root[1])[0])
            results = str(rootaa / rootbb)
        else:
            results = words
        return results
    except Exception as e:
        logger.info(str(e))
        return words
示例#5
0
def power(words, wordsminus):
    """
        求指数,求平方
    :param words: 
    :param wordsminus: 
    :return: 
    """
    try:
        if "平方根" not in words and "平方" in words:
            reagan = wordsminus.replace("平方", "").replace("开",
                                                          "").replace("的", "")
            reagan = extract_number(reagan)[0]
            square = float(reagan)
            radicalbb = math.pow(square, 2)
            results = str(radicalbb)
        elif "立方根" not in words and "立方" in words:
            reagan = wordsminus.replace("立方", "").replace("开",
                                                          "").replace("的", "")
            reagan = extract_number(reagan)[0]
            square = float(reagan)
            radicalbb = math.pow(square, 3)
            results = str(radicalbb)
        elif (("次方" in words or "次幂" in words) and "次方根" not in words
              and "次幂根" not in words):
            reagan = wordsminus.replace("次方",
                                        "").replace("开", "").replace("次幂", "")
            squareroot = reagan.split("的")
            squarerootaa = float(extract_number(squareroot[0])[0])
            squarerootbb = float(extract_number(squareroot[1])[0])
            squarerootcc = math.pow(squarerootaa, squarerootbb)
            results = str(squarerootcc)
        else:
            results = words
        return results
    except Exception as e:
        logger.info(str(e))
        return words
示例#6
0
def factorial(words, wordsminus):
    """
        求阶乘
    :param words: 
    :param wordsminus: 
    :return: 
    """
    results = words
    try:
        if "jiecheng的" in words:
            factory = wordsminus.replace("jiecheng的",
                                         "").replace("的", "").replace(" ", "")
            fact = float(extract_number(factory)[0])
            if fact <= 10000:
                results = str(math.factorial(fact))
            else:
                results = words
        return results
    except Exception as e:
        logger.info(str(e))
        return words
示例#7
0
def StringToCalculateTwo(sentence=''):
    """
        复杂算式, 总调用, 分步计算,先计算三角函数,指数,对数
        1.取出操作符与数据(注意--,++,-,+开头这种)
        2.计算中间的,比如说根号12,2的7次方这种
    :param sentence: 
    :return: 
    """
    try:
        if sentence[0] == '+' or sentence[0] == '-':
            sentence = '0' + sentence
        minus = 0
        operators = []
        complex = re.split("[+*/-]", sentence)
        for s in sentence:
            if ((s == '+' or s == '-' or s == '*' or s == '/') & minus !=
                    0 & minus != 2):
                operators.append("" + s)
                minus = minus + 1
            else:
                minus = 1
        # complex.append(float(formula[prePos:].strip()))
        formula = ""
        for i in range(len(complex)):
            if "" == complex[i]:
                complex[i] = " "
                formula = formula + complex[i] + operators[i]
                continue
            res_reagan = reagan(complex[i], complex[i])  #报错或不执行返回原来的数据
            res_power = power(complex[i], complex[i])
            # aa22 = triangle(complex[i], complex[i])
            res_logarithm = logarithm(complex[i], complex[i])
            res_factorial = factorial(complex[i], complex[i])
            res_fraction = fraction(complex[i], complex[i])

            if (res_reagan != complex[i]):
                goal = res_reagan
            elif (res_power != complex[i]):
                goal = res_power
            # elif (aa22 != complex[i]):
            #     goal = aa22
            elif (res_logarithm != complex[i]):
                goal = res_logarithm
            elif (res_factorial != complex[i]):
                goal = res_factorial
            elif (res_fraction != complex[i]):
                goal = res_fraction
            elif "(" in complex[i] or ")" in complex[i]:
                goal = sph.numberTranslator(target=complex[i].replace("的", ""))
            else:
                oldwords = complex[i].replace("的", "")
                oldwords = extract_number(oldwords)[0]
                goal = oldwords
            if goal == 'illegal math':  #非法抛出
                return 'illegal math'
            if (i < len(complex) - 1):
                rest = goal + operators[i]
            else:
                rest = goal
            formula = formula + rest
        myformula = formula.replace("*-", "*(-1)*").replace("*+", "*").replace(
            "/-", "/(-1)/")
        formulalast = myformula.replace(" ", "").replace("+-", "-").replace(
            "++", "+").replace("-+", "-").replace("--", "+")
    except Exception as e:
        logger.info(str(e))
        return sentence

    return formulalast