How do I compare a string with a #single word?
Afficher commentaires plus anciens
Hello I am trying to compare a string with 'word'. for example if the word ‘retro’ is in the text file, and ‘#retro’ appear in the str,
str = It was #crazy #retro.
word = 'retro'
How do I compare the str with word including the hashtag. I tried using
strfind(lower(str), '#line2')
but it gave me an empty vector.
Thank you.
Réponse acceptée
Plus de réponses (2)
Image Analyst
le 27 Fév 2015
Not sure what you're exactly looking to do so I just offer some possibilities:
str = 'It was #crazy #retro.'
word = 'retro'
hashLocations = str == '#' % Logical vector
hashIndexes = find(hashLocations) % Actual index numbers.
location = strfind(lower(str), '#retro') + 1 % Skips past #
location = strfind(lower(str), word)
In command window:
hashLocations =
0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0
hashIndexes =
8 15
location =
16
location =
16
Joseph Cheng
le 27 Fév 2015
Modifié(e) : Joseph Cheng
le 27 Fév 2015
you can try something like this where hiplist is your hipster word dictionary. Then in my loop there you test for hits against the dictionary in the test string and then look for n-1 index for whether it was a pound sign and award points for each one.
hiplist = [{'denim'};{'vinyl'};{'retro'}];
teststr = 'the denim #vinyl was #crazy #retro.';
pointsawarded=0;
for ind = 1:length(hiplist)
det = strfind(teststr,hiplist{ind});
if ~isempty(det)
if det>1 & teststr(det-1)=='#'
pointsawarded = pointsawarded+2;
end
end
end
disp(teststr)
disp(['got ' num2str(pointsawarded) ' points'])
oh and use lower such that the detection isn't case sensitive.
5 commentaires
That hiplist declaration just look weird to me. Isn't:
hiplist = {'denim'; 'vinyl'; 'retro'};
simpler?
Also, your code doesn't deal with strfind returning more than one match. You won't get any point on this one:
teststr = 'retro #retro';
Joseph Cheng
le 27 Fév 2015
Modifié(e) : Joseph Cheng
le 27 Fév 2015
slight modification
hiplist = [{'denim'};{'vinyl'};{'retro'}];
teststr = 'the retro denim #vinyl was #crazy #retro.';
pointsawarded=0;
for ind = 1:length(hiplist)
det = strfind(teststr,hiplist{ind});
if ~isempty(det)
if det>1
pointsawarded = pointsawarded+2*sum(teststr(det-1)=='#');
end
end
end
disp(teststr)
disp(['got ' num2str(pointsawarded) ' points'])
moved the check for number of # infront of the hits to the sum. This is to mitigate if a hipster word was used but it was not tagged.
what is not implemented and needs an adaptation is what happens when in the teststr retro is the first word. then the det>1 if statement gets skipped completely.
@Joseph Cheng: a simpler way of generating this cell array:
hiplist = [{'denim'};{'vinyl'};{'retro'}];
is like this:
hiplist = {'denim'; 'vinyl'; 'retro'};
Joseph Cheng
le 27 Fév 2015
Modifié(e) : Joseph Cheng
le 27 Fév 2015
good call, i don't deal with cells often when hard coded in.
Kratos
le 27 Fév 2015
Catégories
En savoir plus sur Visualization and Analytics dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!