Beispiel #1
0
 public static Reference newFromSnaks(Statement statement, Snak[] snaks)
 {
     Reference reference = new Reference(statement, new JsonObject());
     foreach (Snak snak in snaks)
     {
         reference.addSnak(snak);
     }
     statement.addReference(reference);
     return reference;
 }
Beispiel #2
0
 private WikiDataState TypeOfAdministrativeUnit(Item item, Entity entity, Boolean createStatement, Boolean overrideWrongData, out Statement statement)
 {
     var entityType = WikiBase.WikiDataItems[entity.type];
     var result = CheckPropertyValue(item, WikiBase.PropertyIdInstanceOf, entityType, createStatement, overrideWrongData, out statement);
     return result;
 }
Beispiel #3
0
        public void AddPopulationDataQualifiers(Statement statement, PopulationData data)
        {
            var pointInTimeQualifier = new Qualifier(statement, SnakType.Value, new EntityId(WikiBase.PropertyIdPointInTime), TimeValue.DateValue(data.referencedate));
            // statement.Qualifiers.Add(pointInTimeQualifier);  // already added by the constructor

            var method = String.Empty;
            switch ( data.source )
            {
                case PopulationDataSourceType.Census:
                    method = WikiBase.ItemCensuses;
                    break;

                case PopulationDataSourceType.DOPA:
                    method = WikiBase.ItemRegistration;
                    break;
            }
            if ( !String.IsNullOrEmpty(method) )
            {
                var methodQualifier = new Qualifier(statement, SnakType.Value, new EntityId(WikiBase.PropertyIdDeterminationMethod), new EntityIdValue(new EntityId(method)));
                // statement.Qualifiers.Add(methodQualifier);  // already added by the constructor
            }
        }
Beispiel #4
0
        private WikiDataState PopulationData(Item item, PopulationData data, Boolean createStatement, Boolean overrideWrongData, out Statement statement)
        {
            var total = data.data.FirstOrDefault(y => y.type == PopulationDataType.total);
            var propertyName = WikiBase.PropertyIdPopulation;

            WikiDataState result = WikiDataState.Unknown;

            // Statement claim = item.Claims.FirstOrDefault(x => x.IsAboutProperty(WikiBase.PropertyIdCountry)) as Statement;
            var property = new EntityId(propertyName);
            var propertyPointInTime = new EntityId(WikiBase.PropertyIdPointInTime);
            var claimsForProperty = item.Claims.Where(x => property.Equals(x.mainSnak.PropertyId));
            Statement claim = claimsForProperty.FirstOrDefault(
                x => x.Qualifiers.Any(
                    y => y.PropertyId.Equals(propertyPointInTime) &&
                         y.DataValue is TimeValue &&
                         (y.DataValue as TimeValue).DateTime.Year == data.Year)) as Statement;

            var dataValue = new QuantityValue(total.total);
            var snak = new Snak(SnakType.Value, new EntityId(propertyName), dataValue);
            if ( claim == null )
            {
                result = WikiDataState.NotSet;
                if ( createStatement )
                {
                    claim = item.createStatementForSnak(snak);
                }
            }
            else
            {
                Snak oldSnak = claim.mainSnak;
                var oldDataValue = snak.DataValue as QuantityValue;
                if ( oldDataValue.Equals(dataValue) )
                {
                    result = WikiDataState.Valid;
                }
                else
                {
                    result = WikiDataState.WrongValue;
                    if ( overrideWrongData )
                    {
                        claim.mainSnak = snak;
                    }
                }
            }

            statement = claim as Statement;
            return result;
        }
Beispiel #5
0
 private WikiDataState OpenStreetMap(Item item, Entity entity, Boolean createStatement, Boolean overrideWrongData, out Statement statement)
 {
     var stringValue = String.Empty;
     if ( entity.wiki.openstreetmapSpecified )
     {
         stringValue = entity.wiki.openstreetmap.ToString(CultureInfo.InvariantCulture);
     }
     return CheckStringValue(item, WikiBase.PropertyIdOpenStreetMap, stringValue, createStatement, overrideWrongData, out statement);
 }
Beispiel #6
0
 private WikiDataState LocatorMap(Item item, Entity entity, Boolean createStatement, Boolean overrideWrongData, out Statement statement)
 {
     var stringValue = String.Empty;
     stringValue = String.Format("Amphoe {0}.svg", entity.geocode);
     return CheckStringValue(item, WikiBase.PropertyIdLocationMap, stringValue, createStatement, overrideWrongData, out statement);
 }
