【问题标题】:Parse sentences and extract conversion values [closed]解析句子并提取转换值
【发布时间】:2013-12-21 18:35:50
【问题描述】:

我有一组给出转化率的句子,比如

  • 10,000 东西 ∫1
  • ∫1 代表 10k SMTH
  • 1200 ∫0.1 的东西
  • 3000 秒卖 3∫

所有这些句子都显示了虚构货币某物 (SMTH) 与虚构单位 ∫ (INTEGRAL) 的比率。我需要一些方法来提取这两个单位之间的转换率。困难在于数字可以采用不同的格式(10,000 或 10000 或 10k),单位可以不同的写法(something, SMTH 和不同的大小写),单位的顺序不同(“x SMTH for ∫x”或“∫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/伪代码

【问题讨论】:

  • 它确实是一种具有单词结构的句子。为了钱的东西。钱买东西。翻译这个似乎有很多障碍。
  • 有,我尝试用一​​个关键字替换每次提及一种货币以统一提及货币,然后尝试将不同的数字格式化为“真实”数字。这使它更简洁一些,但您仍然需要整理所有填充词,例如“for”或“selling”:(

标签: javascript php regex parsing string-parsing


【解决方案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

完成此循环后,您将获得一个数据结构,其中包含每边的货币和金额。

【讨论】:

  • 这很有帮助,我没有完全使用你所做的,但我稍微改变了你的方法并编辑了我的问题。谢谢!
【解决方案2】:

我尝试按照这些思路实现一些东西。正如其他人所提到的,[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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多