Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex Python match large list of strings

Tags:

python

regex

I have a large list of strings which include whitespaces in them (e.g. "New York", "United States", "North Carolina", "United Arab Emirates", "United Kingdom of Great Britain and Northern Ireland" ...and around 5000+ such strings).

And I have a large text in which may include any of these these strings (e.g. "I went to New York on my way to North Carolina, and eventually will be going to United Arab Emirates.")

What is the best way to efficiently use regular expression to detect the presence of these strings within my text?

Or maybe should I think about it the other way around, such that I extract bigrams from the text and see which strings within this list match these bigrams?


EDIT:

After an interesting discussion with VoronoiPotato, I began to think that it is better to index all the tokens of the items of the large string list, and I managed to do this with the function:

def indexing_list(li):
    index_dict={}
    for i in rl(li):
        words=li[i].split()
        for j in rl(words):
            index=complex(i,j)
            word=words[j].lower()
            try:
                index_dict[word].append(index)
            except:
                index_dict[word]=[index]
    return index_dict

And tried with this list:

[u'United Kingdom of Great Britain and Northern Ireland', u'Democratic People\u2019s Republic of Korea', u'Democratic Republic of the Congo', u'Lao People\u2019s Democratic Republic', u'Saint Vincent and the Grenadines', u'United Republic of Tanzania', u'Iran (Islamic Republic of)', u'Central African Republic', u'Islamic Republic of Iran', u'United States of America', u'Bosnia and Herzegovina', u'Libyan Arab Jamahiriya', u'Saint Kitts and Nevis', u'Sao Tome and Principe', u'Syrian Arab Republic', u'United Arab Emirates', u'Antigua and Barbuda', u'Trinidad and Tobago', u'Dominican Republic', u'Russian Federation', u'Brunei Darussalam', u'Equatorial Guinea', u'Republic of Korea', u'Marshall Islands', u'Papua New Guinea', u'Solomon Islands', u'ComodRivadavia', u'Port-au-Prince', u'DumontDUrville', u'Czech Republic', u'United Kingdom', u'Dar es Salaam', u'Cambridge Bay', u'Coral Harbour', u'Port of Spain', u'Santo Domingo', u'St Barthelemy', u'Swift Current', u'Ujung Pandang', u'Yekaterinburg', u'South Georgia', u'C\xf4te d\u2019Ivoire', u"Cote d'Ivoire", u'Guinea-Bissau', u'Liechtenstein', u'United States', u'Johannesburg', u'Buenos Aires', u'Rio Gallegos', u'Blanc-Sablon', u'Campo Grande', u'Danmarkshavn', u'Dawson Creek', u'Indianapolis', u'North Dakota', u'Rankin Inlet', u'Scoresbysund', u'Longyearbyen', u'Kuala Lumpur', u'Antananarivo', u'Port Moresby', u'Burkina Faso', u'Saudi Arabia', u'Sierra Leone', u'South Africa', u'Turkmenistan', u'Addis Ababa', u'Brazzaville', u'Ouagadougou', u'El Salvador', u'Los Angeles', u'Mexico City', u'Pangnirtung', u'Porto Velho', u'Puerto Rico', u'Rainy River', u'Tegucigalpa', u'Thunder Bay', u'Yellowknife', u'Ho Chi Minh', u'Krasnoyarsk', u'Novosibirsk', u'Ulaanbaatar', u'Vladivostok', u'Broken Hill', u'Isle of Man', u'Kaliningrad', u'Guadalcanal', u'Afghanistan', u'Cock Island', u'El Salvador', u'Netherlands', u'New Zealand', u'Philippines', u'Saint Lucia', u'Switzerland', u'Timor-Leste', u'Casablanca', u'Libreville', u'Lubumbashi', u'Nouakchott', u'Porto-Novo', u'Costa Rica', u'Fort Wayne', u'Grand Turk', u'Guadeloupe', u'Hermosillo', u'Petersburg', u'Louisville', u'Monticello', u'Martinique', u'Montevideo', u'Montserrat', u'Paramaribo', u'Porto Acre', u'Rio Branco', u'St Vincent', u'Whitehorse', u'Antarctica', u'South Pole', u'Choibalsan', u'Phnom Penh', u'Ulan Bator', u'Cape Verde', u'Queensland', u'Yancowinna', u'Bratislava', u'Copenhagen', u'Luxembourg', u'San Marino', u'Simferopol', u'Zaporozhye', u'Kiritimati', u'Yugoslavia', u'Azerbaijan', u'Bangladesh', u'Cape Verde', u'Costa Rica', u'Kazakhstan', u'Kyrgyzstan', u'Luxembourg', u'Madagascar', u'Mauritania', u'Micronesia', u'Montenegro', u'Mozambique', u'San Marino', u'Seychelles', u'Tajikistan', u'Uzbekistan', u'Bujumbura', u'Mogadishu', u'Anchorage', u'Araguaina', u'Catamarca', u'Boa Vista', u'Chihuahua', u'Fortaleza', u'Glace Bay', u'Goose Bay', u'Guatemala', u'Guayaquil', u'Tell City', u'Vincennes', u'Menominee', u'Monterrey', u'New Salem', u'Sao Paulo', u'St Thomas', u'Vancouver', u'Ashkhabad', u'Chongqing', u'Chungking', u'Hong Kong', u'Jerusalem', u'Kamchatka', u'Pontianak', u'Pyongyang', u'Qyzylorda', u'Samarkand', u'Singapore', u'Vientiane', u'Jan Mayen', u'Reykjavik', u'St Helena', u'Australia', u'Lord Howe', u'Melbourne', u'Greenwich', u'Amsterdam', u'Bucharest', u'Gibraltar', u'Ljubljana', u'Mariehamn', u'Podgorica', u'Stockholm', u'Volgograd', u'Christmas', u'Kerguelen', u'Mauritius', u'Enderbury', u'Galapagos', u'Kwajalein', u'Marquesas', u'Pago Pago', u'Rarotonga', u'Tongatapu', u'Argentina', u'Australia', u'Guatemala', u'Indonesia', u'Lithuania', u'Mauritius', u'Nicaragua', u'Singapore', u'Sri Lanka', u'Swaziland', u'Macedonia', u'Venezuela', u'Blantyre', u'Djibouti', u'El Aaiun', u'Freetown', u'Gaborone', u'Khartoum', u'Kinshasa', u'Monrovia', u'Ndjamena', u'Sao Tome', u'Timbuktu', u'Windhoek', u'Anguilla', u'La Rioja', u'San Juan', u'San Luis', u'Asuncion', u'Atikokan', u'Barbados', u'Dominica', u'Edmonton', u'Eirunepe', u'Ensenada', u'Kentucky', u'Mazatlan', u'Miquelon', u'Montreal', u'New York', u'Resolute', u'Santiago', u'Shiprock', u'St Johns', u'St Kitts', u'St Lucia', u'Winnipeg', u'Ashgabat', u'Calcutta', u'Damascus', u'Dushanbe', u'Istanbul', u'Jayapura', u'Katmandu', u'Makassar', u'Sakhalin', u'Shanghai', u'Tashkent', u'Tel Aviv', u'Atlantic', u'Adelaide', u'Brisbane', u'Canberra', u'Lindeman', u'Tasmania', u'Victoria', u'Belgrade', u'Brussels', u'Budapest', u'Chisinau', u'Guernsey', u'Helsinki', u'Sarajevo', u'Tiraspol', u'Uzhgorod', u'Auckland', u'Funafuti', u'Honolulu', u'Johnston', u'Pitcairn', u'Barbados', u'Botswana', u'Bulgaria', u'Cambodia', u'Cameroon', u'Colombia', u'Djibouti', u'Dominica', u'Ethiopia', u'Holy See', u'Honduras', u'Kiribati', u'Malaysia', u'Maldives', u'Mongolia', u'Pakistan', u'Paraguay', u'Portugal', u'Slovakia', u'Slovenia', u'Suriname', u'Thailand', u'Tanzania', u'Viet Nam', u'Zimbabwe', u'Anguilla', u'Abidjan', u'Algiers', u'Conakry', u'Kampala', u'Mbabane', u'Nairobi', u'Tripoli', u'America', u'Antigua', u'Cordoba', u'Mendoza', u'Tucuman', u'Ushuaia', u'Caracas', u'Cayenne', u'Chicago', u'Curacao', u'Detroit', u'Godthab', u'Grenada', u'Halifax', u'Indiana', u'Marengo', u'Winamac', u'Iqaluit', u'Managua', u'Marigot', u'Moncton', u'Nipigon', u'Noronha', u'Phoenix', u'Rosario', u'Tijuana', u'Toronto', u'Tortola', u'Yakutat', u'McMurdo', u'Rothera', u'Baghdad', u'Bahrain', u'Bangkok', u'Bishkek', u'Colombo', u'Irkutsk', u'Jakarta', u'Karachi', u'Kashgar', u'Kolkata', u'Kuching', u'Magadan', u'Nicosia', u'Rangoon', u'Tbilisi', u'Thimphu', u'Yakutsk', u'Yerevan', u'Bermuda', u'Madeira', u'Stanley', u'Andorra', u'Belfast', u'Tallinn', u'Vatican', u'Vilnius', u'Mayotte', u'Reunion', u'Chatham', u'Fakaofo', u'Gambier', u'Norfolk', u'Albania', u'Algeria', u'Armenia', u'Austria', u'Bahamas', u'Bahrain', u'Belarus', u'Belgium', u'Bolivia', u'Burundi', u'Comoros', u'Croatia', u'Denmark', u'Ecuador', u'Eritrea', u'Estonia', u'Finland', u'Georgia', u'Germany', u'Grenada', u'Hungary', u'Iceland', u'Ireland', u'Jamaica', u'Lebanon', u'Lesotho', u'Liberia', u'Morocco', u'Myanmar', u'Namibia', u'Nigeria', u'Moldova', u'Romania', u'Senegal', u'Somalia', u'Tunisia', u'Ukraine', u'Uruguay', u'Vanuatu', u'Asmara', u'Asmera', u'Bamako', u'Bangui', u'Banjul', u'Bissau', u'Douala', u'Harare', u'Kigali', u'Luanda', u'Lusaka', u'Malabo', u'Maputo', u'Maseru', u'Niamey', u'Belize', u'Bogota', u'Cancun', u'Cayman', u'Cuiaba', u'Dawson', u'Denver', u'Havana', u'Inuvik', u'Juneau', u'La Paz', u'Maceio', u'Manaus', u'Merida', u'Nassau', u'Recife', u'Regina', u'Virgin', u'Mawson', u'Palmer', u'Vostok', u'Arctic', u'Almaty', u'Anadyr', u'Aqtobe', u'Beirut', u'Brunei', u'Harbin', u'Kuwait', u'Manila', u'Muscat', u'Riyadh', u'Saigon', u'Taipei', u'Tehran', u'Thimbu', u'Urumqi', u'Azores', u'Canary', u'Faeroe', u'Currie', u'Darwin', u'Hobart', u'Sydney', u'Europe', u'Athens', u'Berlin', u'Dublin', u'Jersey', u'Lisbon', u'London', u'Madrid', u'Geneva', u'Monaco', u'Moscow', u'Prague', u'Samara', u'Skopje', u'Tirane', u'Vienna', u'Warsaw', u'Zagreb', u'Zurich', u'Chagos', u'Comoro', u'Easter', u'Kosrae', u'Majuro', u'Midway', u'Noumea', u'Ponape', u'Saipan', u'Tahiti', u'Tarawa', u'Wallis', u'Andora', u'Angola', u'Belize', u'Bhutan', u'Brazil', u'Canada', u'Cyprus', u'France', u'Gambia', u'Greece', u'Guinea', u'Guyana', u'Israel', u'Jordan', u'Kuwait', u'Latvia', u'Malawi', u'Mexico', u'Monaco', u'Norway', u'Panama', u'Poland', u'Rwanda', u'Serbia', u'Sweden', u'Turkey', u'Tuvalu', u'Uganda', u'Zambia', u'Accra', u'Cairo', u'Ceuta', u'Dakar', u'Lagos', u'Tunis', u'Jujuy', u'Aruba', u'Bahia', u'Belem', u'Boise', u'Vevay', u'Thule', u'Casey', u'Davis', u'Syowa', u'Amman', u'Aqtau', u'Dacca', u'Dhaka', u'Dubai', u'Kabul', u'Macao', u'Macau', u'Qatar', u'Seoul', u'Tokyo', u'Faroe', u'Eucla', u'Perth', u'Malta', u'Minsk', u'Paris', u'Sofia', u'Vaduz', u'Cocos', u'Efate', u'Nauru', u'Palau', u'Samoa', u'Benin', u'Chile', u'China', u'Congo', u'Egypt', u'Gabon', u'Ghana', u'Haiti', u'India', u'Italy', u'Japan', u'Kenya', u'Libya', u'Malta', u'Nauru', u'Nepal', u'Niger', u'Palau', u'Qatar', u'Samoa', u'Spain', u'Sudan', u'Syria', u'Tonga', u'Yemen', u'Lome', u'Adak', u'Atka', u'Nuuk', u'Knox', u'Lima', u'Nome', u'Asia', u'Aden', u'Baku', u'Dili', u'Gaza', u'Hovd', u'Omsk', u'Oral', u'Zulu', u'Kiev', u'Oslo', u'Bonn', u'Riga', u'Rome', u'Mahe', u'Apia', u'Fiji', u'Guam', u'Niue', u'Truk', u'Wake', u'Chad', u'Cuba', u'Fiji', u'Iran', u'Iraq', u'Mali', u'Oman', u'Peru', u'Togo', u'GMT', u'Yap']

And then I started to analyze the text I have:

text='I went to New York and New Orleans and London and United States, Canada, and Egypt, Saudi Arabia'
text_words=text.split()

and tried to investigate each word separately:

for i in rl(text_words):
    tw=text_words[i].lower()
    print i
    print tw
    print index_dict.get(tw)

The methods seems fast, but I'm not sure how to take it further

like image 307
hmghaly Avatar asked Aug 10 '12 13:08

hmghaly


1 Answers

whitespaces_list=['New York','United States', 'United Arab Emirates']
large_text="I went to New York on my way to North Carolina, and eventually will be going to United Arab Emirates."
for i in whitespaces_list:
    if i in large_text:
        print i," Exist in Large text"

are you looking for something like this ?

like image 182
Nagri Avatar answered Sep 21 '22 12:09

Nagri