Beispiel #7
0
 private WikiDataState IsInAdministrativeUnit(Item item, Entity entity, Boolean createStatement, Boolean overrideWrongData, out Statement statement)
 {
     Entity parentEntity = null;
     if ( entity.type == EntityType.Chumchon )
     {
         // Between Chumchon and Thesaban there is one level without numbers, at least so far
         parentEntity = _allEntities.FirstOrDefault(x => x.geocode == entity.geocode / 100);
         if ( parentEntity == null )
         {
             parentEntity = _allEntities.FirstOrDefault(x => x.geocode == entity.geocode / 10000);
         }
     }
     else if ( entity.type.IsLocalGovernment() )
     {
         var parentGeocode = entity.parent.FirstOrDefault();
         if ( parentGeocode == 0 )
         {
             parentGeocode = entity.geocode / 100;
         }
         parentEntity = _allEntities.FirstOrDefault(x => x.geocode == parentGeocode);
     }
     else
     {
         parentEntity = _allEntities.FirstOrDefault(x => x.geocode == entity.geocode / 100);
     }
     if ( (parentEntity != null) && (parentEntity.wiki != null) && (!String.IsNullOrEmpty(parentEntity.wiki.wikidata)) )
     {
         var parent = parentEntity.wiki.wikidata;
         return CheckPropertyValue(item, WikiBase.PropertyIdIsInAdministrativeUnit, parent, createStatement, overrideWrongData, out statement);
     }
     else
     {
         statement = null;
         return WikiDataState.Unknown;
     }
 }
Beispiel #8
0
        /// <summary>
        /// Checks and evetually sets a link property.
        /// </summary>
        /// <param name="item">Wikidata item.</param>
        /// <param name="propertyId">Id of the property.</param>
        /// <param name="expectedItemId">Expected item id.</param>
        /// <param name="createStatement"><c>true</c> to create the statement if not existing yet.</param>
        /// <param name="overrideWrongData"><c>true</c> to overwrite wrong data.</param>
        /// <param name="statement">Newly created statement.</param>
        /// <returns>Status of the link property.</returns>
        public WikiDataState CheckPropertyValue(Item item, String propertyId, String expectedItemId, Boolean createStatement, Boolean overrideWrongData, out Statement statement)
        {
            if ( item == null )
                throw new ArgumentNullException("item");

            WikiDataState result = WikiDataState.Unknown;

            // Statement claim = item.Claims.FirstOrDefault(x => x.IsAboutProperty(WikiBase.PropertyIdCountry)) as Statement;
            var property = new EntityId(propertyId);
            Statement claim = item.Claims.FirstOrDefault(x => property.Equals(x.mainSnak.PropertyId)) as Statement;

            var entity = new EntityId(expectedItemId);
            var dataValue = new EntityIdValue(entity);

            var snak = new Snak(SnakType.Value, new EntityId(propertyId), dataValue);
            if ( claim == null )
            {
                if ( String.IsNullOrEmpty(expectedItemId) )
                {
                    result = WikiDataState.Valid;
                }
                else
                {
                    result = WikiDataState.NotSet;
                    if ( createStatement )
                    {
                        claim = item.createStatementForSnak(snak);
                    }
                }
            }
            else
            {
                Snak oldSnak = claim.mainSnak;
                var oldDataValue = oldSnak.DataValue as EntityIdValue;
                if ( oldDataValue.NumericId == dataValue.NumericId )
                {
                    result = WikiDataState.Valid;
                }
                else
                {
                    if ( !String.IsNullOrEmpty(expectedItemId) )
                    {
                        result = WikiDataState.WrongValue;
                        if ( overrideWrongData )
                        {
                            claim.mainSnak = snak;
                        }
                    }
                    else
                    {
                        result = WikiDataState.DataMissing;
                    }
                }
            }

            statement = claim as Statement;
            return result;
        }
Beispiel #9
0
 private WikiDataState Gnd(Item item, Entity entity, Boolean createStatement, Boolean overrideWrongData, out Statement statement)
 {
     var stringValue = String.Empty;
     stringValue = entity.codes.gnd.value;
     return CheckStringValue(item, WikiBase.PropertyIdGND, stringValue, createStatement, overrideWrongData, out statement);
 }
Beispiel #10
0
        private WikiDataState Inception(Item item, Entity entity, Boolean createStatement, Boolean overrideWrongData, out Statement statement)
        {
            var history = entity.history.Items.FirstOrDefault(x => x is HistoryCreate) as HistoryCreate;
            var hasValue = history != null && history.effectiveSpecified;
            var timeValue = history.effective;

            return CheckTimeValue(item, WikiBase.PropertyIdInception, timeValue, createStatement, overrideWrongData, out statement);
        }
Beispiel #11
0
 private WikiDataState Geocode(Item item, Entity entity, Boolean createStatement, Boolean overrideWrongData, out Statement statement)
 {
     var stringValue = String.Empty;
     stringValue = entity.geocode.ToString(CultureInfo.InvariantCulture);
     return CheckStringValue(item, WikiBase.PropertyIdThaiGeocode, stringValue, createStatement, overrideWrongData, out statement);
 }
