Sijo K Sijo K - 1 month ago 4
R Question

How to check for the presence of a location in a sentence in R?

I am very new to R.

I want to check a particular sentence has a location name or not. For example

sentence <- " I am travelling to Tulsa"
.

How should i write a code so that the program can understand that the sentence contains a world called "Tulsa" and which is a location. Should i build a manual dictionary containing the required locations?

Please guide me.

Answer

I think R has not implemented a list of Towns or other locations as this is very specific. Thus I would search for a list in the web. If you have such a list, you can work e.g. with

grep("Tulsa", sentence, fixed = T) # [1] 1
grep("Tulsaa", sentence, fixed = T) # integer(0)

You can then work with a list, but things might get demanding:

sentences <- c("I am travelling to Tulsa", "I am travelling to Washington and York", 
               "I am travelling to Tulsa and New York")
cities <- c("Tulsa", "New York", "York")
grep(cities[1], sentences, fixed = T) # [1] 1 3
grep(cities[2], sentences, fixed = T) # [1] 3
grep(cities[3], sentences, fixed = T) # [1] 2 3

I hope that helps. Speed optimzation will be a different topic...