如何识别段落中的数字并将其提取为新表中的列?我有一个文本段落,其中包含不同句子中的几个数字,我想提取这些数字并将它们堆叠在新表的一列中。
这个句子看起来是这样的:
在存储地点用移动秤称重袋子。每袋 5. 进行一次抽样。A 的初始样品:10 公斤,B:20 公斤,C:30 公斤,
40kg和E:50kg在105℃下干燥24小时。样品经过反复破碎,剩下约800-1000克,进一步研磨以通过100目试验筛。将破碎的样品放入V型搅拌机中搅拌20分钟,然后转移到100毫升防水塑料瓶中并密封。
此外,不同原始样本的数量可以从 1 到 20 或 30 不等。
如果有人能用脚本给我指明正确的方向,我将不胜感激。我尝试在 Copilot 的帮助下编写脚本,但没有得到我想要的结果。
非常感谢!
自动化和脚本回复:如何识别段落中的数字并将其提取为新表中的列?以下是实现此目的的一种方法:
names default to here(1);
paragraph="The bags are weighed on a mobile scale at storage location. Each 5. bag is spear sampled.
The primary sample of A: 10 kg, B: 20 kg, C: 30 kg, 40 kg and E: 50 kg was dried for 24 hours at 105 C.
The sample was subject to repeated splitting, leaving some 800-1000 grams which was further milled to
pass a 100-mesh test sieve. The crushed sample is blended in a V-blender for 20 minutes and transferred
to 100 ml waterproof plastic bottle and sealed.";
theNumbers = {};
i = 1;
While( Word( i, paragraph, " ." ) != "",
theWord = Word( i, paragraph, " ." );
If( Is Missing( Num( theWord ) ) == 0,
Insert Into( theNumbers, Num( theWord ) )
);
i++;
);<BR />Show( theNumbers );
我确信有人也会提供 RegEx() 解决方案。
回复:如何识别段落中的数字并将其提取为新表中的列?您可以使用 Words 来实现这一点,但您必须有一个非数字字符列表。然后,您可以使用 Concat Items() 将您获得的列表重新转换为字符串
Names Default To Here(1);
str = "The bags are weighed on a mobile scale at storage location. Each 5. bag is spear sampled. The primary sample of A: 10 kg, B: 20 kg, C: 30 kg, 40 kg and E: 50 kg was dried for 24 hours at 105 C. The sample was subject to repeated splitting, leaving some 800-1000 grams which was further milled to pass a 100-mesh test sieve. The crushed sample is blended in a V-blender for 20 minutes and transferred to 100 ml waterproof plastic bottle and sealed.";
matchchar = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" || Get Punctuation Characters() || Get Whitespace Characters();
nums = Words(str, matchchar);
// {"5", "10", "20", "30", "40", "50", "24", "105", "800", "1000", "100", "20", "100"}
Concat Items(nums, ", "); // "5, 10, 20, 30, 40, 50, 24, 105, 800, 1000, 100, 20, 100"
如果需要更复杂的匹配,Regex() 或 Pat Match() 是选项,但在我看来,使用其当前文档和从文档中找到的示例来学习 Pat Match() 并不值得。 向 Regex Match() 添加标志以查找所有不重叠的模式出现将使此类任务更容易完成。
回复:如何识别段落中的数字并将其提取为新表中的列?我尝试扩展您的建议,但无法使其发挥作用。不幸的是,我的发现让我感到困惑,所以我希望能够得到更多的意见。
以下代码片段将找到“10 kg”。到目前为止一切顺利。
Names Default To Here( 1 );
text = "The bags are weighed on a mobile scale at storage location. Each 5. bag is spear sampled. <BR />The primary sample of A: 10 kg, B: 20 kg, C: 30 kg, 40 kg and E: 50 kg was dried for 24 hours at 105 C. <BR />The sample was subject to repeated splitting, leaving some 800-1000 grams which was further milled to <BR />pass a 100-mesh test sieve. The crushed sample is blended in a V-blender for 20 minutes and transferred <BR />to 100 ml waterproof plastic bottle and sealed.";<BR />
// Use a regular expression to find all the numbers, including decimals
matches = Regex( text, "\d+([.,]\d+)?\s*kg" );
Show( matches );
让我们尝试将其编写成一个循环,以查找字符串中与“kg”相关的所有重量。我可以找到所有单个单词,并将它们组合成对。在这些对中,我认为应该可以顺利找到“10 kg”、“20 kg”、“30 kg”等对,但代码却顽固地拒绝找到任何匹配项并将它们放入 matches={} 列表中。
我尝试将 regexPattern 移到 If 函数内部,并且尝试将“combined”以及“regexPattern”变量包装在 Eval() 中。
Names Default To Here( 1 );<BR />// the text
text = "The bags are weighed on a mobile scale at storage location. Each 5. bag is spear sampled. <BR />The primary sample of A: 10 kg, B: 20 kg, C: 30 kg, 40 kg and E: 50 kg was dried for 24 hours at 105 C. <BR />The sample was subject to repeated splitting, leaving some 800-1000 grams which was further milled to <BR />pass a 100-mesh test sieve. The crushed sample is blended in a V-blender for 20 minutes and transferred <BR />to 100 ml waterproof plastic bottle and sealed.";<BR />
// break text into words and print each of them. works as expected
words = Words( text );
For( i = 1, i <= N Items( words ), i++,
Print( words[i] )
);
// regex pattern variable
regexPattern = "\d+([.,]\d+)?\s*kg";
// empty list to store matches
matches = {};
// loop over all words in text to create and print pairs of words (we're looking for "10 kg", "20 kg" and so on). works fine
For( i = 1, i <= N Items( words ) - 1, i++,
combined = words[i] || " " || words[i + 1];
Print( "Checking: " || combined );
);
<BR /><BR /><BR />// THIS PART DOESN'T WORK
// loop once more over all words from text to create pairs of words and look for the regexPattern
For( i = 1, i <= N Items( words ) - 1, i++,
combined = words[i] || " " || words[i + 1];
// check if the current combined string matches the pattern (e.g., "10 kg")
If( Regex Match( combined, regexPattern ),
Insert Into( matches, combined );
Print( "Match found: " || combined );
,
// else
Print( "No match for: " || combined );
);
);我忽略了什么?
回复:如何识别段落中的数字并将其提取为新表中的列?我还没有仔细检查所有内容,但是粗略地看一下脚本,“。” 是一个通配符,因此您需要对其进行转义。
回复:如何识别段落中的数字并将其提取为新表中的列?谢谢,但这正是我想用第一个代码片段演示的:它确实正确地识别了示例中“数字 + kg”的第一个匹配项(即“10 kg”)。如果它只起作用一次,那么它不应该对所有标记/单词对都起作用吗?
回复:如何识别段落中的数字并将其提取为新表中的列?哦,是的,抱歉。我应该醒来喝杯咖啡再发帖。我以前遇到过这种事,并在这里贴了一个答案(当时我在上一份工作)。
回复:如何识别段落中的数字并将其提取为新表中的列?哈哈,没什么好担心的!有友好的意见总是好的:)
回复:如何识别段落中的数字并将其提取为新表中的列?支持 hogi 愿望清单项目的好例子。
正则表达式为所有标志添加选项
回复:如何识别段落中的数字并将其提取为新表中的列?感谢迄今为止的所有意见 - 现在正在测试
回复:如何识别段落中的数字并将其提取为新表中的列?我不知道您是在尝试练习 RegEx,还是在尝试真正找到找到所有对 kg 值的引用的解决方案。如果是后者,则下面的 JSL 是一种非常有效的解决方案
Names Default To Here( 1 );// the text
text = "The bags are weighed on a mobile scale at storage location. Each 5. bag is spear sampled. The primary sample of A: 10 kg, B: 20 kg, C: 30 kg, 40 kg and E: 50 kg was dried for 24 hours at 105 C. The sample was subject to repeated splitting, leaving some 800-1000 grams which was further milled to pass a 100-mesh test sieve. The crushed sample is blended in a V-blender for 20 minutes and transferred to 100 ml waterproof plastic bottle and sealed.";
// break text into words and print each of them. works as expected
words = Words( text );
// regex pattern variable
regexPattern = "\d+([.,]\d+)?\s*kg";
// empty list to store matches
matches = {};
potential = loc(words,"kg");
If( length(potential) > 0,
for each( {kg}, potential,
if( isMissing(words[kg-1])==0,
insert into(matches, words[kg -1] || " kg")
)
)
);回复:如何识别段落中的数字并将其提取为新表中的列?实际上,两者都有。感谢您的帮助和耐心。我从未使用过“loc()”,所以这是很好的学习。
复制粘贴整个代码块并附加“Print(matches)”可得到 {“40 kg”, “50 kg”}。这与“potential = Loc(words, “kg” );”一致,仅找到“[32, 36]”。很有趣。
- 为什么你的解决方案没有找到“10 公斤”、“20 公斤”和“30 公斤”?
- 为什么我的解决方案没有找到任何东西?
回复:如何识别段落中的数字并将其提取为新表中的列?是逗号。“kg,”把它打乱了。
回复:如何识别段落中的数字并将其提取为新表中的列?Words() 函数仅使用空格作为分隔符,因此,带有逗号 ( kg, ) 或句号 ( kg. ) 的 kg 单词会在提取的单词中保留这些字符。只需添加逗号和句号作为分隔符来检查空格,即可轻松更改此设置
改变
words = Words( text );
到
words = Words( text," ,." );
回复:如何识别段落中的数字并将其提取为新表中的列?您可以使用一个 while 循环来完成这样的操作(这将修改原始字符串,因此您可能需要使用它的副本)
Names Default To Here(1);
text = "The bags are weighed on a mobile scale at storage location. Each 5. bag is spear sampled. The primary sample of A: 10 kg, B: 20 kg, C: 30 kg, 40 kg and E: 50 kg was dried for 24 hours at 105 C. The sample was subject to repeated splitting, leaving some 800-1000 grams which was further milled to pass a 100-mesh test sieve. The crushed sample is blended in a V-blender for 20 minutes and transferred to 100 ml waterproof plastic bottle and sealed.";
res = {};
While(!Is Missing(match = Regex(text, "\d+.kg")),
Insert Into(res, match);
text = Substr(text, Contains(text, match) + Length(match));
);
show(res);
或者如果你能弄清楚 Pat Match 是如何工作的(我从来没有真正能够正确地做到这一点)
Names Default To Here(1);
text = "The bags are weighed on a mobile scale at storage location. Each 5. bag is spear sampled. The primary sample of A: 10 kg, B: 20 kg, C: 30 kg, 40 kg and E: 50 kg was dried for 24 hours at 105 C. The sample was subject to repeated splitting, leaving some 800-1000 grams which was further milled to pass a 100-mesh test sieve. The crushed sample is blended in a V-blender for 20 minutes and transferred to 100 ml waterproof plastic bottle and sealed.";
found_list = {};
Pat Match(
text,
Pat Any("., ") + Pat Regex("(\d+.kg)") >> match + Pat Any("., ") + Pat Test(
Insert Into(found_list, match);
0;
)
);
Show(found_list);回复:如何识别段落中的数字并将其提取为新表中的列?@CalibrationBear ,你找到解决方案了吗?可以分享一下吗?谢谢:)