Code Monkey home page Code Monkey logo

weightunit's Introduction

介绍

解析字符串中的重量单位,并进行简单的计算,主要在淘宝的商品标题中做了测试

一些使用例子

from weightunit import parse_str, UnitLevel

# 乘法情况, 将升转化为毫升
mu = parse_str("德国进口德亚全脂纯牛奶1L*12盒整箱营养早餐奶学生儿童老人")
print(mu)
print((mu.start_index, mu.end_index))
print(mu.calc_weight())# 计算单位,并转换为ml
print("---------------------")

# 有加法的情况
mu = parse_str("简醇0蔗糖250g*1瓶+开啡尔草莓味200g*5瓶")
print(mu)
print((mu.start_index, mu.end_index))
print(mu.calc_weight())
print("---------------------")

# 或的情况
mu = parse_str("德国进口德亚全脂纯牛奶1L*12盒|24盒整箱营养早餐奶学生儿童老人")
print(mu)
print((mu.start_index, mu.end_index))
print(mu.calc_weight())# 计算单位,并转换为ml
print("---------------------")

# 添加自定义的重量单位
mu = parse_str("手拉葫芦吊机手动2吨倒链1吨10t小型5吨家用起重机3吨工业吊葫芦")
print("未添加【吨】",mu)
ul = UnitLevel.gen_default()
ul.add_unit_word("吨", level=1)
ul.add_unit_word("t", level=1)
mu = parse_str("手拉葫芦吊机手动2吨倒链1吨10t小型5吨家用起重机3吨工业吊葫芦", unit_level=ul)
print("添加【吨】",mu)
print("---------------------")


# 设置str_modifier函数,可以在真正解析前字符串前,删除一些字符,让长串的计算公式连一起
def str_modifier(input_str):
# 替代默认的str修改,不对input_str进行修改
return input_str
mu = parse_str("爱尔培 奶酪500g*6盒11月29到期", )
print(mu)
mu = parse_str("爱尔培 奶酪500g*6盒11月29到期", str_modifier=str_modifier)
print(mu)
print("---------------------")

# 获得所有的段落,不强制合并到一个公式里
mu_list = parse_str("3箱爱尔培 奶酪500g*6盒11月29到期", only_one=False)
print(type(mu_list))
print(mu_list)
mu_list = parse_str("3箱爱尔培 奶酪500g*6盒11月29到期", only_one=True)
print(type(mu_list))
print(mu_list)
print("---------------------")

weightunit's People

Contributors

chongwangcc avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.