How I can find a specific word in a notepad text?

1 Ansicht (letzte 30 Tage)
Armina Petrean
Armina Petrean am 6 Apr. 2023
Bearbeitet: dpb am 6 Apr. 2023
I have a notepad text and i want to find a specific word in this and then to count how many times do i found him .
  1 Kommentar
Armina Petrean
Armina Petrean am 6 Apr. 2023
for example i want to find the word " lege" in the document and then to count how mnay times apear this specific word.in a text like that :

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

dpb
dpb am 6 Apr. 2023
Bearbeitet: dpb am 6 Apr. 2023
"i want to find the word " lege" in the document and then to count how mnay times apear this specific word"
txt=readlines('yourfile.txt'); % return file as string array
MyWord="lege"; % define the lookup string
nTimes=count(txt,MyWord,'IgnoreCase',true); % find and count
It doesn't appear from the many posts you've done a lot of digging/reading in the doc nor looked at the examples on analyzing text...start with <TopLevelCharacter&Strings> and dig down. In particular to get an overview of facilities very quickly click on the 'Functions' link at the top and peruse through it for features.
NOTA BENE AND ADDENDUM:
The above will find/count the string "lege" anywhere in the document and will match the string "LEGEA ..." as well as "PRIN LEGE NU" that also occurs in the sample text (btw, if you really want help, attach the actual text as text, not as an image thereof so folks can do something with it besides just look at it).
The Q? isn't totally clear as to what kind of match you're looking for; you also say to find "this specific word" which could be interpreted to be only the latter of the two above examples is wanted. If that were to be the case, then
wordpat=whitespacePattern+MyWord+whitespacePattern;
nTimes=count(txt,wordpat,'IgnoreCase',true);
will find only the cases where the wanted string is separated by whitespace. It wouldn't find it if it were the last word in a sentence or had some other punctuation directly before/behind it, though.
  2 Kommentare
