如何最好地解析成分表中的每个项目,并根据每个解析结果创建一个新对象? [英] How does one parse best each item of an ingredient list and does create a new object based on each parsing result?

查看:49
本文介绍了如何最好地解析成分表中的每个项目,并根据每个解析结果创建一个新对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个配料清单,我想做一个正则表达式来查找 1杯或1个 tsp 或1大汤匙,等等.

I have this list of ingredients I am trying to make a regex to look for 1 cup , or 1 tsp or 1 tablespoon and so on.....

我制作了此正则表达式,但效果不佳.我正在尝试从测量中分离出成分.

I have made this regex but It doesn't work as well. I am trying separate ingredients from the measurements.

因此,使用此字符串 1切碎的番茄,应将 1 作为数量取出并输出:

So with this string 1 Chopped Tomato it should take out the 1 as amount and output this:

const output = [
  {
    val: "Chopped Tomato",
    amount: "1",
  },

在下面的字符串下,它应该能够从 1/2 tsp精盐中取出 1/2 tsp 并输出:

And with this string below it should be able to take out ½ tsp from ½ tsp fine salt and output this:

const output = [
  {
    val: "fine sea salt",
    amount: "½ tsp",
  },

这些是我用于测量的值:

These are the values I am using for the measurements:

    const measures = [
      "tbsp","tablespoon","tsp","teaspoon","oz","ounce","fl. oz","fluid ounce","cup","qt",
      "quart","pt","pint","gal","gallon","mL","ml","milliliter","g","grams","kg","kilogram","l","liter",
];

这是我构建的输入和正则表达式

This is the input and regex I built

const Ingris = [
  "1 teaspoon heavy cream",
  "1 Chopped Tomato",
  "1/2 Cup yogurt",
  "1 packet pasta ",
  "2 ounces paprika",
]


const FilterFunction = (term) => {
  let data = []
  if (term) {
    const newData = Ingris.filter(({
      ingridients
    }) => {
      if (RegExp(term, "gim").exec(ingridients))
        return ingridients.filter(({
            val
          }) =>
          RegExp(term, "gim").exec(val)
        ).length;
    })
    data.push(newData)
  } else {
    data = []
  }
};
console.log(FilterFunction("cup"))

所需的输出:

const output = [
  {
    val: "Tomato",
    amount: "1 Chopped ",
  },
  {
    val: "yogurt",
    amount: "1/2 Cup",
  },
  {
    val: "1",
    amount: "packet pasta ",
  },
  {
    val: "fine sea salt",
    amount: "½ tsp",
  },
  {
    val: "heavy cream",
    amount: "1/2 teaspoon",
  },
  {
    val: "paprika",
    amount: "2 ounces",
  },
];

推荐答案

当我添加小包和盎司(复数)时,这是起作用的

Here is something that worked when I added packet and ounces (plural)

可以处理

  • 金额仅为1,2,¼,½,¾和1/2
  • 没有数量的单词,例如碎肉"
  • 诸如流体盎司"之类的复合量度.单数和复数
  • 切碎或切碎的动作词
  • Just amounts like 1, 2, ¼, ½, ¾ and 1/2
  • Just words without amounts like "Ground meat"
  • Compound measures like "fluid ounces" in singular and plural
  • Action words like chopped or ground

全部由一个半正则表达式和一个解构任务处理

All handled by one and a half regex and one destructuring assignment

const measures = [
  "tbsp", "tablespoon", "tsp", "teaspoon", "oz", "ounce", "ounces", "cup", "qt", "packet", "quart", "pt", "pint", "gal", "gallon", "mL", "ml", "milliliter", "g", "grams", "kg", "kilogram", "l", "liter", 
  "fl. oz", "fluid ounce", "fluid ounces" ]; // plural after singular!
const action = ["chopped","ground"]  

const compound = measures.filter(measure => measure.split(" ").length > 1); // extract compound words

const amountRe =     /^(\d+\/\d+|¼|½|¾|\d|\d+)/; // amounts like 1, 1/2 etc
const amountValueRe = /(\d+\/\d+|¼|½|¾|\d|\d+) ([\w.]+) (.*)/; // first part must be the same as amountRe

const makeList = list => list.map(line => {
  if (!amountRe.test(line)) return { value: line }; // no amounts found

  // test for compound measures
  compound.forEach(cmp => line = line.replace(cmp, cmp.split(" ").join("_"))); // add underscores if found
  
  // destruct the match on amount plus value or amount of amount plus value
  let [, num, measure, what] = line.match(amountValueRe);
  
  if (action.includes(measure.toLowerCase())) { // test for chopped
    what = `${measure} ${what}`; // or add an action item to the object
    measure = "";
  }
  
  const obj = {}
  if (num) obj.amount = num;
  if (measure) obj.measure = measure.split("_").join(" ").trim(); // remove added underscores
  if (what) obj.value = what;
  return obj;
});

const Ingris = [
  "Chicken breast",
  "Ground ginger",
  "1 teaspoon heavy cream",
  "2 fluid ounces lemon juice",
  "1 Chopped Tomato",
  "1/2 Cup yogurt",
  "2 fl. oz paprika",
  "1 fluid ounce water",
  "½ packet pasta ",
  "2 ounces paprika"
];

console.log(makeList(Ingris))

这篇关于如何最好地解析成分表中的每个项目,并根据每个解析结果创建一个新对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