Igor
Igor

Reputation: 2969

Texts intersection in Python

I have the list of 'cities':

US,Upper St. Clair
US,VCT,Victoria,Viktorija,bigtolia,bikutoria,fyktwrya  tksas,wei duo li ya,wyktwrya  tgzas,Виктория,Викторија,فيكتوريا، تكساس,ویکتوریا، تگزاس,ビクトリア,維多利亞,빅토리아
US,VCV,Victorville,Viktorvil,Vikturvil,bhiktarabhila,bigteobil,fyktwrfyly  san byrnardynw  kalyfwrnya,vu~ikutavu~iru,wei ke duo wei er,wyktwr wyl  kalyfrnya,Викторвил,Виктървил,فيكتورفيلي، سان بيرناردينو، كاليفورنيا,وکٹورولے,ویکتور ویل، کالیفرنیا,भिक्टरभिल,ヴィクターヴィル,维克多维尔,빅터빌
US,VIS,Vajsejlija,Vajselija,Visalia,Visejlija,baiseiria,bhisaliya,fysalya  kalyfwrnya,wei sai li ya,wysalya,wysalya  kalyfrnya,Вайселия,Вајсејлија,Висейлия,فيساليا، كاليفورنيا,ویسالیا,ویسالیا، کالیفرنیا,भिसालिया,バイセイリア,維塞利亞
US,VKS,Vicksburgum,Viksberg,Viksburg,bigseubeogeu,vu~ikkusubagu,wei ke si bao,wyksbrg  mysysypy,Виксберг,Виксбург,Виксбърг,ویکسبرگ، میسیسیپی,ヴィックスバーグ,维克斯堡,빅스버그
US,VLD,Valdosta,Валдоста
US,VLO,Val'ekho,Valejo,Valekho,Vallejo,balleio,bhyalejo,falyjw  kalyfwrnya,vu~areho,walhw  kalyfrnya,walyjw,wei li he,Валехо,Валејо,Вальехо,فاليجو، كاليفورنيا,والهو، کالیفرنیا,والیجو

And there is also a list of sentences which can contain any city name.

The question is how can I 'intersect' these two entities in order to have in the end an array like 'this particular text' has 'this particular city inside'?

Python 2.7

Thank you.

Update: The output may be a dictionary like:

{
   'Los-Angeles': 'something is going on in Los Angeles',
   'Washington': 'something is happenes in Washington!',
   ...
}

Upvotes: 0

Views: 84

Answers (2)

brunsgaard
brunsgaard

Reputation: 5168

You could do it like this!

from collections import defaultdict
from itertools import product
from pprint import pprint

cities = [
    'Copenhagen',
    'Berlin',
    'Stockholm'   
]

sentences = [
    'Hello world',
    'Hello Copenhagen',
    'Hello Berlin',
    'Bye Berlin',
]

res = defaultdict(list)

for city, sentence in product(cities, sentences):
    if city in sentence:
        res[city].append(sentence)

pprint(res)

result in the output:

~ python test.py 
{'Berlin': ['Hello Berlin', 'Bye Berlin'],
 'Copenhagen': ['Hello Copenhagen']}

Upvotes: 1

Daniel
Daniel

Reputation: 42778

So you have to first split the cities to their aliases, and then search your text for them:

all_cities = {}
with open('cities') as cities:
    for city in cities:
        city = city.strip()
        parts = city.split(',')
        all_cities.update((part, parts[:2]) for part in parts[1:]) # skip the "US"

for city in all_cities:
    if city in some_text:
        print all_cities[city]

Upvotes: 1

Related Questions