Beispiel #12
0
 private Boolean CreateDateQualifierAndReference(Statement statement, String propertyId, HistoryEntryBase entityHistory)
 {
     var result = false;
     var startDateQualifier = statement.Qualifiers.FirstOrDefault(x => x.PropertyId.PrefixedId == propertyId);
     if ( startDateQualifier == null )
     {
         startDateQualifier = new Qualifier(statement, SnakType.Value, new EntityId(propertyId), TimeValue.DateValue(entityHistory.effective));
         result = true;
     }
     var gazetteReference = entityHistory.Items.FirstOrDefault(x => x is GazetteRelated) as GazetteRelated;
     if ( gazetteReference != null )
     {
         GazetteEntry gazetteAnnouncement = GlobalData.AllGazetteAnnouncements.FindAnnouncement(gazetteReference);
         if ( gazetteAnnouncement != null )
         {
             var snak = new Snak(SnakType.Value, new EntityId(WikiBase.PropertyIdReferenceUrl), new StringValue(gazetteAnnouncement.DownloadUrl.AbsoluteUri));
             var startDateReference = statement.CreateReferenceForSnak(snak);
             result = true;
         }
     }
     return result;
 }
Beispiel #13
0
        private WikiDataState CheckStringMultiValue(Item item, String propertyId, String expectedValue, Boolean createStatement, out Statement statement)
        {
            if ( item == null )
                throw new ArgumentNullException("item");

            WikiDataState result = WikiDataState.Unknown;

            if ( String.IsNullOrEmpty(expectedValue) )
            {
                statement = null;
                return result;  // TODO better handling!
            }
            var dataValue = new StringValue(expectedValue);
            var snak = new Snak(SnakType.Value, new EntityId(propertyId), dataValue);

            var property = new EntityId(propertyId);
            Statement foundStatement = null;
            foreach ( var claim in item.Claims.Where(x => property.Equals(x.mainSnak.PropertyId)) )
            {
                Snak oldSnak = claim.mainSnak;
                var oldDataValue = oldSnak.DataValue as StringValue;
                if ( oldDataValue.Value == dataValue.Value )
                {
                    foundStatement = claim as Statement;
                    result = WikiDataState.Valid;
                }
            }

            if ( foundStatement == null )
            {
                if ( String.IsNullOrEmpty(expectedValue) )
                {
                    result = WikiDataState.Valid;
                }
                else
                {
                    result = WikiDataState.Incomplete;
                    if ( createStatement )
                    {
                        foundStatement = item.createStatementForSnak(snak);
                    }
                }
            }

            statement = foundStatement;
            return result;
        }
Beispiel #14
0
        public void AddPopulationDataReferences(Statement statement, PopulationData data, Entity entity)
        {
            Reference reference = null;
            Snak snak;
            switch ( data.source )
            {
                case PopulationDataSourceType.Census:
                    var statedInItem = String.Empty;
                    if ( WikiBase.ItemCensus.Keys.Contains(data.Year) )
                    {
                        statedInItem = WikiBase.ItemCensus[data.Year];
                    }
                    snak = new Snak(SnakType.Value, new EntityId(WikiBase.PropertyIdStatedIn), new EntityIdValue(new EntityId(statedInItem)));
                    reference = statement.CreateReferenceForSnak(snak);
                    foreach ( var refItem in data.reference )
                    {
                        var urlReference = refItem as MyUri;
                        if ( urlReference != null )
                        {
                            reference.AddSnak(new Snak(SnakType.Value, new EntityId(WikiBase.PropertyIdReferenceUrl), new StringValue(urlReference.Value)));
                        }
                    }
                    break;

                case PopulationDataSourceType.DOPA:
                    Uri source = PopulationDataDownloader.GetDisplayUrl(data.Year, entity.geocode);
                    snak = new Snak(SnakType.Value, new EntityId(WikiBase.PropertyIdReferenceUrl), new StringValue(source.AbsoluteUri));
                    reference = statement.CreateReferenceForSnak(snak);
                    reference.AddSnak(new Snak(SnakType.Value, new EntityId(WikiBase.PropertyIdPublisher), new EntityIdValue(new EntityId(WikiBase.ItemDopa))));
                    break;
            }

            if ( reference != null )
            {
                statement.AddReference(reference);
            }
        }
Beispiel #15
0
 private WikiDataState IsInCountry(Item item, Boolean createStatement, Boolean overrideWrongData, out Statement statement)
 {
     var parent = WikiBase.WikiDataItems[EntityType.Country];
     return CheckPropertyValue(item, WikiBase.PropertyIdCountry, parent, createStatement, overrideWrongData, out statement);
 }