dpb
dpb am 6 Apr. 2023
Bearbeitet: dpb am 6 Apr. 2023
So, did you try the sample code??? -- and what's the answer to the Q? regarding the search type/match wanted?
fn='https://www.mathworks.com/matlabcentral/answers/uploaded_files/1347944/proba.txt';
txt=readlines(fn);
txt(1:3)
ans = 3×1 string array
"CODUL CIVIL DIN 2009 (LEGEA NR. 287/2009) - REPUBLICARE CODUL CIVIL DIN 2009 DIN 2011.07.15 STATUS: ACTE ÎN VIGOARE VERSIUNE DE LA: 18 AUGUST 2022 DATĂ ACT: 17-IUL-2009 EMITENT: PARLAMENTUL ÎN TEMEIUL ART. 220 ALIN. (1) DIN LEGEA NR. 71/2011 PENTRU PUNEREA ÎN APLICARE A LEGII NR. 287/2009 PRIVIND CODUL CIVIL, PUBLICATĂ ÎN MONITORUL OFICIAL AL ROMÂNIEI, PARTEA I, NR. 409 DIN 10 IUNIE 2011, CODUL CIVIL INTRĂ ÎN VIGOARE LA DATA DE 1 OCTOMBRIE 2011. PENTRU A VEDEA EXPUNEREA DE MOTIVE A ACESTUI ACT APĂSAŢI AICI. TITLUL PRELIMINAR:DESPRE LEGEA CIVILĂ)" "DISPOZIŢIILE DE PUNERE ÎN APLICARE A TITLULUI PRELIMINAR SUNT CUPRINSE ÎN ART. 8 DIN LEGEA NR. 71/2011. CAPITOLUL I:DISPOZIŢII GENERALE ART. 1: IZVOARELE DREPTULUI CIVIL (1)SUNT IZVOARE ALE DREPTULUI CIVIL LEGEA, UZANŢELE ŞI PRINCIPIILE GENERALE ALE DREPTULUI.(2)ÎN CAZURILE NEPREVĂZUTE DE LEGE SE APLICĂ UZANŢELE, IAR ÎN LIPSA ACESTORA, DISPOZIŢIILE LEGALE PRIVITOARE LA SITUAŢII ASEMĂNĂTOARE, IAR CÂND NU EXISTĂ ASEMENEA DISPOZIŢII, PRINCIPIILE GENERALE ALE DREPTULUI.(3)ÎN MATERIILE REGLEMENTATE PRIN LEGE, UZANŢELE SE APLICĂ NUMAI ÎN MĂSURA ÎN CARE LEGEA TRIMITE ÎN MOD EXPRES LA ACESTEA.(4)NUMAI UZANŢELE CONFORME ORDINII PUBLICE ŞI BUNELOR MORAVURI SUNT RECUNOSCUTE CA IZVOARE DE DREPT.(5)PARTEA INTERESATĂ TREBUIE SĂ FACĂ DOVADA EXISTENŢEI ŞI A CONŢINUTULUI UZANŢELOR. UZANŢELE PUBLICATE ÎN CULEGERI ELABORATE DE CĂTRE ENTITĂŢILE SAU ORGANISMELE AUTORIZATE ÎN DOMENIU SE PREZUMĂ CĂ EXISTĂ, PÂNĂ LA PROBA CONTRARĂ.(6)ÎN SENSUL" "PREZENTULUI COD, PRIN UZANŢE SE ÎNŢELEGE OBICEIUL (CUTUMA) ŞI UZURILE PROFESIONALE. ART. 2: OBIECTUL ŞI CONŢINUTUL CODULUI CIVIL (1)DISPOZIŢIILE PREZENTULUI COD REGLEMENTEAZĂ RAPORTURILE PATRIMONIALE ŞI NEPATRIMONIALE DINTRE PERSOANE, CA SUBIECTE DE DREPT CIVIL.(2)PREZENTUL COD ESTE ALCĂTUIT DINTR-UN ANSAMBLU DE REGULI CARE CONSTITUIE DREPTUL COMUN PENTRU TOATE DOMENIILE LA CARE SE REFERĂ LITERA SAU SPIRITUL DISPOZIŢIILOR SALE.ART. 3: APLICAREA GENERALĂ A CODULUI CIVIL (1)DISPOZIŢIILE PREZENTULUI COD SE APLICĂ ŞI RAPORTURILOR DINTRE PROFESIONIŞTI, PRECUM ŞI RAPORTURILOR DINTRE ACEŞTIA ŞI ORICE ALTE SUBIECTE DE DREPT CIVIL.(2)SUNT CONSIDERAŢI PROFESIONIŞTI TOŢI CEI CARE EXPLOATEAZĂ O ÎNTREPRINDERE.(3)CONSTITUIE EXPLOATAREA UNEI ÎNTREPRINDERI EXERCITAREA SISTEMATICĂ, DE CĂTRE UNA SAU MAI MULTE PERSOANE, A UNEI ACTIVITĂŢI ORGANIZATE CE CONSTĂ ÎN PRODUCEREA, ADMINISTRAREA ORI ÎNSTRĂINAREA DE BUNURI SAU ÎN PRESTAREA DE SERVICII, INDIFERENT DACĂ ARE SAU NU UN SCOP LUCRATIV.ART. 4: APLICAREA PRIORITARĂ A TRATATELOR INTERNAŢIONALE PRIVIND DREPTURILE OMULUI (1)ÎN MATERIILE REGLEMENTATE DE PREZENTUL COD, DISPOZIŢIILE PRIVIND DREPTURILE ŞI LIBERTĂŢILE PERSOANELOR VOR FI INTERPRETATE ŞI APLICATE ÎN CONCORDANŢĂ CU CONSTITUŢIA, DECLARAŢIA UNIVERSALĂ A DREPTURILOR OMULUI, PACTELE ŞI CELELALTE TRATATE LA CARE ROMÂNIA ESTE PARTE.(2)DACĂ EXISTĂ NECONCORDANŢE ÎNTRE PACTELE ŞI TRATATELE PRIVITOARE LA DREPTURILE FUNDAMENTALE ALE OMULUI, LA CARE ROMÂNIA ESTE PARTE, ŞI PREZENTUL COD, AU PRIORITATE REGLEMENTĂRILE INTERNAŢIONALE, CU EXCEPŢIA CAZULUI ÎN CARE PREZENTUL COD CONŢINE DISPOZIŢII MAI FAVORABILE.ART. 5: APLICAREA PRIORITARĂ A DREPTULUI UNIUNII EUROPENE ÎN MATERIILE REGLEMENTATE DE PREZENTUL COD, NORMELE DREPTULUI UNIUNII EUROPENE SE APLICĂ ÎN MOD PRIORITAR, INDIFERENT DE CALITATEA SAU STATUTUL PĂRŢILOR.CAPITOLUL II:APLICAREA LEGII CIVILE"
MyWord="lege";
nTimesA=count(txt,MyWord,'IgnoreCase',true)
nTimesA = 21×1
3 6 1 4 10 2 3 3 1 2
NA=sum(nTimesA)
NA = 61
wordpat=whitespacePattern+MyWord+whitespacePattern;
nTimesB=count(txt,wordpat,'IgnoreCase',1)
nTimesB = 21×1
0 1 0 0 4 1 0 0 1 1
NB=sum(nTimesB)
NB = 11
Other than correcting a couple missepllings, looks like the suggestion would works just fine...undoubtedly some more thoughtful search patterns would be useful to find specific items of interest -- like the first "(LEGEA..." is found by the first search but not the second because of the leading "(" instead of being separated by whitespace. That's easily-enough fixed, but left as "exercise for student".

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Vilém Frynta
Vilém Frynta am 6 Apr. 2023
I have created and attached a file with Lorem Ipsum text as an example.
Now, I will try to show you how to find word 'ipsum' and it's counts in the file.
% Read the file
str = fileread('ipsum.txt');
% Put all the words into the cell
wrds = strsplit(str, ' ')
wrds = 1×318 cell array
Columns 1 through 14 {'Lorem'} {'ipsum'} {'dolor'} {'sit'} {'amet,'} {'consectetur'} {'adipiscing'} {'elit.'} {'Duis'} {'luctus,'} {'quam'} {'id'} {'egestas'} {'vestibulum,'} Columns 15 through 29 {'nunc'} {'tellus'} {'cursus'} {'enim,'} {'vitae'} {'egestas'} {'enim'} {'nisi'} {'vitae'} {'metus.'} {'Aenean'} {'porta,'} {'nisl'} {'sit'} {'amet'} Columns 30 through 44 {'accumsan'} {'molestie,'} {'justo'} {'urna'} {'finibus'} {'nibh,'} {'eget'} {'ultrices'} {'magna'} {'neque'} {'id'} {'urna.'} {'Fusce'} {'at'} {'velit'} Columns 45 through 59 {'nec'} {'felis'} {'accumsan'} {'bibendum'} {'ac'} {'a'} {'nisl.'} {'Etiam'} {'sodales,'} {'dolor'} {'eu'} {'eleifend'} {'egestas,'} {'est'} {'nulla'} Columns 60 through 73 {'venenatis'} {'risus,'} {'aliquet'} {'efficitur'} {'tortor'} {'mauris'} {'eget'} {'mauris.'} {'Proin'} {'nunc'} {'neque,'} {'mollis'} {'vitae'} {'nibh'} Columns 74 through 87 {'eget,'} {'consequat'} {'euismod'} {'ipsum.'} {'Curabitur'} {'interdum'} {'dolor'} {'non'} {'ligula'} {'tempor,'} {'id'} {'viverra'} {'felis'} {'dapibus.'} Columns 88 through 101 {'Class'} {'aptent'} {'taciti'} {'sociosqu'} {'ad'} {'litora'} {'torquent'} {'per'} {'conubia'} {'nostra,'} {'per'} {'inceptos'} {'himenaeos.'} {'In'} Columns 102 through 116 {'euismod'} {'quam'} {'sed'} {'nunc'} {'vehicula,'} {'et'} {'dapibus'} {'lectus'} {'laoreet.'} {'Nam'} {'rutrum'} {'tempus'} {'auctor.'} {'Sed'} {'sed'} Columns 117 through 129 {'eros'} {'ligula.←↵←↵Praes…'} {'eget'} {'velit'} {'mi.'} {'Vestibulum'} {'eleifend'} {'lacinia'} {'augue,'} {'rhoncus'} {'dignissim'} {'dolor'} {'aliquet'} Columns 130 through 142 {'eget.'} {'Quisque'} {'tincidunt'} {'porta'} {'velit,'} {'in'} {'ultrices'} {'ipsum'} {'elementum'} {'non.'} {'Curabitur'} {'imperdiet'} {'lacus'} Columns 143 through 156 {'lacus,'} {'ut'} {'semper'} {'metus'} {'malesuada'} {'at.'} {'Etiam'} {'dictum'} {'ligula'} {'nec'} {'mi'} {'ullamcorper'} {'ultrices.'} {'Nunc'} Columns 157 through 170 {'scelerisque,'} {'dolor'} {'vitae'} {'dapibus'} {'aliquet,'} {'ligula'} {'nisi'} {'tristique'} {'eros,'} {'vel'} {'imperdiet'} {'arcu'} {'quam'} {'et'} Columns 171 through 185 {'massa.'} {'Duis'} {'arcu'} {'nulla,'} {'blandit'} {'at'} {'enim'} {'ut,'} {'fermentum'} {'ultrices'} {'ex.'} {'Aenean'} {'sagittis,'} {'arcu'} {'et'} Columns 186 through 199 {'interdum'} {'posuere,'} {'ex'} {'tortor'} {'maximus'} {'dui,'} {'quis'} {'suscipit'} {'felis'} {'risus'} {'ut'} {'urna.'} {'Duis'} {'sed'} Columns 200 through 213 {'pellentesque'} {'dui.'} {'Phasellus'} {'imperdiet'} {'quis'} {'arcu'} {'scelerisque'} {'varius.'} {'Nam'} {'non'} {'quam'} {'ac'} {'nunc'} {'auctor'} Columns 214 through 228 {'mattis'} {'a'} {'quis'} {'risus.'} {'Pellentesque'} {'at'} {'nisl'} {'nec'} {'nibh'} {'congue'} {'mattis.'} {'Aenean'} {'vitae'} {'ex'} {'ligula.'} Columns 229 through 242 {'Nullam'} {'gravida'} {'orci'} {'id'} {'ornare'} {'lacinia.'} {'Nam'} {'commodo'} {'sodales'} {'felis'} {'non'} {'pretium.←↵←↵Maec…'} {'tempus,'} {'justo'} Columns 243 through 256 {'eget'} {'pharetra'} {'commodo,'} {'diam'} {'ligula'} {'facilisis'} {'lacus,'} {'ac'} {'laoreet'} {'enim'} {'tellus'} {'vitae'} {'tortor.'} {'Nulla'} Columns 257 through 270 {'pretium'} {'ipsum'} {'id'} {'posuere'} {'blandit.'} {'Nam'} {'urna'} {'dui,'} {'scelerisque'} {'quis'} {'erat'} {'non,'} {'semper'} {'cursus'} Columns 271 through 284 {'felis.'} {'Aliquam'} {'at'} {'pharetra'} {'libero.'} {'Sed'} {'vitae'} {'condimentum'} {'enim,'} {'vel'} {'tempor'} {'felis.'} {'Aliquam'} {'pellentesque'} Columns 285 through 297 {'iaculis'} {'elementum.'} {'Sed'} {'id'} {'ligula'} {'scelerisque,'} {'consequat'} {'lorem'} {'eu,'} {'vestibulum'} {'augue.'} {'Suspendisse'} {'lorem'} Columns 298 through 310 {'orci,'} {'aliquam'} {'dignissim'} {'metus'} {'ac,'} {'consectetur'} {'pellentesque'} {'leo.'} {'Cras'} {'ultrices'} {'turpis'} {'non'} {'tristique'} Columns 311 through 318 {'blandit.'} {'Maecenas'} {'varius'} {'diam'} {'id'} {'velit'} {'porta'} {'tincidunt.'}
% Find the 'ipsum' words
idx = strfind(wrds, 'ipsum');
idx = find(not(cellfun('isempty', idx)));
% Find the occurences
N = length(idx)
N = 4

Kategorien

Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange

Produkte


Version

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by