dongpenggan6812 2011-10-01 15:11
浏览 36
已采纳

如何检测QWERTY键盘上的另一个角色是否接近?

I'm developing a spam detection system and have been alerted to find that it can't detect strings like this - "asdfsdf".

My solution to this involves detecting if the previous keys were near the other keys on the keyboard. I am not getting the input (to detect spam from) from the keyboard, I'm getting it in the form of a string.

All I want to know is whether a character is one key, two keys or more than two keys away from another character.

For example, on a modern QWERTY keyboard, the characters 'q' and 'w' would be 1 key away. Same would the chars 'q' and 's'. Humans can figure this out logically, how could I do this in code?

  • 写回答

4条回答 默认 最新

  • drygauost253590142 2011-10-01 15:23
    关注

    You could simply create a two-dimensional map for the standard qwerty keyboard. Basically it could look something like this:

    map[0][0] = 'q';
    map[0][1] = 'a';
    map[1][0] = 'w';
    map[1][1] = 's';
    

    and so on.

    When you get two characters, you simply need to find their x, and y in the array 'map' above, and can simply calculate the distance using pythagoras. It would not fill the requirement you had as 'q' and 's' being 1 distance away. But rather it would be sqrt(1^2 + 1^2) approx 1.4

    The formula would be:

    • Characters are c1 and c2
    • Find coordinates for c1 and c2: (x1,y1) and (x2,y2)
    • Calculate the distance using pythagoras: dist = sqrt((x2-x1)^2 + (y2-y1)^2).
    • If necessary, ceil or floor the result.

    For example:

    Say you get the characters c1='q', and c2='w'. Examine the map and find that 'q' has coordinates (x1,y1) = (0, 0) and 'w' has coordinates (x2,y2) = (1, 0). The distance is

    sqrt((1-0)^2 + (0-0)^2) = sqrt(1) = 1
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度