php做单词和图片同时转换,将单词转换为PHP中的数字

Well, overall you are doing two things: Finding tokens (words that translates to numbers) and applying grammar. In short, you are building a parser for a very limited language.

The tokens you would need are:

POWER: thousand, million, billion

HUNDRED: hundred

TEN: twenty, thirty… ninety

UNIT: one, two, three, … nine,

SPECIAL: ten, eleven, twelve, … nineteen

(drop any “and”s as they are meaningless. Break hyphens into two tokens. That is sixty-five should be processed as “sixty” “five”)

Once you’ve tokenized your string, move from RIGHT TO LEFT.

Grab all the tokens from the RIGHT until you hit a POWER or the whole string.

Parse the tokens after the stop point for these patterns:

SPECIAL

TEN

UNIT

TEN UNIT

UNIT HUNDRED

UNIT HUNDRED SPECIAL

UNIT HUNDRED TEN

UNIT HUNDRED UNIT

UNIT HUNDRED TEN UNIT

(This assumes that “seventeen hundred” is not allowed in this grammar)

This gives you the last three digits of your number.

If you stopped at the whole string you are done.

If you stopped at a power, start again at step 1 until you reach a higher POWER or the whole string.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值