Beispiel #16
0
        /// <summary>
        /// Adds the start and end date qualifiers to the type statement, and the corresponding Gazette announcement reference.
        /// </summary>
        /// <param name="statement">Statement with the entity type.</param>
        /// <param name="entityType">Entity type to be searched for.</param>
        /// <param name="entity">Entity.</param>
        /// <exception cref="ArgumentNullException"><paramref name="statement"/> or <paramref name="entity"/> is <c>null</c>.</exception>
        /// <returns><c>true</c> if qualifiers or references were added, <c>false</c> otherwise.</returns>
        public Boolean AddTypeOfAdministrativeQualifiersAndReferences(Statement statement, EntityType entityType, Entity entity)
        {
            if ( statement == null )
                throw new ArgumentNullException("statement");
            if ( entity == null )
                throw new ArgumentNullException("entity");
            var result = false;

            foreach ( var historyItem in entity.history.Items )
            {
                var entityHistoryCreate = historyItem as HistoryCreate;
                var entityStatusChange = historyItem as HistoryStatus;
                var entityAbolish = historyItem as HistoryAbolish;
                if ( entityHistoryCreate != null && entityHistoryCreate.type == entity.type && entityHistoryCreate.effectiveSpecified )
                {
                    // create as requested type
                    result |= CreateDateQualifierAndReference(statement, WikiBase.PropertyIdStartDate, entityHistoryCreate);
                }
                if ( entityStatusChange != null && entityStatusChange.@new == entity.type && entityStatusChange.effectiveSpecified )
                {
                    // change to requested type
                    result |= CreateDateQualifierAndReference(statement, WikiBase.PropertyIdStartDate, entityStatusChange);
                }
                if ( entityStatusChange != null && entityStatusChange.old == entity.type && entityStatusChange.effectiveSpecified )
                {
                    // change from requested type
                    result |= CreateDateQualifierAndReference(statement, WikiBase.PropertyIdEndDate, entityStatusChange);
                }
                if ( entityAbolish != null && entityAbolish.type == entity.type && entityAbolish.effectiveSpecified )
                {
                    // abolish as requested type
                    result |= CreateDateQualifierAndReference(statement, WikiBase.PropertyIdEndDate, entityAbolish);
                }
            }
            return result;
        }
Beispiel #17
0
 private WikiDataState Location(Item item, Entity entity, Boolean createStatement, Boolean overrideWrongData, out Statement statement)
 {
     Point value = null;
     var office = entity.office.FirstOrDefault();
     if ( office != null )
     {
         value = office.Point;
     }
     if ( value != null )
     {
         return CheckCoordinateValue(item, WikiBase.PropertyIdCoordinate, value, createStatement, overrideWrongData, out statement);
     }
     else
     {
         statement = null;
         return WikiDataState.Unknown;
     }
 }
Beispiel #18
0
        private WikiDataState CheckCoordinateValue(Item item, String propertyId, Point expected, Boolean createStatement, Boolean overrideWrongData, out Statement statement)
        {
            if ( item == null )
                throw new ArgumentNullException("item");

            WikiDataState result = WikiDataState.Unknown;

            // Statement claim = item.Claims.FirstOrDefault(x => x.IsAboutProperty(WikiBase.PropertyIdCountry)) as Statement;
            var property = new EntityId(propertyId);
            Statement claim = item.Claims.FirstOrDefault(x => property.Equals(x.mainSnak.PropertyId)) as Statement;

            var dataValue = new GlobeCoordinateValue(Convert.ToDouble(expected.lat), Convert.ToDouble(expected.@long), 0.0001, Globe.Earth);
            var snak = new Snak(SnakType.Value, new EntityId(propertyId), dataValue);
            if ( claim == null )
            {
                result = WikiDataState.NotSet;
                if ( createStatement )
                {
                    claim = item.createStatementForSnak(snak);
                }
            }
            else
            {
                Snak oldSnak = claim.mainSnak;
                var oldDataValue = oldSnak.DataValue as GlobeCoordinateValue;
                if ( (Math.Abs(oldDataValue.Latitude - dataValue.Latitude) < dataValue.Precision) &&
                    (Math.Abs(oldDataValue.Longitude - dataValue.Longitude) < dataValue.Precision) )
                {
                    result = WikiDataState.Valid;
                }
                else
                {
                    result = WikiDataState.WrongValue;
                    if ( overrideWrongData )
                    {
                        claim.mainSnak = snak;
                    }
                }
            }

            statement = claim as Statement;
            return result;
        }
Beispiel #19
0
 internal Reference(Statement statement, JsonObject data)
 {
     this.statement = statement;
     this.fillData(data);
 }