解析句子并提取转换值

时间:2013-12-21 18:35:51

标签: javascript php regex parsing string-parsing

我有一组句子给出转换率,例如

  • 10,000件用于∫1
  • ∫1表示10k SMTH
  • 1200∫0.1
  • 的东西
  • 销售3000 Smth 3次

所有这些句子都显示了虚构单位∫(INTEGRAL)的虚构货币比率(SMTH)。我需要一些方法来提取这两个单元之间的转换比率。困难在于数字的格式可以是不同的方式(10,000或10000或10k),单位可以不同的方式编写(某些东西,SMTH和不同的大小写),单位的顺序是不同的(" x THTH代表∫x&#34 ;或者"∫x代表x SMTH"),有时单位写成∫x或x∫。

TL; DR :以某种方式将上述字符串格式化为数学关系,但要注意许多不同的格式。

我知道这有很多问题,而且非常复杂。如果已经有类似的问题,我很乐意看看。

你问什么语言?最好是PHP或JS,但伪代码是一个好的开始

修改

var val = get sentence,
    integral,
    something;
val = val.replace(",", "").replace("k ", "000 ").replace("m ", "000000 ").replace("million ", "000000 ").replace(" million ", "000000 ").replace(" something", "SMTH").replace(" smth", "SMTH");
words = val.split(" ");
for (var i = 0; i < words.length; i++) {
  if (words[i].indexOf("$")!==-1) {
    integral = words[i].replace("∫" , "");
  } else if (words[i].indexOf("SMTH")!==-1) {
    something = words[i].replace("SMTH" , "");
  }
}

简化的javascript / Pseudo-code

2 个答案:

答案 0 :(得分:1)

您使用“for”分隔转换的所有示例。所以没有那么多组合。您可以做的是有一个标识每种货币的单词列表,一个匹配数字的正则表达式,然后您将左侧和右侧用“for”分隔。 要处理每个短语,您将执行以下伪代码:

for each word:
    if it's a known currency identifier
        Store what is the currency
    else if it's a number
        Store the value
    else if it's the "for" word
        Change side
    end if
end for

完成此循环后,您将拥有一个数据结构,其中包含您所拥有的货币数量以及金额。

答案 1 :(得分:1)

我试图在这些方面实施一些东西。正如其他人所提到的,[currency] for [currency]中有一个明显的模式可以轻松匹配。看看下面的内容,它的记录非常清楚。

/**
 * Parse an amount with currency "[symbol (optional)][amount][postfix (optional)] [currency (optional)]"
 * @param  {String} str Currency string e.g. "$100k dollars", "$100million", "100billion euro"
 * @return {Array}      See below
 */
function parseCurrency(str) {
    var match = /([^0-9\.]+)?([0-9\.]+)(\w+)?(?:\s+(\w+))?/.exec(str);

    if(!match) throw new Error("Bad currency input: " + str);

    var symbol = match[1], // €, $, £
        amount = match[2], // 100, 200
        factor = match[3], // k, million i.e. 100k, 100million
        unit = match[4] // euro, pound

    return [symbol, amount, factor, unit];
}

/**
 * Takes in a rate in the form of "[currency] for [currency]"
 * @param  {String} str "[currency] for [currency]"
 * @return {Float}     Rate float
 */
function parseRate(str) {
    // Split and parse the currencies
    var currencies = str.split("for").map(function(amount) {
        return parseCurrency(amount.trim());
    });

    // Calculate the rate
    // put the "for [currency]" over the "[currency] for"
    var base = expandPostfix(currencies[0][1], currencies[0][2]),
        exchangeTo = expandPostfix(currencies[1][1], currencies[1][2]);

    return base / exchangeTo;
}

/**
 * Expand a number postfix
 * @param  {Number} num     
 * @param  {String} postfix Postfix such as "k", "m", "billion"
 * @return {Number}         Expanded number
 */
function expandPostfix(num, postfix) {
    return num * (({
        k : 1000,
        m: 1000000,
        million: 1000000
    })[postfix] || 1);
}

parseRate("1 euro for 3 pound"); // 0.333
parseRate("10000 something for ∫1"); // 10000
parseRate("1200 Something for ∫0.1"); // 12000