How do I compare a string with a #single word?
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Kratos
el 27 de Feb. de 2015
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.
0 comentarios
Respuesta aceptada
Guillaume
el 27 de Feb. de 2015
Editada: Guillaume
el 28 de Feb. de 2015
All of the solution proposed so far have the problem that they'll find the hashtag #retro in the hashtag #retrorocket, which I don't think is wanted.
At the end of the day, a very good parser for strings has been invented long ago, it's called a regular expression. Here is a way to get your matches without the need of a loop:
hiplist = {'denim'; 'vinyl'; 'retro'};
teststr = 'the denim #vinyl was #crazy #retro but the #retrorocket went backward';
%build regular expression from hitlist:
regpattern = sprintf('\\<(?<=#)(%s)\\>', strjoin(hiplist, '|'));
matches = regexp(teststr, regpattern, 'match')
The pattern I've built only match hashtags surrounded by whitespaces (regular spaces, newlines, tabs, etc.) or at the beginning and end of the string. A hashtag followed by a punctuation mark will not be detected, but it's a fairly small change to the regex if wanted.
8 comentarios
Guillaume
el 28 de Feb. de 2015
I've given you 99% of the solution in my last answer. You only need to modify slightly one of the regular expression (and it's just removing part of it) and make it case insensitive (it's explained how in the doc), add one line to calculate the score and you're done.
As it is an assignment, I'm not going to help you any further.
Más respuestas (2)
Image Analyst
el 27 de Feb. de 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
el 27 de Feb. de 2015
Editada: Joseph Cheng
el 27 de Feb. de 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 comentarios
Joseph Cheng
el 27 de Feb. de 2015
Editada: Joseph Cheng
el 27 de Feb. de 2015
good call, i don't deal with cells often when hard coded in.
Ver también
Categorías
Más información sobre Particle Swarm en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!