Come faccio a identificare i numeri in un paragrafo ed estrarli come colonna in una nuova tabella?Ho un paragrafo di testo che contiene diversi numeri in frasi diverse e vorrei estrarre tali numeri e impilarli in una colonna di una nuova tabella.
La frase si presenta così:
I sacchi vengono pesati su una bilancia mobile nel luogo di stoccaggio. Ogni 5. sacco viene campionato con una lancia. Il campione primario di A: 10 kg, B: 20 kg, C: 30 kg,
40 kg ed E: 50 kg sono stati essiccati per 24 ore a 105 °C. Il campione è stato sottoposto a ripetute divisioni, lasciando circa 800-1000 grammi che sono stati ulteriormente macinati per passare un setaccio di prova da 100 maglie. Il campione frantumato è stato miscelato in un frullatore a V per 20 minuti e trasferito in una bottiglia di plastica impermeabile da 100 ml e sigillata.
Inoltre il numero di campioni primari diversi può variare da 1 fino a 20 o 30.
Sarei molto grato se qualcuno potesse indicarmi la giusta direzione con uno script. Ho provato a scriverlo con l'aiuto di Copilot, ma non ho ottenuto il risultato che volevo.
Grazie mille!
Automazione e scriptingR: Come faccio a identificare i numeri in un paragrafo ed estrarli come colonna in una nuova tabella?Ecco un modo per farlo:
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 );
Sono sicuro che ci sarà qualcuno che fornirà anche una soluzione RegEx().
R: Come faccio a identificare i numeri in un paragrafo ed estrarli come colonna in una nuova tabella?Puoi usare Words per questo, ma devi avere un elenco di caratteri non numerici. Puoi quindi usare Concat Items() per trasformare l'elenco che ottieni in una stringa
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"
Se è richiesta una corrispondenza più complicata, Regex() o Pat Match() sono opzioni, ma secondo me non vale la pena imparare Pat Match() con la sua documentazione attuale e gli esempi trovati nella documentazione. Aggiungere un flag a Regex Match() per trovare tutte le occorrenze non sovrapposte del pattern renderebbe compiti come questo molto più facili da completare.
R: Come faccio a identificare i numeri in un paragrafo ed estrarli come colonna in una nuova tabella?Ho provato ad ampliare il tuo suggerimento, ma non sono riuscito a farlo funzionare. Sfortunatamente, le mie scoperte mi confondono, quindi spero che sia possibile ottenere qualche altro contributo, per favore.
Il frammento di codice seguente troverà "10 kg". Fin qui tutto bene.
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 );
Proviamo a scrivere questo in un ciclo per trovare tutti i pesi associati a "kg" nella stringa. Posso trovare tutte le singole parole e posso combinarle in coppie. All'interno di queste coppie, pensavo che sarebbe stato facile trovare coppie come "10 kg", "20 kg", "30 kg" e così via, ma il codice si rifiuta ostinatamente di trovare corrispondenze e di inserirle nell'elenco matches={}.
Ho provato a spostare regexPattern all'interno della funzione If e ho provato a racchiudere "combined" e la variabile "regexPattern" in 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 );
);
);Cosa sto trascurando?
R: Come faccio a identificare i numeri in un paragrafo ed estrarli come colonna in una nuova tabella?Non ho esaminato tutti i dettagli, ma da una rapida occhiata allo script, "." è un carattere jolly, quindi dovresti evitarlo.
R: Come faccio a identificare i numeri in un paragrafo ed estrarli come colonna in una nuova tabella?Grazie, ma è quello che volevo dimostrare con il primo frammento: identifica correttamente la prima corrispondenza con "un numero + kg" (ad esempio, "10 kg") nell'esempio. Se funziona una volta, non dovrebbe funzionare per tutte le coppie di token/parole?
R: Come faccio a identificare i numeri in un paragrafo ed estrarli come colonna in una nuova tabella?Oh, sì, scusa. Dovrei svegliarmi e prendere il mio caffè prima di postare qui. Mi sono imbattuto in questo genere di cose prima e ho pubblicato una risposta (mentre ero al mio ultimo lavoro) qui .
R: Come faccio a identificare i numeri in un paragrafo ed estrarli come colonna in una nuova tabella?Haha, non c'è niente di cui preoccuparsi! È sempre bello avere un contributo amichevole:)
R: Come faccio a identificare i numeri in un paragrafo ed estrarli come colonna in una nuova tabella?Buon motivo per supportare l'articolo della lista dei desideri di Hogi.
Opzione di aggiunta regex per tutti i flag
R: Come faccio a identificare i numeri in un paragrafo ed estrarli come colonna in una nuova tabella?Grazie per tutti i contributi finora - li sto testando ora
R: Come faccio a identificare i numeri in un paragrafo ed estrarli come colonna in una nuova tabella?Non so se quello che stai cercando di fare è esercitare RegEx, o se stai cercando di trovare effettivamente una soluzione per trovare tutti i riferimenti ai valori kg. Se è il secondo caso, il JSL sottostante è una soluzione molto efficiente
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")
)
)
);R: Come faccio a identificare i numeri in un paragrafo ed estrarli come colonna in una nuova tabella?In realtà, entrambi. Grazie per il tuo aiuto e la tua pazienza. Non ho mai usato "loc()", quindi è un buon apprendimento.
Copia e incolla l'intero blocco di codice e aggiungi "Print( matches)" e ottieni {"40 kg", "50 kg"}. Ciò è coerente con "potential = Loc( words, "kg" );" trovando solo "[32, 36]". Interessante.
- Perché la tua soluzione non trova "10 kg", "20 kg" e "30 kg"?
- Perché la mia soluzione non trova nulla?
R: Come faccio a identificare i numeri in un paragrafo ed estrarli come colonna in una nuova tabella?Sono le virgole. "kg" confonde tutto.
R: Come faccio a identificare i numeri in un paragrafo ed estrarli come colonna in una nuova tabella?La funzione Words() usa solo spazi come delimitatori, quindi, kg parole con virgole (kg, ) o punti (kg. ) lasciano quei caratteri nella parola estratta. Questo può essere facilmente modificato aggiungendo la virgola e il punto come delimitatori da controllare insieme allo spazio
Modifica
words = Words( text );
A
words = Words( text," ,." );
R: Come faccio a identificare i numeri in un paragrafo ed estrarli come colonna in una nuova tabella?Puoi usare un ciclo while per qualcosa del genere (ciò modificherà la stringa originale, quindi potresti volerne usare una copia)
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);
Oppure Pat Match se riesci a capire come funziona (io non sono mai riuscito a farlo correttamente)
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);R: Come faccio a identificare i numeri in un paragrafo ed estrarli come colonna in una nuova tabella?@CalibrationBear , hai trovato una soluzione e la condividerai? Grazie:)