如何識別段落中的數字並提取為新表格中的列?我有一個文字段落,其中包含不同句子中的多個數字,我想提取這些數字並將它們堆疊在新表格的一列中。
這句話看起來像這樣:
袋子在存放地點的移動秤上稱重。 5. 每袋均經過矛取樣。初級樣品A:10公斤,B:20公斤,C:30公斤,
將40kg和E:50kg在105℃下乾燥24小時。將粉碎後的樣品在V型攪拌機中攪拌20分鐘,然後轉移至100ml防水塑膠瓶中並密封。
此外,不同初級樣本的數量可以從 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公斤”,“20公斤”,“30公斤”等對應該很順利,但代碼頑固地拒絕找到任何匹配並將它們放入匹配中={} 列表。
我嘗試將 regexPattern 移至 If 函數內,並且嘗試在 Eval() 中包裝“combined”以及“regexPattern”變數。
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 );
);
);我在忽略什麼?
回覆:如何識別段落中的數字並提取為新表格中的列?我沒有把所有內容都講一遍,只是粗略地看了一下劇本,“。”是一個通配符,所以你需要轉義它。
回覆:如何識別段落中的數字並提取為新表格中的列?謝謝,但這就是我想用第一個片段演示的內容:它確實正確地識別了示例中與“a number + kg”(即“10 kg”)的第一個匹配項。如果它有效一次,那麼它不應該對所有標記/單字對都有效嗎?
回覆:如何識別段落中的數字並提取為新表格中的列?哦,是的,抱歉。在發文之前我應該起床喝杯咖啡。我以前遇到過這類事情,並在這裡給出了答案(當我在上一份工作時)。
回覆:如何識別段落中的數字並提取為新表格中的列?哈哈,這個沒什麼好擔心的!有友善的輸入總是好的:)
回覆:如何識別段落中的數字並提取為新表格中的列?支持 hogi 願望清單項目的好例子。
正規表示式為所有標誌新增選項
回覆:如何識別段落中的數字並提取為新表格中的列?感謝迄今為止的所有輸入 - 立即測試
回覆:如何識別段落中的數字並提取為新表格中的列?我不知道您是否想要執行正規表示式,或者您是否正在嘗試實際找到找到所有對公斤值的引用的解決方案。如果是後者,下面的 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 kg”、“20 kg”和“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 ,您找到解決方案了嗎?謝謝:)