Skip to content

Commit 53e40df

Browse files
committed
2 parents 1907f00 + 652f577 commit 53e40df

5 files changed

Lines changed: 130 additions & 124 deletions

File tree

migration/ormigrate/EventLocationContext.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ def generateORLocationPages(self, events:list, overwrite:bool=False, limit:int=N
5252
# Generate country location pages
5353
self.generateLocationPages(self.locationContext.countries, overwrite)
5454
# Fix locations of the event → Normalized location names afterwards
55-
self.locationFixer.fixEvents(events)
55+
for event in events:
56+
self.locationFixer.fixEventRecord(event.__dict__)
5657

5758
cityCounter=EventLocationContext.getFieldCounter(events, 'city', 'City')
5859
usedCities=[]

migration/ormigrate/issue166_cfp.py

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from wikifile.wikiFile import WikiFile
1313
from smw.pagefixer import PageFixerManager
1414
from ormigrate.fixer import ORFixer
15+
from smw.rating import EntityRating
1516

1617

1718
class WikiCFPIDFixer(ORFixer):
@@ -73,46 +74,34 @@ def getWikiCFPIdFromPage(self, eventWikiText):
7374
return wikicfpid
7475
return None
7576

76-
def fixEventFileFromWiki(self,pageTitle):
77+
def fix(self, rating:EntityRating):
7778
"""
78-
Get the pageTitle from the wiki directly and run the fixer on it
79+
Get the entityRating object and apply the fixer on it.
7980
8081
Args:
81-
pageTitle(str): page title of a wiki page
82-
83-
Returns:
84-
wikiFile(WikiFile): A WikiFile object if fixer is applied, None otherwise
82+
entityRating(EntityRating): EntityRating object of the Event
8583
"""
86-
wikiFileManager = self.wikiFileManager
87-
wikiFile = wikiFileManager.getWikiFile(pageTitle)
88-
event = str(wikiFile.wikiText)
89-
wikicfpid= self.getWikiCFPIdFromPage(event)
84+
wikiFile = rating.wikiFile
85+
eventRecord = rating.getRecord()
86+
wikiText = str(wikiFile.wikiText)
87+
wikicfpid= self.getWikiCFPIdFromPage(wikiText)
9088
if wikicfpid is not None:
91-
values = {}
92-
values['wikicfpId'] = wikicfpid
93-
wikiFile.add_template('Event',values)
94-
return wikiFile
95-
return None
89+
eventRecord['wikicfpId'] = wikicfpid
9690

97-
def fixEventFile(self,path,event):
91+
def rate(self, rating: EntityRating):
9892
"""
99-
Get the path and content of .wiki file and run the fixer on it
100-
Args:
101-
path(str): path of .wiki file
102-
event(str): content of .wiki file
103-
Returns:
104-
wikiFile(WikiFile): A WikiFile object if fixer is applied, None otherwise
93+
Rate the rating object as per the issue 166
94+
Args:
95+
rating(EntityRating): EntityRating object of the Event
10596
"""
106-
filename = ntpath.basename(path).replace('.wiki','')
107-
wikiFilePath = ntpath.dirname(path)
108-
wikicfpid= self.getWikiCFPIdFromPage(event)
109-
if wikicfpid is not None:
110-
wikiFile = WikiFile(filename,wikiFilePath)
111-
values = {}
112-
values['wikicfpId']=wikicfpid
113-
wikiFile.add_template('Event',values)
114-
return wikiFile
115-
return None
97+
wikiFile= rating.wikiFile
98+
eventWikiText = str(wikiFile.wikiText)
99+
wikiCFPId = self.getWikiCFPIdFromPage(eventWikiText)
100+
if wikiCFPId is None:
101+
rating.set(1, RatingType.ok, "no legacy wikiCFP import id found")
102+
else:
103+
rating.set(5, RatingType.invalid, f"legacy wikiCFP reference {wikiCFPId} found")
104+
return rating
116105

117106

118107
def fixPageWithDBCrosscheck(self, wikiText, wikicfpid):
@@ -143,7 +132,9 @@ def fixPageWithDBCrosscheck(self, wikiText, wikicfpid):
143132
# print('Title or acronym not found')
144133
# print(dic)
145134
# print(orEvent)
146-
135+
136+
137+
#TODO Change function when architecture is implemented.
147138
def getRatingFromWikiFile(self,wikiFile:WikiFile)->PageRating:
148139
'''
149140
Args:

migration/ormigrate/issue220_location.py

Lines changed: 29 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
from geograpy.locator import LocationContext, Location, City, Country, Region
2-
from openresearch.event import Event
32
from smw.pagefixer import PageFixerManager
43
from ormigrate.fixer import ORFixer
5-
from smw.rating import Rating, RatingType
4+
from smw.rating import Rating, RatingType, EntityRating
65

76

87
class LocationFixer(ORFixer):
@@ -23,41 +22,21 @@ def __init__(self,pageFixerManager):
2322
'''
2423
super(LocationFixer, self).__init__(pageFixerManager)
2524
LocationFixer.locationContext=self.getORLocationContext()
26-
27-
def fixEventRecords(self, events:list):
28-
"""
29-
Gets list of dicts (list of events) and tries to fix the location entries
30-
"""
31-
count=0
32-
stats={}
33-
for event_unfixed in events:
34-
event, errors = self.fixEventRecord(event_unfixed)
35-
print(errors)
36-
if errors is not None:
37-
for error in errors.keys():
38-
if error in stats:
39-
stats[error]+=1
40-
else:
41-
stats[error]=1
42-
print(stats)
4325

44-
def fixEvents(self, events:list):
26+
def fix(self,rating:EntityRating):
4527
'''
46-
fixes the location of the given events
47-
48-
Args:
49-
events(list): list of Event objects that should be fixed
28+
tries fixing the location entries of the given entity
5029
'''
51-
for event in events:
52-
self.fixEvent(event)
30+
eventRecord = rating.getRecord()
31+
self.fixEventRecord(eventRecord)
5332

54-
def fixEvent(self, event:Event):
33+
def fixEventRecord(self, event:dict, errors:dict=None, bestFit=True):
5534
'''
56-
fixes the location of the given event
35+
Args:
36+
event(dict): event records containing the location values that should be fixed
37+
errors(dict): dictonary containing the errors of the given event record → new errors are added to the dict
38+
bestFit(bool): If true the best/closed fit for a location is chosen (e.g. city with highest population). Otherwise a fix is only applied if the location can be identified with certainty.
5739
'''
58-
self.fixEventRecord(event.__dict__)
59-
60-
def fixEventRecord(self, event:dict, errors=None):
6140
# event location values
6241
if errors is None:
6342
errors = {}
@@ -115,21 +94,22 @@ def fixEventRecord(self, event:dict, errors=None):
11594
cities = list(filter(
11695
lambda x: 'wikidataid' in x.country.__dict__ and x.country.wikidataid in countryids,
11796
cities))
118-
if len(cities) == 1:
97+
if len(cities) >0:
11998
# city can be identified (Could still be incorrect)
120-
final_city=cities[0]
99+
if len(cities) > 1:
100+
errors["city_unclear"] = f"City '{event_city}' matches against multiple cities in the LocationCorpus. Other location information are not sufficient enough to clearly identify the city"
101+
cities=sorted(cities,key=lambda city:0 if 'population'not in city.__dict__ or city.population is None else int(city.population), reverse=True)
102+
final_city = cities[0]
121103
if isinstance(final_city,City):
122104
event[self.CITY]=self.getPageTitle(final_city)
123105
event[self.REGION] = self.getPageTitle(final_city.region)
124106
event[self.COUNTRY] = self.getPageTitle(final_city.country)
125107
errors["complete"]="Location of event complete"
126108
return event, errors
127-
elif len(cities) == 0:
109+
else:
128110
# No matching city -> Two possibilities: event location information incorrect or locations missing in LocationCorpus
129111
errors["city_unknown"]=f"City '{event_city}' could not be matched against a city in the LocationCorpus with the given region and country. Either location information are incorrect or location is missing in the corpus."
130112
#return None, errors
131-
else:
132-
errors["city_unclear"]=f"City '{event_city}' matches against multiple cities in the LocationCorpus. Other location information are not sufficient enough to clearly identify the city"
133113
else:
134114
# event_city is not the LocationCorpus
135115
isPossiblyMisplaced=True
@@ -314,11 +294,17 @@ def getPageTitle(location:Location):
314294
pageTitle=location.name
315295
return pageTitle
316296

317-
@classmethod
318-
def getRating(cls, eventRecord):
297+
298+
def rate(self, rating:EntityRating):
319299
'''
320300
get the pain Rating for the given eventRecord
321301
'''
302+
eventRecord=rating.getRecord()
303+
arating=self.getRating(eventRecord)
304+
rating.set(arating.pain, arating.reason, arating.hint)
305+
306+
@classmethod
307+
def getRating(cls, eventRecord):
322308
painrating = None
323309
city = None
324310
region = None
@@ -329,24 +315,24 @@ def getRating(cls, eventRecord):
329315
if cls.COUNTRY in eventRecord: country = eventRecord[cls.COUNTRY]
330316
if not city and not region and not country:
331317
# location is not defined
332-
painrating = Rating(7, RatingType.missing,f'Locations are not defined')
318+
painrating=Rating(7, RatingType.missing,f'Locations are not defined')
333319
else:
334320
if 'locationContext' in cls.__dict__:
335321
cities=cls.__dict__['locationContext'].getCities(city)
336322
regions=cls.__dict__['locationContext'].getCities(region)
337323
countries=cls.__dict__['locationContext'].getCities(country)
338324
if cities and regions and countries:
339325
# all locations are recognized
340-
painrating= Rating(1,RatingType.ok,f'Locations are valid. (Country: {country}, Region: {region}, City:{city})')
326+
painrating=Rating(1,RatingType.ok,f'Locations are valid. (Country: {country}, Region: {region}, City:{city})')
341327
elif not cities:
342328
# City is not valid
343-
painrating = Rating(6, RatingType.invalid,f'City is not recognized. (City:{city})')
329+
painrating=Rating(6, RatingType.invalid,f'City is not recognized. (City:{city})')
344330
elif not regions:
345331
# City is valid but region is not
346-
painrating = Rating(5, RatingType.invalid,f'Region is not recognized. (Country: {country}, Region: {region}, City:{city})')
332+
painrating=Rating(5, RatingType.invalid,f'Region is not recognized. (Country: {country}, Region: {region}, City:{city})')
347333
else:
348334
# City and region are valid but country is not
349-
painrating = Rating(3, RatingType.invalid,f'Country is not recognized. (Country: {country}, Region: {region}, City:{city})')
335+
painrating=Rating(3, RatingType.invalid,f'Country is not recognized. (Country: {country}, Region: {region}, City:{city})')
350336
return painrating
351337

352338
@staticmethod

migration/tests/testIssue166_WikiCfpID.py

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
from ormigrate.issue166_cfp import WikiCFPIDFixer
88
from openresearch.event import Event
99
from tests.pagefixtoolbox import PageFixerTest
10+
from wikifile.wikiFileManager import WikiFileManager
11+
from wikifile.wikiFile import WikiFile
12+
from smw.rating import EntityRating
1013

1114
class TestWikiCFPId(PageFixerTest):
1215
'''
@@ -20,32 +23,52 @@ def setUp(self):
2023

2124
def testIssue166Examples(self):
2225
"""
23-
Tests the issue 166 for addition of WikiCFP-ID to applicable pages
26+
Tests the issue 166 for addition of WikiCFP-ID to applicable pages.
27+
Testing the fix function
28+
2429
"""
2530
fixer=self.getPageFixer()
2631
samplesWikiText = Event.getSampleWikiTextList()
2732
wikicfpid= fixer.getWikiCFPIdFromPage(samplesWikiText[1])
2833
self.assertIsNotNone(wikicfpid)
2934
self.assertEqual(wikicfpid,'3845')
3035

31-
32-
samplesDict=Event.getSamples()
33-
count=0
34-
for sample in samplesDict:
35-
wikiFile= fixer.fixEventFileFromWiki(sample['pageTitle'])
36-
if wikiFile is not None:
37-
count+=1
38-
self.assertGreaterEqual(count,1)
36+
wikiFile = WikiFile('sampleFile',None,samplesWikiText)
37+
event = Event()
38+
event.wikiFile = wikiFile
39+
entityRating = EntityRating(event)
40+
entityRating.pageTitle='Test'
41+
entityRating.templateName= 'Event'
42+
43+
# Get Fixer
44+
fixer = self.getPageFixer()
45+
46+
# Rate With Fixer
47+
fixer.rate(entityRating)
48+
if self.debug:
49+
print(entityRating)
50+
self.assertEqual(entityRating.pain, 5)
51+
52+
# Fix With Fixer
53+
fixer.fix(entityRating)
54+
self.assertEqual(entityRating.entity.wikicfpId,"3845")
3955

4056
# TODO
41-
if fixer.databaseAvailable():
42-
fixedPage= fixer.fixPageWithDBCrosscheck('test', samplesWikiText[1], wikicfpid)
43-
if self.debug:
44-
print(fixedPage)
45-
fixedDict=fixedPage.extract_template('Event')
46-
self.assertIsNotNone(fixedDict['wikicfpId'])
47-
self.assertEqual(fixedDict['wikicfpId'],'3845')
48-
57+
# if fixer.databaseAvailable():
58+
# fixedPage= fixer.fixPageWithDBCrosscheck(samplesWikiText[1], wikicfpid)
59+
# if self.debug:
60+
# print(fixedPage)
61+
# fixedDict=fixedPage.extract_template('Event')
62+
# self.assertIsNotNone(fixedDict['wikicfpId'])
63+
# self.assertEqual(fixedDict['wikicfpId'],'3845')
64+
65+
def testIssue166Rating(self):
66+
"""
67+
test the Rating function of the fixer
68+
"""
69+
70+
71+
# TODO Change function when architecture is implemented.
4972
def testIssue166(self):
5073
'''
5174
test the wikicfpID handling

0 commit comments

Comments
 (0)