基于包含多个可能子串的变量的计算

Calculation based on variable containing multiple possible substrings

我有 2 个变量(源和平台)的结果如下:

“来源”: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 Edg/100.0.1185.44 等等

“平台”: Win32 等等

我想创建 2 个新变量(设备和系统),从上面的变量中“提取”出来。

我找到了一些代码,但我不知道如何用 spss 语法实现它(即使可能)。


const getDeviceType = () => {
  const ua = navigator.userAgent;
  if (/(tablet|ipad|playbook|silk)|(android(?!.*mobi))/i.test(ua)) {
    return "tablet";
  }
  if (
    /Mobile|iP(hone|od)|Android|BlackBerry|IEMobile|Kindle|Silk-Accelerated|(hpw|web)OS|Opera M(obi|ini)/.test(
      ua
    )
  ) {
    return "mobile";
 }
  return "desktop";
};

function getOS() {
  var userAgent = window.navigator.userAgent,
      platform = window.navigator.platform,
      macosPlatforms = ['Macintosh', 'MacIntel', 'MacPPC', 'Mac68K'],
      windowsPlatforms = ['Win32', 'Win64', 'Windows', 'WinCE'],
      iosPlatforms = ['iPhone', 'iPad', 'iPod'],
      os = null;

  if (macosPlatforms.indexOf(platform) !== -1) {
    os = 'Mac OS';
  } else if (iosPlatforms.indexOf(platform) !== -1) {
    os = 'iOS';
  } else if (windowsPlatforms.indexOf(platform) !== -1) {
    os = 'Windows';
  } else if (/Android/.test(userAgent)) {
    os = 'Android';
  } else if (!os && /Linux/.test(platform)) {
    os = 'Linux';
  }

  return os;
}

我也试过这个,但是每个变量都变成“零”:

IF PLATFORM contains the word 'Macintosh' or  'MacIntel' or 'MacPPC' or 'Mac68K'
Then SYSTEM is “Mac OS”
Else if PLATFORM contains the word 'iPhone' or 'iPad' or 'iPod' 
Then SYSTEM is “iOS”
Else if PLATFORM contains the word 'Win32' or 'Win64' or 'Windows' or 'WinCE'
Then SYSTEM is “Windows”
Else if PLATFORM contains the word 'Android'
Then SYSTEM is “Android”
Else if PLATFORM does not contains ‘os’ but contains the word ‘Linux’ 
Then SYSTEM is “Linux”

IF SOURCE contains words tablet or ipad or playbook or silk or (android but not mobi)
Then DEVICE = Tablet
Else If source contains Mobile|iP(hone|od)|Android|BlackBerry|IEMobile|Kindle|Silk-Accelerated|(hpw|web)OS|Opera M(obi|ini)
Then DEVICE = mobile
Else DEVICE = desktop

COMPUTE test = CHAR.INDEX(platform,"windows").
EXECUTE.
string c (A255).
IF test>0 c="windows".
EXECUTE.

COMPUTE wind = CHAR.INDEX(source,"windows").
EXECUTE.
COMPUTE wind32 = CHAR.INDEX(source,"Win32").
EXECUTE.
STRING c2 (A255).
IF wind>0 or wind32 > 0 c="Windows".
EXECUTE.

谁能帮我解决这个问题?谢谢!

我建议采用不同的策略,而不是在语法本身中包含所有值: 您在源列中创建一个 table 可能的子字符串,并在目标列中创建相应的值 - 并将其保存在一个单独的文件中,您可以在其中进行更正和添加。例如:

sourceVal | targetVal 
----------|----------
  windows | Windows  
  Windows | Windows  
    win32 | Windows  
   win 10 | Windows  
  Android | Android
  android | Android

一旦您将其用作具有以下语法的字典:

(数据集 dict 包含您创建的字典,数据集 orig 是您正在处理的原始数据)

dataset activate dict.
string cmd (a200).
compute cmd=concat("if char.index(source, '", rtrim(sourceVal), "')>0 device='", targetVal, "'.").
write out="c:\yourpath\runthis.sps" /cmd.
exe.

此时您已经创建了一个新的语法文件,该文件对您的词典中标识的每个子字符串都有一个单独的命令。现在你可以 运行 它在原始数据上:

dataset activate orig.
string device (a200).
insert file="c:\yourpath\runthis.sps".
exe.

如果你想用同一个命令在sourceplatform中搜索,最简单的方法是创建一个包含它们的新变量:

string srcPlat (a2000)
compute srcPlat=concat(rtrim(source), rtrim(platform))

现在相应地更新 cmd