问:
Data Modeling: Logical Modeling Exercise
In trying to learn the art of data storage I have been trying to take in as much solid information as possible.  PerformanceDBA posted some really helpful tutorials/examples in the following posts among others: is my data normalized? and Relational table naming convention.  I already asked a subset question of this model here.

So to make sure I understood the concepts he presented and I have seen elsewhere I wanted to take things a step or two further and see if I am grasping the concepts.  Hence the purpose of this post, which hopefully others can also learn from.  Everything I present is conceptual to me and for learning rather than applying it in some production system.  It would be cool to get some input from PerformanceDBA also since I used his models to get started, but I appreciate all input given from anyone.

As I am new to databases and especially modeling I will be the first to admit that I may not always ask the right questions, explain my thoughts clearly, or use the right verbage due to lack of expertise on the subject.  So please keep that in mind and feel free to steer me in the right direction if I head off track.

If there is enough interest in this I would like to take this from the logical to physical phases to show the evolution of the process and share it here on Stack.  I will keep this thread for the Logical Diagram though and start new one for the additional steps.  For my understanding I will be building a MySQL DB in the end to run some tests and see if what I came up with actually works.

Here is the list of things that I want to capture in this conceptual model. Edit for V1.2

The purpose of this is to list Bands, their members, and the Events that they will be appearing at, as well as offer music and other merchandise for sale

Members will be able to match up with friends

Members can write reviews on the Bands, their music, and their events.

There can only be one review per member on a given item, although they can edit their reviews and history will be maintained.

BandMembers will have the chance to write a single Comment on Reviews about the Band they are associated with. Collectively as a Band only one Comment is allowed per Review.

Members can then rate all Reviews and Comments but only once per given instance

Members can select their favorite Bands, music, Merchandise, and Events

Bands, Songs, and Events will be categorized into the type of Genre that they are and then further subcategorized into a SubGenre if necessary. It is ok for a Band or Event to fall into more then one Genre/SubGenre combination.

Event date, time, and location will be posted for a given band and members can show that they will be attending the Event. An Event can be comprised of more than one Band, and multiple Events can take place at a single location on the same day

Every party will be tied to at least one address and address history shall be maintained. Each party could also be tied to more then one address at a time (i.e. billing, shipping, physical)

There will be stored profiles for Bands, BandMembers, and general members.

So there it is, maybe a bit involved but could be a great learning tool for many hopefully as the process evolves and input is given by the community. Any input?

EDIT v1.1
In response to PerformanceDBA

U.3) That means no merchandise other than Band merchandise in the database. Correct ?
That was my original thought but you got me thinking.  Maybe the site would want to sell its own merchandise or even other merchandise from the bands.  Not sure a mod to make for that. Would it require an entire rework of the Catalog section or just the identifying relationship that exists with the Band?
Attempted a mod to sell both complete albums or song.  Either way they would both be in electronic format only available for download.  That is why I listed an Album as being comprised of Songs rather then 2 separate entities.

U.5) I understand what you bring up about the circular relation with Favorite.  I would like to get to this “It is either one Entity with some form of differentiation (FavoriteType) which identifies its treatment” but how to is not clear to me.  What am I missing here?

u.6) “Business Rules This is probably the only area you are weak in.”

Thanks for the honest response.  I will readdress these but I hope to clear up some confusion in my head first with the responses I have posted back to you.

Q.1) Yes I would like to have Accepted, Rejected, and Blocked.  I am not sure what you are referring to as to how this would change the logical model?

Q.2) A person does not have to be a User.  They can exist only as a BandMember.  Is that what you are asking?

Minor Issue

Zero, One, or More…Oops I admit I forgot to give this attention when building the model.  I am submitting this version as is and will address in a future version.  I need to read up more on Constraint Checking to make sure I am understanding things.

M.4) Depends if you envision OrderPurchase in the future.
Can you expand as to what you mean here?

EDIT V1.2
In response to PerformanceDBA input...

Lessons learned.

I was mixing the concept of Identifying / Non-Identifying and Cardinality (i.e. Genre / SubGenre), and doing so inconsistently to make things worse.

Associative Tables are not required in Logical Diagrams as their many-to-many relationships can be depicted and then expanded in the Physical Model.

I was overlooking the Cardinality in a lot of the relationships

The importance of reading through relationships using effective Verb Phrases to reassure I am modeling what I want to accomplish.

U.2)  In the concept of this model it is only required to track a Venue as a location for an Event.  No further data needs to be collected.  With that being said Events will take place on a given EventDate and will be hosted at a Venue.  Venues will host multiple events and possibly multiple events on a given date.  In my new model my thinking was that EventDate is already tied to Event .  Therefore, Venue will not need a relationship with EventDate.  The 5th and 6th bullets you have listed under U.2) leave me questioning my thinking though.  Am I missing something here?

U.3) Is it time to move the link between Item and Band up to Item and Party instead?  With the current design I don't see a possibility to sell merchandise not tied to the band as you have brought up.

U.5) I left as per your input rather than making it a discrete Supertype/Subtype Relationship as I don’t see a benefit of having that type of roll up.

Additional Revisions

AR.1) After going through the exercise for FavoriteItem, I feel that Item to Review requires a many-to-many relationship so that is indicated.  Necessary?

Ok here we go for v1.3

I took a few days on this version, going back and forth with my design.  Once the logical process is complete, as I want to see if I am on the right track, I will go through in depth what I had learned and the troubles I faced as a beginner going through this process.  The big point for this version was it took throwing in some Keys to help see what I was missing in the past.  Going through the process of doing a matrix proved to be of great help also.  Regardless of anything, if it wasn't for the input given by PerformanceDBA I would still be a lost soul wondering in the dark.  Who knows my current design might reaffirm that I still am, but I have learned a lot so I am know I at least have a flashlight in my hand.

At this point in time I admit that I am still confused about identifying and non-identifying relationships.  In my model I had to use non-identifying relationships with non nulls just to join the relationships I wanted to model. In reading a lot on the subject there seems to be a lot of disagreement and indecisiveness on the subject so I did what I thought represented the right things in my model.  When to force (identifying) and when to be free (non-identifying)? Anyone have inputs?

EDIT V1.4

Ok took the V1.3 inputs and cleaned things up for this V1.4

Currently working on a V1.5 to include attributes.

EDIT V1.6

Okay, it has been some time since I have posted on here but the work on this project is still ongoing.  I am posting V1.6 now which includes a number of changes from the last posting of V1.4.  This version shows the further evolution of the Keys.  It still does not include the attributes or any AK's or IE's.  I have started working on the physical model and used that to help work through the attributes and to try and shed some light on the problems I am having with defining the AK's and IE's.  The next posting of the Logical Model will include these keys and the attributes.

答:
Method

I will cover specifics, but I will cover one or two Subject Areas completely, not all.  You can pick that up and apply it to all subject Areas.

I have not responded to the core Subject Area, because we are still dealing with Identifying Entities.  When that is resolved the Reviews, etc will be easier; the Transaction Entities are Dependent on the Identifying Entities.

Direction

D.1) I know that I stated that I need to see the whole model.  There is one exception.  Historic or Temporal or Audit data (eg. the Edit and stored versions).  At this early stage, they can be set aside; to be implemented just before completion of the Logical Model.  This is in recognition that (a) they are simple Dependents of some parent (b) the parents need to be modelled in relation to all other tables first, and (c) to exclude unnecessary complications, and thus allow us to concentrate on the relevant field.

in particular, you can ignore the tense in the Verb Phrases (every location of a version table would otherwise require Has/Had). Stay with present tense for now, because the focus is modelling, not archiving.

Unresolved

U.1) Optional Parent

That is completely disallowed. Not just by IDEF1X, but by any notion of Integrity.  If the FK Reference is defined, then there must be a Parent.  To allow optional parents, the FK Reference must be removed (or not implemented). Such a condition would exclude the result from qualfying as a "Relational database", by definition.  Eg. Address:Order.

Of course, in developed countries, an Order must have an Address for legal or taxation reasons; that is separate to the Standard requirement issue.

.

U.2) Event

Party::PartyAddress is correct; Address::PartyAdress is correct. Event::Address needs work.  Address is an Identifying Reference table; if used, it would be the parent, Event would be the child.  I leave it to you to identify/model multiple Events to a location, and Events at one or multiple locations.

There may be a Venue involved. Or a EventOccurrence

But if it is a generic Event which happens at multiple locations, that does not need an Entity, the Address is already in Order.

U.3) Assuming Catalog is an entry in the traditional sense (JCPenney 2011), a list of items for sale or hire.

OrderSaleItem is correct

Critical point. Catalog is Dependent, and can exist only in the context of a Band, as an Assset.  Fine. That means no merchandise other than Band merchandise in the database. Correct ?

I can see how "Evening performance with the Blues Brothers" is an Event that can be ordered, invoiced, and paid. Also reviewed, commented, etc.

I can't see how Song fits into that. Are the bands selling albums, songs, or both ?

Is there no other Band merchandise: concert/event souvenirs; poster; engraved shot glasses ?

Consistent with the naming conventions that you reference, and the rest of the database, Catalog (the cotent) should be named Item (the row).  You have already (naturally ?) used that in OrderSaleItem,( as opposed to OrderSaleCatalog.

U.4) Genre

No problem with an Item is classified by one-to-many Genres.

I think additionally a Genre classifies one-to-many Items.  The Relation is one-to-many (which will be resolved as an Associative table when we get to the Physical).

U.5) Favorite

The Cardinality of Item::Favorite is reversed.  When you correct that, the Favorite Subject Area will require further modelling.

Circular relation or dual paths between the same pair of Entities is a signal of an unresolved model.  Generally one is correct and the other is redundant.  (There are exceptions, but not here; and when this happens the Verb Phrases differentiate them.)

Either Band::Favorite xor Item::Favorite is correct, not both.

Item::Favorite seems to be correct, because Band is already identified in Item

Likewise, one Favorite Entity for bands and merchandise does not sound solid.  Every Identifier in the single Favorite Entity is a Party.  It would break when we Normalise, might as well demand that the Identifiers be clarified at this stage.  It is either one Entity with some form of differentiation (FavoriteType) which identifies its treatment; or one Favorite for bands and another for merchandise, in which case differentiation is not required, ambiguity is eliminated.

U.6) Business Rules
This is probably the only area you are weak in.  General response. You have done the tasks separately (all the modelling vs writing BRs).  These do not match the model. When you go through the next cycle, take the Business Rules as directives, and modulate them at the same time, as with the Entities, the Relations, and the Verb Phrases.

Question

Q.1) User/Friend

You have the essence of it perfectly. And the Cardinality of the Relations. (Full treatment on this one.) That is correct for Accepted Friend.

therefore the tense should be past (go with the majority rows)

Requested, and pending Accepted, are the minority.  Easily implemented in a IsAccepted Bit or Boolean.

Later you may have IsRejected or IsBlocked (that latter should be a separate Entity).

Is that what you require ?

Q.2) What is the basis on which a Person is zero-to-many Users ?

Minor Issue

M.1) Singular only.

M.2) Party Has zero-to-many Addresses.  I would think they must have one, in order to transact business (but perhaps not for all Users).

M.3) Order May Have zero-to-many Payments. "Requires" means that first Payment has to be inserted at the same time as Order.

Likewise, for any mandatory children (one-to-many as opposed to zero-to-many) that first child must be inserted at the same time as the parent.  This is done via Transactions in enterprise databases, because Immediate Constraint Checking (not Deferred) is implemented; and the small end of town fight over silly things like Deferred Constraint Checking is "better" and then spend half their life figuring out how not to get caught in the infinite loops they created, which trap them. MySQL does not have any at all, so nothing to worry about for this implementation.

M.4) OrderSaleItem shoulld be OrderItem xor Order should be OrderSale.  Depends if you envision OrderPurchase in the future.

Subject Area Example

Readers who are unfamiliar with the Standard for Modelling Relational Databases may find IDEF1X Notation useful.

As stated, I am not providing a finished Data Model, only guidance.  This is just one progression of one selected Subject Area.  It is not "right" or complete in any way.

Your Verb Phrases are excellent. I have provided alternatives for you to consider, they are not "right" or "better". You need to choose an progress them or your own. The goal being the most concise and accurate VP in each case.

No suggestion that Person is correct and User is incorrect, that is pending your answer.  But I had to use something in the model; since you have modelled them as separate, a counterpoint may be interesting to evaluate.

So go ahead and progress the model, then post again (just edit the question, leaving the header paras, and replacing the rest).

V1.1 and Response

That is certainly a progression.

I have re-numbered the items in pseudo-legal format, including the section headings, so that we can keep the numbering throughout, and keep adding to it.  Actually it really eases the SO editing problems as well.

U.3) Would it require an entire rework of the Catalog section or just the identifying relationship that exists with the Band?

No. That's the great thing about working at this level, the decisions you make here will be the railroad tracks that the data runs on, as freight, or does not run on (and thus needs alternate transport and heavy lifting to derive, in the form of masses of code or an additional data warehouse). And the decisions here are cheap (modelling time, paper).

Right now an Item exists only in the context of a Band. It is Dependent.  To allow non-band merchandise, it needs to be Independent. And then the existing super/subtype cluster needs rework.

Attempted a mod to sell both complete albums or song. Either way they would both be in electronic format only available for download. That is why I listed an Album as being comprised of Songs

OK. But now you can only sell albums, not songs.

rather then 2 separate entities.

Not sure what you mean (you have two separate entities).

It appears you have not seen my Subject Area Example.  Note that if you open it now, it contains bits that I have added V1.1; I have not changed what was there yesterday, the V1.0 response.

Actually that means you should go through my V1.0 Answer again, while viewing the Example.

U.5) ... but how to is not clear to me. What am I missing here?

An example of one Entity with differentiation is any of the Supertype/Subtype clusters you have.  The Favorite is the Supertype, BandFavourite and ItemFavourite are subtypes; allowing each to reference to Band xor Item respectively.

You have modelled ItemFavourite.  Now the question is, does the fact of a ItemFavourite imply that the Band is Favourite; or is BandFavourite a discrete fact ?  In the example, I have modelled the latter, without the Favourite::ItemFavourite/BandFavourite structure.

Q.1) Yes I would like to have Accepted, Rejected, and Blocked. I am not sure what you are referring to as to how this would change the logical model?

No change (I already stated it was pretty complete) to V1.0, but you might need an additional Entity.

You need three Bit or Boolean indicators in Friend. That will service these statuses:

Requested (but not Accepted)

Requested & Accepted

.

But Blocked is not a Friend (or could have been a Friend previously, but not since being Blocked). So either the Entity name has to change to reflect that (no change to the two Relations) xor Blocked has to be a separate Entity. Two separate meanings for the second Relation leads to complexity, therefore I would go with the latter.

With the former, we have additional statuses:

Blocked

.

Then the Verb Phrases need change (and I will include the RoleName for clarity), and one of them has a alternate meaning.
.

(It will be much more clear at the Attribute level Model, that's why we model in pictures, not words; so I have included it.)

Q.2) A person does not have to be a User. They can exist only as a BandMember. Is that what you are asking?

No. Why do we need to differentiate Person and User ? What are the separate actions or attributes ?  Thus far, I see Person and User as the same Entity; Person is an User with no activity.

This is the last item, holding us back from dealing with the core Subject Area.

M.3) I need to read up more on Constraint Checking to make sure I am understanding things.

Don't worry about that now; I was giving you reason to keep it simple (the non-compliant SQL databases appear to simplify things but actually they make it more complex).  MySQL has none of those capabilities, so you can eliminate consideration of the platform, and just model the Cardinality meaningfully.

M.4) Depends if you envision OrderPurchase in the future. Can you expand as to what you mean here?

In the context of the Model. You provide the structures to make SalesOrders (of Items).  Therefore Item, Order and OrderItem.

But if you provided the structures to track PurchaseOrders as well (to purchase Items as well as office supplies, rent, whatever), then you need to differentiate Sales Orders and Purchase Orders.  Therefore:

Item

OrderSale and OrderSaleItem

OrderPurchase and OrderPurchaseItem

Version 1.1

U.2) Event Progressed

EventDate looks good. I would define the Relation as Event Was Perfromed On EvenDate.

Whereas ItemGenre is perfect, Event::Venue Needs work.  This is a mistake you make consistently, so an explanation is called for.

You have modelled Venue correctly, it is Independent and does exist outside the context of Event. But Event May Be [Held] At zero-to-many [Independent] Venues is not possible.

Events are held at many Venues, and Venues host many Events.  If that was all, since this is the Logical Level, you can draw a many-to-many Relation, and you are done.  At the Physical level, that Relation is resolved by implementing an Associative Table, of which the PK is the two parent PKs, and there is no data. (Enemy is a good example.)

But if there is data (eg. you need to track the date or number of attendees or whatever), then it is not an Associative Table, it is another Entity. A Thing that Takes Place between Event and Venue.

EventDate is a good candidate.  We already have that, and the date.  Just add Venue and stir.  I would call the Thing that Takes Place between Event and Venue a Performance.

Likewise, EventAddress has progressed but is not complete.

Do Events have Addresses or Venues have Addresses ? (model it, no need for words)

If Venue: do you need all the historic Addresses for the Venue (like Party), or just the current one (like Order) ?

M.5) SubGenre.  Can you explain why SubGenre is (a) Independent and (b) the Relation is Non-Identifying.

M.6) Item Is zero-to-many Favourites. Therefore: Item Is a Favourite of zero-to-many Users.  Likewise, Each User Chooses zero-to-many Favourites. Therefore Each User Chooses zero-to-many Favourite Items.

V1.2 and Response

Great Progress.

U.2) Event Further Progressed

Going by your Edit as well as the new Requirements, some yes and some no. All the other Subject Areas of the Data Model are pretty much complete (for Logical), this one area is confused, not nearly as resolved.  Partly because of the added Requirements (no complaint, that happens in real life; it is about how you handle it).

The main point I will make here is that the Data Model should always model the real world, as opposed to only the business Requirement.  That (a) insulates the DM from the effect of change and (b) provides a solid platform for added Requirements.  That does not mean you have to model the whole real world, but the parts of it that you do model must reflect reality and not be squished up to fill just the Requirement.

Second, there is lack of clarity about the distinctions between Event, Band-Event, Performance, etc.  Right now an Event is a Party-Band-Item-Event.  That's fine, but it does not work for the new style Event per Requirement.

Third, you have a good handle on Address re Party and Order, but not re Venue.

Since you are accepting the Standard-compliant model and therefore the treatment, Address is a Reference table.

It is Independent (square corners)

Actually, you can place Address and everything above it on page one; making this part of the model page two, and have Address only on this page.

Correctly modelled: A Party has a history of Addresses.  They must have at least one current { IsBilling | IsShipping | IsPhysical } Address, based on whatever activity is being executed.

Correctly modelled: An Order has one IsBilling Address (if you need IsShipping, you need to add a separate  Relation).

Address is not a child of Venue (also Independent, correct).  I do not think a Venue is located in zero-to-many Addresses. (Maybe that is the old Cardinality-reversed bug, but I am not sure, due to the other confusion re Event and Venue.)

Actually Address::Order is suspicious. (Q.3) Do you want Order to reference any valid Address, or a specific address for the Party executing the Order ?

Back to Event.  Accepting EventDate as declared.  That's fine but then Reviews etc, apply to the generic concert and not the single concert which they performed on mushrooms.
Go for V1.3.

Your terminology re Event, etc is consistent with the Requirement, etc. but it does not support the Requirement as stated.

So let us start using "Event" the way it is used in the real world, and model it that way.  What we have been calling "Event", the Party-Band-Item, is actually a Performance.  And not a generic one that is scheduled, but a single one at a specific Venue.

That is either what you meant with EventDate, or EventDate resolves into Performance.

If you do not mind, I will avoid typing one thousand words, and give you a picture. Subject Area Example V1.2

Notice that the multiple Bands per Event is resolved.

And the Verb Phrases are straight from heaven. An Address hosted multiple Venues, each of which catered multiple Events, each of which is multiple Performances, each of which is one Party-Band-Item.

U.3) Is it time to move the link between Item and Band up to Item and Party instead? With the current design I don't see a possibility to sell merchandise not tied to the band as you have brought up.

First, we need to use Relational terminology, not because I am a pedant, but because the real gurus say it really helps to make the transition to the Relational world.

Second, we cannot accomplish that by "moving the Relation".

You have to model non-Band merchandise: how you are going to sell it; track it; get paid for it.  Whether you want Reviews and Responses, etc.  I do not see what Party has to do with it, and right now we are selling Band-Items, not Party-Items.  Consider the Referential Integrity issues.

Version 1.2

AR.1) After going through the exercise for FavoriteItem, I feel that Item to Review requires a many-to-many relationship so that is indicated. Necessary?

In V1.1, An Item had many Reviews, and a Review was about one Item.  A Person generated many Reviews (one per Item).  That is logical.

A Review is about many Items is not reasonable.

If anything, now that FavouriteItem/FavouriteBand is resolved, Review needs likewise resolution and distinction: do we need to differentiate BandReview from ItemReview; does a good/bad ItemReview indicate a good/bad BandReview or are they discrete ?

a Review (as it stands) cannot be about either a Band or an Item.  That means two Foreign Keys, and one of the will Null, and Null FKs are not allowed.  Item and Band are alreay differentiated, and that differentiation is mature.

ItemReviews can be summarised, etc, but that is a different story.

U.7) That leaves us with a new issue to resolve. If a Review can be about a Band or Album or Song or Performance, how do we ensure that Referential Integrity. We do not need an AlbumReview to reference a SongReview, etc. Model it.

R.5) The model currently provides Genre at the Item level, that means Album and Song  (Merchandise can be disallowed via a CHECK Constraint). Not Band.  That may be enough, given that (a) bands change over time, (b) that kind of classification at the Item level is more precise, and (c) Band Genre can be easily derived from their Albums or Songs.

If you need separate Band Genres, you need to add that.

What about Event Genre ?  If you need it, I think it will be one Genre per Event.

Keep in mind that tables like Venue and Genre are serious search criteria in a major database.  Vectors for analysis.

The Data Warehouse boys need to add this in as Dimensions to their Facts; in a  properly modelled Database, they already exist as Dimensions to Facts.  Show me all the Venues with "Folk Music" Events scheduled that attracted more than 10,000 People is dead easy.

.

Discussion Point. Not saying the above is incorrect. What I have found in both Databases and iTunes is, precision counts.  Why have laissez faire Genre::Several things when you can have Genre ::Specific Thing.  If you had Genre::Song only, and Song has one Genre only, then Album and Band are precise roll-ups.  The way we have it now, it depends on the music knowledge of the data entry person, and Genre::Thing is many, so it is loose.  Genre::Song is tight.

R.6) members can show that they will be attending the Event is not modelled.  Also clarify interest vs booking vs attendance.

R.8) Is not modelled.

M.3) The issue is closed, but the Verb Phrase remains unchanged.

M.7) Logical Model vis-a-vis Associative tables.  Now that that issue is closed, remove any Associative tables for the Logical model; any remaining tables (between two parents) will contain data.  That means, go through all the Dependent tables and remove any that do not have data.  Thus V1.3 should be less cluttered.

M.8) Item is OrderItem.

M.9) Now that Party-Person-User is resolved.  An Exclusive Subtype structure requires a Discriminator, and the Constrainst will be used to enforce Integrity.  Where there are many, PartyType is the way to go.  But for just two, a column IsBand or IsPerson is adequate.

M.10) You have corrected the cardinality-reversed bug, but some Verb Phrases are still going the wrong way.

27 Jan 11

Actually, I think a lot of these issues would be clearer if we move into the Logical Key/Attribute level (rather than just Entity Relation level). And it is high time we did.  For example:

Q.3) Order:Address is suspicious.  The constraint is not quite correct because that would allow the order to have any Address, not an Address that is specific to the Party executing the order.

But since you are MySQL, which has no Referential Integrity, you may not be aware of how it is done in real SQL, so I will provide the FK Definitions, which happen to be RI Constraints as well.  It is kind of unfair to expect you to understand my terse statements, which are based in the RM, Normalisation and supported by SQL, when you do not have SQL.

In order for the two constraints to be true, since Party must be the same in each Constraint (there is only one Order.PartyId), only the subset of PartyAddress which belongs to PartyId, will be allowed.

Address Qualification Example

Continued in Part II ...


来源: stackexchange

问:
value of a factor variable is different than level
I have a factor variable in a dataframe as follows

```<code>str(x)
'data.frame':   1000 obs. of  10 variables:
 $ PK                : chr  "1-108" "1-10M" "1-10F" "1-10Q" ...
 $ var1      : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
 $ var2: Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
</code>```

when I subset x on var1, value of factor variable shows 2 in str command

```<code>y <- x[x$var1== 1,]
str(y)
'data.frame':   300 obs. of  10 variables:
 $ PK  : chr  "1-12U" "1-13895" "1-13R" "1-149" ...
 $ var1: Factor w/ 2 levels "0","1": 2 2 2 2 2 2 2 2 2 2 ...
 $ var2: Factor w/ 2 levels "0","1": 2 1 1 2 2 1 2 2 2 1 ...
</code>```

but actual value of var1 is 1

```<code>y$var1
   [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
</code>```

答:
When you filter your data, levels of factor var1 is not change.

```<code>x <- factor(1, levels = c(0, 1))
str(x)
# Factor w/ 2 levels "0","1": 2
</code>```

ContentLicense: CC BY-SA 3.0

Body:
I have a factor variable in a dataframe as follows

str(x)
'data.frame':   1000 obs. of  10 variables:
 $ PK                : chr  "1-108" "1-10M" "1-10F" "1-10Q" ...
 $ var1      : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
 $ var2: Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...

when I subset x on var1, value of factor variable shows 2 in str command

y <- x[x$var1== 1,]
str(y)
'data.frame':   300 obs. of  10 variables:
 $ PK  : chr  "1-12U" "1-13895" "1-13R" "1-149" ...
 $ var1: Factor w/ 2 levels "0","1": 2 2 2 2 2 2 2 2 2 2 ...
 $ var2: Factor w/ 2 levels "0","1": 2 1 1 2 2 1 2 2 2 1 ...

but actual value of var1 is 1

y$var1
   [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

Body:
When you filter your data, levels of factor var1 is not change.

x <- factor(1, levels = c(0, 1))
str(x)
# Factor w/ 2 levels "0","1": 2


问:
recursively get CSS from an element and all the childs

So I would like to be able to do something like this:

```<code>getRecursiveCSS(document.getElementById('#menubar'))
</code>```

And I would like it to return a string of CSS, for the main element and all the childs.

This is what I have tried: (does not work)

```<code>function fullPath(el){
  var names = [];
  while (el.parentNode){
    if (el.id){
      names.unshift('#'+el.id);
      break;
    }else{
      if (el==el.ownerDocument.documentElement) names.unshift(el.tagName);
      else{
        for (var c=1,e=el;e.previousElementSibling;e=e.previousElementSibling,c++);
        names.unshift(el.tagName+":nth-child("+c+")");
      }
      el=el.parentNode;
    }
  }
  return names.join(" > ");
}

function styleRecursive(elements, css) {
    elements = Object.prototype.toString.call(elements) === '[object Array]' ? elements: [elements];
    if (elements.length == 0 || typeof elements[0] == 'undefined')
        return css;

    if (typeof elements[0].querySelector == 'undefined')
        return css

    if (typeof css == 'undefined')
        css = fullPath(elements[0]) + '{' + getComputedStyle(elements[0]).cssText + '}';
    else
        css += fullPath(elements[0]) + '{' + getComputedStyle(elements[0]).cssText + '}';

    _elements = [];

    for (var i = 0; i < elements.length; i++) {
        for (var ii = 0; ii < elements[i].childNodes.length; ii++)
            _elements.push(elements[i].childNodes[ii]);
    }

    return styleRecursive(_elements, css);
};
</code>```

答:
i came up with a solution that maybe give you idea about how improve your code. In order to test driving this code I've made an element that have some children in different depths and this code traverse all children by their depth in recursive way to find/get their css. After that, all founded css plus the element name will storage in an object (JSON like) for later use.

Please Note:

1) This code is not bullet proof so you need to add a lot of conditions/checker to make it work for all kind of situations.

2) Tested in chrome.

3) Limited to classes for finding element and its children (easy to upgrade for ids and tags support)

Output:

```<code>one : {
  display: "block",
  position: "relative"
}
two : {
  display: "inline-block",
  font-family: "Montserrat"
}
three_1 : {
  display: "table",
  position: "absolute",
  left: "0px"
}
four_1 : {
  display: "table-cell",
  position: "relative"
}
three_2 : {
  display: "table",
  position: "absolute",
  right: "0px"
}
four_2 : {
  display: "table-cell",
  position: "relative"
}
</code>```

HTML(Sample):

```<code><div class="one">
    <div class="two">
        <div class="three_1">
            <div class="four_1"></div>
        </div>
        <div class="three_2">
            <div class="four_2"></div>
        </div>
    </div>
</div>
</code>```

CSS(Sample):

```<code>.one {display:block;position:relative;}
.two {display:inline-block;font-family:'Montserrat';}
.three_1 {display:table;position:absolute;left:0;}
.three_2 {display:table;position:absolute;right:0;}
.four_1 {display:table-cell;position:relative;}
.four_2 {display:table-cell;position:relative;}
</code>```

JS:

```<code>function convertObjlike(css) {
    var s = {};
    if (!css) return s;
    css = css.split("; ");
    for (var i in css) {
        var l = css[i].split(": ");
        s[l[0].toLowerCase()] = (l[1]);
    }
    return s;
}

function getCss(a) {
    var sheets = document.styleSheets, o = {};
    for (var i in sheets) {
        var rules = sheets[i].rules || sheets[i].cssRules;
        for (var r in rules) {
            if (a === rules[r].selectorText) {
                o = convertObjlike(rules[r].style.cssText);
            }
        }
    }
    return o;
}

var anObject = {};
function styleRecursive(element){
  anObject[element.className] = (getCss('.'+element.className));
  var children = element.children;

  for (var i = 0; i < children.length; i++) {
    styleRecursive(children[i])
  }
}

styleRecursive( document.querySelector('.one') );
console.log(anObject);
</code>```

Jsfiddle

Body:
So I would like to be able to do something like this:

getRecursiveCSS(document.getElementById('#menubar'))

And I would like it to return a string of CSS, for the main element and all the childs.

This is what I have tried: (does not work)

function fullPath(el){
  var names = [];
  while (el.parentNode){
    if (el.id){
      names.unshift('#'+el.id);
      break;
    }else{
      if (el==el.ownerDocument.documentElement) names.unshift(el.tagName);
      else{
        for (var c=1,e=el;e.previousElementSibling;e=e.previousElementSibling,c++);
        names.unshift(el.tagName+":nth-child("+c+")");
      }
      el=el.parentNode;
    }
  }
  return names.join(" > ");
}

function styleRecursive(elements, css) {
    elements = Object.prototype.toString.call(elements) === '[object Array]' ? elements: [elements];
    if (elements.length == 0 || typeof elements[0] == 'undefined')
        return css;

    if (typeof elements[0].querySelector == 'undefined')
        return css

    if (typeof css == 'undefined')
        css = fullPath(elements[0]) + '{' + getComputedStyle(elements[0]).cssText + '}';
    else
        css += fullPath(elements[0]) + '{' + getComputedStyle(elements[0]).cssText + '}';

    _elements = [];

    for (var i = 0; i < elements.length; i++) {
        for (var ii = 0; ii < elements[i].childNodes.length; ii++)
            _elements.push(elements[i].childNodes[ii]);
    }

    return styleRecursive(_elements, css);
};

Body:
i came up with a solution that maybe give you idea about how improve your code. In order to test driving this code I've made an element that have some children in different depths and this code traverse all children by their depth in recursive way to find/get their css. After that, all founded css plus the element name will storage in an object (JSON like) for later use.

Please Note:

1) This code is not bullet proof so you need to add a lot of conditions/checker to make it work for all kind of situations.

2) Tested in chrome.

3) Limited to classes for finding element and its children (easy to upgrade for ids and tags support)

Output:

one : {
  display: "block",
  position: "relative"
}
two : {
  display: "inline-block",
  font-family: "Montserrat"
}
three_1 : {
  display: "table",
  position: "absolute",
  left: "0px"
}
four_1 : {
  display: "table-cell",
  position: "relative"
}
three_2 : {
  display: "table",
  position: "absolute",
  right: "0px"
}
four_2 : {
  display: "table-cell",
  position: "relative"
}

HTML(Sample):

<div class="one">
    <div class="two">
        <div class="three_1">
            <div class="four_1"></div>
        </div>
        <div class="three_2">
            <div class="four_2"></div>
        </div>
    </div>
</div>

CSS(Sample):

.one {display:block;position:relative;}
.two {display:inline-block;font-family:'Montserrat';}
.three_1 {display:table;position:absolute;left:0;}
.three_2 {display:table;position:absolute;right:0;}
.four_1 {display:table-cell;position:relative;}
.four_2 {display:table-cell;position:relative;}

JS:

function convertObjlike(css) {
    var s = {};
    if (!css) return s;
    css = css.split("; ");
    for (var i in css) {
        var l = css[i].split(": ");
        s[l[0].toLowerCase()] = (l[1]);
    }
    return s;
}

function getCss(a) {
    var sheets = document.styleSheets, o = {};
    for (var i in sheets) {
        var rules = sheets[i].rules || sheets[i].cssRules;
        for (var r in rules) {
            if (a === rules[r].selectorText) {
                o = convertObjlike(rules[r].style.cssText);
            }
        }
    }
    return o;
}

var anObject = {};
function styleRecursive(element){
  anObject[element.className] = (getCss('.'+element.className));
  var children = element.children;

  for (var i = 0; i < children.length; i++) {
    styleRecursive(children[i])
  }
}

styleRecursive( document.querySelector('.one') );
console.log(anObject);

Jsfiddle


问:
Ionic Native File Opener plugin won't open .pdf files with default app in Android
I'm quite new to the whole Ionic and Cordova environment and I'm having some trouble trying to open a .pdf file from the Ionic app with the default native pdf reader.

Here is my markup:

```<code><ion-scroll overflow-scroll="true" scrollX="true">
<ion-card>
  <ion-card-content>
    <a href="assets/sheets/clicks_bs.pdf">
      <img src="assets/sheets/clicks_bs.jpg"/>
    </a>
  </ion-card-content>
</ion-card>
<ion-card>
  <ion-card-content>
    <a href="assets/sheets/makro.pdf">
      <img src="assets/sheets/makro.png" />
    </a>
  </ion-card-content>
</ion-card>
<ion-card>
  <ion-card-content>
    <a href="assets/sheets/picknpay_bs.pdf">
      <img src="assets/sheets/picknpay_bs.jpg"/>
    </a>
  </ion-card-content>
</ion-card>
</ion-scroll>
</code>```

Here is my component:

```<code>import { Component, ViewChild } from '@angular/core';
import { Slides } from 'ionic-angular';
import { FileOpener } from '@ionic-native/file-opener';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html',
  providers: [FileOpener]
})
export class HomePage {

  @ViewChild('storebrands') slides: Slides;
  constructor(private fileOpener: FileOpener ) {
    let i = 0;
    setInterval(() => {
      this.goToSlide(i);
      if (i === 8)
        i = 0;
      else
        i++;
    }, 5000);

    this.fileOpener.open('assets/sheets/*.pdf', 'application/pdf')
      .then(() => console.log('File is opened'))
      .catch(e => console.log('Error openening file', e));

   }
}
</code>```

I have imported the file opener and injected it into the constructor and I've added it as a provider, however when I try clicking on the image in Android, it does nothing. Can someone please give some insight on why it isn't opening in the default .pdf reader on a native device.

Thanks in advance!

答:
Use DocumentViewer Plugin

```<code>showDocument(pdff){
      const options: DocumentViewerOptions = {
        title: 'My PDF',
        openWith:   { enabled: true},
        bookmarks : {
          enabled : true
      },
      search : {
          enabled : true
      },
      autoClose: {
          onPause : true
      }
      }
      const imgeLocation = `${cordova.file.applicationDirectory}www/assets/imgs/${pdff}`;
      this.document.viewDocument(imgeLocation, 'application/pdf', options)
    }
</code>```

Body:
I'm quite new to the whole Ionic and Cordova environment and I'm having some trouble trying to open a .pdf file from the Ionic app with the default native pdf reader.

Here is my markup:

<ion-scroll overflow-scroll="true" scrollX="true">
<ion-card>
  <ion-card-content>

  </ion-card-content>
</ion-card>
<ion-card>
  <ion-card-content>

  </ion-card-content>
</ion-card>
<ion-card>
  <ion-card-content>

  </ion-card-content>
</ion-card>
</ion-scroll>

Here is my component:

import { Component, ViewChild } from '@angular/core';
import { Slides } from 'ionic-angular';
import { FileOpener } from '@ionic-native/file-opener';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html',
  providers: [FileOpener]
})
export class HomePage {

  @ViewChild('storebrands') slides: Slides;
  constructor(private fileOpener: FileOpener ) {
    let i = 0;
    setInterval(() => {
      this.goToSlide(i);
      if (i === 8)
        i = 0;
      else
        i++;
    }, 5000);

    this.fileOpener.open('assets/sheets/*.pdf', 'application/pdf')
      .then(() => console.log('File is opened'))
      .catch(e => console.log('Error openening file', e));

   }
}

I have imported the file opener and injected it into the constructor and I've added it as a provider, however when I try clicking on the image in Android, it does nothing. Can someone please give some insight on why it isn't opening in the default .pdf reader on a native device.

Thanks in advance!

Body:
Use DocumentViewer Plugin

showDocument(pdff){
      const options: DocumentViewerOptions = {
        title: 'My PDF',
        openWith:   { enabled: true},
        bookmarks : {
          enabled : true
      },
      search : {
          enabled : true
      },
      autoClose: {
          onPause : true
      }
      }
      const imgeLocation = `${cordova.file.applicationDirectory}www/assets/imgs/${pdff}`;
      this.document.viewDocument(imgeLocation, 'application/pdf', options)
    }


问:
Is there a (relatively) painless upgrade path from ASP.NET 1.1 to 3.5?
One of my clients has an e-commerce CMS written in VB.NET 1.1.  (The original solution was authored with Visual Studio 2003.)  It's not an enormous site, but it does consist of around 40 or 50 Web forms, and I'd like to upgrade it to .NET 3.5, and to maintain and continue developing it it with VS 2008.

I's sure I could do it surgically, re-creating the new project one file at a time, testing at each step of the way, and so on, but is there an easier way?  I somewhat optimistically hoped VS 2008 would offer some sort of an upgrade wizard, but no such luck for Web Site projects, it looks like.

Anyone gone through this already?  If so, what would you suggest as the most sensible approach?

答:
I've done both ways - recreating the project one file at a time and also just leaving it. It took way longer than I thought to recreate the project, I ended up spending about 5-10 minutes a page on a site with about 200 pages. If I had known it would take so long I would not have done it. Only if you are going to be actively maintaining the site rather than the occasional update would I recommend investing that time upfront. (Then again, if you are paid by the hour... hmm...)

In projects since then, I've let the wizard upgrade the solution to VS2008 (changes web.config, references and your project file a little bit) then I just left the ugly code the way it was.

OwnerDisplayName: Christian Nunciato

ContentLicense: CC BY-SA 2.5

LastEditorDisplayName: Kent Fredric

OwnerDisplayName: David Thomas Garcia


问:
Can the html page run from local URL converted into pdf with colour in chrome
I have created a html page with bootstrap control and my own css. I am running the page through visual studio in chrome. I want this page to get converted into pdf with colours as it is displayed in chrome. I have not connected any printer to my system. If i give ctrl+P, my page is getting displayed without colours.
I have used 'save as pdf' extension of chrome for conversion.
It is saying 'cant access your local url'. Is there a way to print the page in the same way as it is seen in browser as a pdf

Bootstrap controls like progress bar not getting appeared in print.

答:
I had the same issue but found an easy solution.
Simply get the google chrome extension called : FireShot

Get it here: https://chrome.google.com/webstore/detail/take-webpage-screenshots/mcbpblocgmgfnpjjppndjkmgjaogfceg?hl=en

It will take full page screenshot of your HTML page and even local Html pages.
Then capture the full page.

After it will lead you to a new page with saving option.
Beside “Email” I suggest you to click on “PDF”. Then it will lead you to Gmail so you can send yourself the PDF.

I don’t suggest to click on print, because in my case when I saved it, there was an issue with the pixels.

Very easy.


答:
Well, factors are actually expressed internally as natural numbers starting from 1 and mapped sequentially to factor levels. With 'str(y)' you get internal expression, and by 'print(y)' or simply 'y' in interactive environment you get mapped value.

Let me show you:

```<code>> t = c("0", "0", "1", "1")
> t
[1] "0" "0" "1" "1"
> t2 = as.factor(t)
> t2
[1] 0 0 1 1
Levels: 0 1
> str(t2)
 Factor w/ 2 levels "0","1": 1 1 2 2
> t2[t2 == 0]
[1] 0 0
Levels: 0 1
> str(t2[t2 == 0])
 Factor w/ 2 levels "0","1": 1 1
</code>```

Title: value of a factor variable is different than level

Body:
Well, factors are actually expressed internally as natural numbers starting from 1 and mapped sequentially to factor levels. With 'str(y)' you get internal expression, and by 'print(y)' or simply 'y' in interactive environment you get mapped value.

Let me show you:

> t = c("0", "0", "1", "1")
> t
[1] "0" "0" "1" "1"
> t2 = as.factor(t)
> t2
[1] 0 0 1 1
Levels: 0 1
> str(t2)
 Factor w/ 2 levels "0","1": 1 1 2 2
> t2[t2 == 0]
[1] 0 0
Levels: 0 1
> str(t2[t2 == 0])
 Factor w/ 2 levels "0","1": 1 1


问:
Left join for D3 mapping returns null that prevents next step of data processing (ndjson-join or jq) command line
I am doing D3 mapping on a state level. Here  one problem that i met in data processing. For example, map data are like this, (dat1.ndjson)

```<code>{state: a, code: aa}
{state: b, code: bb}
{state: c, code: cc}
</code>```

But usually the information we have are not complete, for example, there are no information in Antarctica usually but we still need to draw its contour when we do mapping. Information data is like, (dat2.ndjson)

```<code>{state: a, code: aa, count: 1}
{state: b, code: bb, count: 2}
</code>```

So, when i try to do left join on these two data, it will returns (dat3.ndjson)

```<code>[{state: a, code: aa},{state: a, code: aa, count: 1}]
[{state: b, code: bb},{state: b, code: bb, count: 2}]
[{state: c, code: cc},null]
</code>```

This is returned by

```<code>ndjson-join --left 'd.code' dat1.ndjson dat2.ndjson < merge.ndjson
</code>```

The purpose is to connect this 'count' information to map data, so usually I will first assign all items a count = 0 in dat1.ndjson, like this, (dat11.ndjson)

```<code>{state: a, code: aa, count: 0}
{state: b, code: bb, count: 0}
{state: c, code: cc, count: 0}
</code>```

and then use this left join method like the one I showed before to get something like this, (dat33.ndjson)

```<code>[{state: a, code: aa, count: 0},{state: a, code: aa, count: 1}]
[{state: b, code: bb, count: 0},{state: b, code: bb, count: 2}]
[{state: c, code: cc, count: 0},null]
</code>```

But here comes the problem. If i use the following command to add all values together, it will return an error because of that null in the third line.

```<code>ndjson-map '{state: d[0].state, code: d[0].code, count: d[0].count +
d[1].count}' < dat33.ndjson > merge.ndjson
</code>```

Now I have to do this data processing in R which takes a lot of time as I need to do transformation between .ndjson and .csv. So I am looking for a better way to do this. I think there might be some ways by using 'ndjson-cli', 'jq' or 'awk' and 'sed' and etc.

Anyone have ideas? Thank you! :)

E.

答:
Here is a solution that has several parts:

Transforming your input into valid JSON.

Library functions for performing joins.

Running jq to produce the desired output, assuming your version of jq is sufficiently recent.

What to do if you only have access to jq 1.5

To illustrate how straightforward everything is once you have the fluff issues resolved, here is the "main" jq program:

```<code>join(.state) | .count //= 0
</code>```

In effect, this says: perform the join using .state as the join-key, and then ensure .count is set.

The output from the above one-liner will be the NDJSON:

```<code>{"state":"a","code":"aa","count":1}
{"state":"b","code":"bb","count":2}
{"state":"c","code":"cc","count":0}
</code>```

Part 1: dat1.json and dat2.json

I am going to assume that you can produce valid JSON from your inputs.
For the sample data, I used sed:

```<code>for i in 1 2 ; do
  sed -e 's/state/"state"/' -e 's/code/"code"/' -e 's/count/"count"/' \
      -e 's/ \([a-z]*\)\([,}]\)/ "\1"\2/g' dat$i.ndjson > dat$i.json
done
</code>```

The following at any rate assumes you have two files, dat1.json and dat2.json, containing streams of valid JSON.

Part 2: join

Here is a small library of filters for producing joins: the first works on streams, and the others on arrays.  These definitions assume your jq has INDEX/2.  See Part 4 if that is not the case.

```<code>def joins(s1; s2; filter1; filter2):
  # combine two dictionaries using `add`
    def merge: . as $in
    | reduce (add|keys_unsorted[]) as $k ({}; .[$k] = ([$in[] | .[$k]] | add));
  [INDEX(s1; filter1 | select(. != null)), INDEX(s2; filter2 | select(. != null))]
  | merge[] ;

def join(filter1; filter2):
  joins(.[0][]; .[1][]; filter1; filter2);

def join(f): join(f; f);
</code>```

Part 3. Solution

First, let's keep things simple.  If you place the above definitions for join and joins in a file, say d3.jq, followed by the one-line program given in the preamble, then the following invocation will do the trick, assuming your jq has INDEX:

```<code>jq -c -s -f d3.jq <(jq -s . dat1.json) <(jq -s . dat2.json)
</code>```

This assumes you are using a shell that supports process substitution.  If not, then you can first run the "." programs separately, e.g. if you have sponge:

```<code> for i in 1 2 ; do jq -s . dat$i.json | sponge dat$i.json ; done
</code>```

Using include

If your jq supports include, and if you have the above definitions of join in a private standard library such as ~/.jq/jq/jq.jq
then your main jq program becomes the two-liner:

```<code>include "jq";
join(.state) | .count //= 0'
</code>```

This means you could dispense with d3.jq and use the invocation:

```<code>jq -c -s 'include "jq"; join(.state)|.count //= 0' \
   <(jq -s . dat1.json) <(jq -s . dat2.json)
</code>```

Part 4: INDEX

Here is a copy of INDEX as provided by recent versions of jq.  You could add these definitions to d3.jq (before the "main" part of the program), or to your library file, and so on:

```<code>def INDEX(stream; idx_expr):
  reduce stream as $row ({};
    .[$row|idx_expr|
      if type != "string" then tojson
      else .
      end] |= $row);

def INDEX(idx_expr): INDEX(.[]; idx_expr);
</code>```

Body:
I am doing D3 mapping on a state level. Here  one problem that i met in data processing. For example, map data are like this, (dat1.ndjson)

{state: a, code: aa}
{state: b, code: bb}
{state: c, code: cc}

But usually the information we have are not complete, for example, there are no information in Antarctica usually but we still need to draw its contour when we do mapping. Information data is like, (dat2.ndjson)

{state: a, code: aa, count: 1}
{state: b, code: bb, count: 2}

So, when i try to do left join on these two data, it will returns (dat3.ndjson)

[{state: a, code: aa},{state: a, code: aa, count: 1}]
[{state: b, code: bb},{state: b, code: bb, count: 2}]
[{state: c, code: cc},null]

This is returned by

ndjson-join --left 'd.code' dat1.ndjson dat2.ndjson < merge.ndjson

The purpose is to connect this 'count' information to map data, so usually I will first assign all items a count = 0 in dat1.ndjson, like this, (dat11.ndjson)

{state: a, code: aa, count: 0}
{state: b, code: bb, count: 0}
{state: c, code: cc, count: 0}

and then use this left join method like the one I showed before to get something like this, (dat33.ndjson)

[{state: a, code: aa, count: 0},{state: a, code: aa, count: 1}]
[{state: b, code: bb, count: 0},{state: b, code: bb, count: 2}]
[{state: c, code: cc, count: 0},null]

But here comes the problem. If i use the following command to add all values together, it will return an error because of that null in the third line.

ndjson-map '{state: d[0].state, code: d[0].code, count: d[0].count +
d[1].count}' < dat33.ndjson > merge.ndjson

Now I have to do this data processing in R which takes a lot of time as I need to do transformation between .ndjson and .csv. So I am looking for a better way to do this. I think there might be some ways by using 'ndjson-cli', 'jq' or 'awk' and 'sed' and etc.

Anyone have ideas? Thank you! :)

E.

Body:
Here is a solution that has several parts:

Transforming your input into valid JSON.

Library functions for performing joins.

Running jq to produce the desired output, assuming your version of jq is sufficiently recent.

What to do if you only have access to jq 1.5

To illustrate how straightforward everything is once you have the fluff issues resolved, here is the "main" jq program:

join(.state) | .count //= 0

In effect, this says: perform the join using .state as the join-key, and then ensure .count is set.

The output from the above one-liner will be the NDJSON:

{"state":"a","code":"aa","count":1}
{"state":"b","code":"bb","count":2}
{"state":"c","code":"cc","count":0}

Part 1: dat1.json and dat2.json

I am going to assume that you can produce valid JSON from your inputs.
For the sample data, I used sed:

for i in 1 2 ; do
  sed -e 's/state/"state"/' -e 's/code/"code"/' -e 's/count/"count"/' \
      -e 's/ \([a-z]*\)\([,}]\)/ "\1"\2/g' dat$i.ndjson > dat$i.json
done

The following at any rate assumes you have two files, dat1.json and dat2.json, containing streams of valid JSON.

Part 2: join

Here is a small library of filters for producing joins: the first works on streams, and the others on arrays.  These definitions assume your jq has INDEX/2.  See Part 4 if that is not the case.

def joins(s1; s2; filter1; filter2):
  # combine two dictionaries using `add`
    def merge: . as $in
    | reduce (add|keys_unsorted[]) as $k ({}; .[$k] = ([$in[] | .[$k]] | add));
  [INDEX(s1; filter1 | select(. != null)), INDEX(s2; filter2 | select(. != null))]
  | merge[] ;

def join(filter1; filter2):
  joins(.[0][]; .[1][]; filter1; filter2);

def join(f): join(f; f);

Part 3. Solution

First, let's keep things simple.  If you place the above definitions for join and joins in a file, say d3.jq, followed by the one-line program given in the preamble, then the following invocation will do the trick, assuming your jq has INDEX:

jq -c -s -f d3.jq <(jq -s . dat1.json) <(jq -s . dat2.json)

This assumes you are using a shell that supports process substitution.  If not, then you can first run the "." programs separately, e.g. if you have sponge:

 for i in 1 2 ; do jq -s . dat$i.json | sponge dat$i.json ; done

Using include

If your jq supports include, and if you have the above definitions of join in a private standard library such as ~/.jq/jq/jq.jq
then your main jq program becomes the two-liner:

include "jq";
join(.state) | .count //= 0'

This means you could dispense with d3.jq and use the invocation:

jq -c -s 'include "jq"; join(.state)|.count //= 0' \
   <(jq -s . dat1.json) <(jq -s . dat2.json)

Part 4: INDEX

Here is a copy of INDEX as provided by recent versions of jq.  You could add these definitions to d3.jq (before the "main" part of the program), or to your library file, and so on:

def INDEX(stream; idx_expr):
  reduce stream as $row ({};
    .[$row|idx_expr|
      if type != "string" then tojson
      else .
      end] |= $row);

def INDEX(idx_expr): INDEX(.[]; idx_expr);


问:
JQuery - trigger call on div becoming visible
I have a div that is shown / hidden through CSS media queries.

I want to trigger a JQuery call when this div becomes visible - triggering once if the div changes from

```<code>display: none;
</code>```

to

```<code>display: block;
</code>```

Is this possible to do?

Something like the onclick call, but on the div property changing to visible?

```<code>$(document).on('click', '#mydiv', function() {
}
</code>```

答:
UPDATED to handle it on page load

Use window.matchMedia() with rule used for the div to become visible and set relevant event listener filtering by event matches property.

Here an example BUT you have to use your own media queries rule.

```<code>var widthMatch = window.matchMedia("(min-width:500px)");
if(widthMatch.matches) {
  divIsVisible();
}
widthMatch.addListener(function(e){
  if(e.matches) {
    divIsVisible();
  }
});

function divIsVisible(){
  console.log('DIV is visible!')
}
</code>```

-jsFiddle-

Body:
I have a div that is shown / hidden through CSS media queries.

I want to trigger a JQuery call when this div becomes visible - triggering once if the div changes from

display: none;

to

display: block;

Is this possible to do?

Something like the onclick call, but on the div property changing to visible?

$(document).on('click', '#mydiv', function() {
}

Body:
UPDATED to handle it on page load

Use window.matchMedia() with rule used for the div to become visible and set relevant event listener filtering by event matches property.

Here an example BUT you have to use your own media queries rule.

var widthMatch = window.matchMedia("(min-width:500px)");
if(widthMatch.matches) {
  divIsVisible();
}
widthMatch.addListener(function(e){
  if(e.matches) {
    divIsVisible();
  }
});

function divIsVisible(){
  console.log('DIV is visible!')
}

-jsFiddle-


问:
How to insert data to MySQL database in asp.net page?
i am trying to use MySQL 5 and asp.net 4

i succeed withe the linking and display the data from MySQL DB but the problem is within the DML Commands !!

please take a look to my code

```<code>Imports System.Data.SqlClient
Imports MySql.Data.MySqlClient

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim MyConnection As String = "server=localhost;User Id=root;password=123;database=cms"

        Dim Connection As New MySqlConnection(MyConnection)

        Connection.Open()

        Dim Sql As String = "INSERT INTO [CMS.tbimages] ( Title,Description) VALUES (parm2,parm3);"
        Dim cmd As New MySqlCommand(Sql, Connection)

        cmd.Parameters.Add(New MySqlParameter("parm2", "hajjaj"))
        cmd.Parameters.Add(New MySqlParameter("parm3", "hajjaj"))

        cmd.ExecuteNonQuery()
        cmd.Connection.Close()
    End Sub
End Class
</code>```

what i did is a simple page for testing the insert command , I added a button when i click on it it should do the insert command!!

but when i click it it gives me error :

#42000 You have an error in your SQL syntax; check the manual that
  corresponds to your MySQL server
  version for the right syntax to use
  near '[CMS.tbimages] (
  Title,Description) VALUES
  (parm2,parm3)' at line 1

not : i tried many other ways to change the insert statement :

"INSERT INTO [tbimages] ( [Title], [Description]) VALUES ( ? , ?)"

"INSERT INTO [tbimages] ([Title], [Description])

 VALUES (?Title , ?Description)"

INSERT INTO [tbimages] ( [Title], [Description]) VALUES (Title, Description)

答:
MySQL doesn't use brackets around identifiers, it uses backticks:

```<code>Dim Sql As String = "INSERT INTO `CMS.tbimages` (Title,Description) VALUES (?,?)"
</code>```

If the table names doesn't cause any conflicts, you don't need to enclose it in backticks either:

```<code>Dim Sql As String = "INSERT INTO CMS.tbimages (Title,Description) VALUES (?,?)"
</code>```

I'm not sure how your database driver handles parameters, but I think they should be unnamed.

Body:
i am trying to use MySQL 5 and asp.net 4

i succeed withe the linking and display the data from MySQL DB but the problem is within the DML Commands !!

please take a look to my code

Imports System.Data.SqlClient
Imports MySql.Data.MySqlClient

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim MyConnection As String = "server=localhost;User Id=root;password=123;database=cms"

        Dim Connection As New MySqlConnection(MyConnection)

        Connection.Open()

        Dim Sql As String = "INSERT INTO [CMS.tbimages] ( Title,Description) VALUES (parm2,parm3);"
        Dim cmd As New MySqlCommand(Sql, Connection)

        cmd.Parameters.Add(New MySqlParameter("parm2", "hajjaj"))
        cmd.Parameters.Add(New MySqlParameter("parm3", "hajjaj"))

        cmd.ExecuteNonQuery()
        cmd.Connection.Close()
    End Sub
End Class

what i did is a simple page for testing the insert command , I added a button when i click on it it should do the insert command!!

but when i click it it gives me error :

#42000 You have an error in your SQL syntax; check the manual that
  corresponds to your MySQL server
  version for the right syntax to use
  near '[CMS.tbimages] (
  Title,Description) VALUES
  (parm2,parm3)' at line 1

not : i tried many other ways to change the insert statement :

"INSERT INTO [tbimages] ( [Title], [Description]) VALUES ( ? , ?)"

"INSERT INTO [tbimages] ([Title], [Description])

 VALUES (?Title , ?Description)"

INSERT INTO [tbimages] ( [Title], [Description]) VALUES (Title, Description)

Body:
MySQL doesn't use brackets around identifiers, it uses backticks:

Dim Sql As String = "INSERT INTO `CMS.tbimages` (Title,Description) VALUES (?,?)"

If the table names doesn't cause any conflicts, you don't need to enclose it in backticks either:

Dim Sql As String = "INSERT INTO CMS.tbimages (Title,Description) VALUES (?,?)"

I'm not sure how your database driver handles parameters, but I think they should be unnamed.


问:
Angular UI Datepicker Limiting Days to One Month
I'm using the datepicker for AngularUI.

By default it lists the days from the previous month and the next month. Here's a picture.

How do I make these days invisible. I'd like the first day to always be Sunday. So the days should be listed Sunday, Monday, Tuesday, etc on top of the columns.

答:
You can try this;

```<code>function hideDates(){
   var span = document.getElementsByClassName("text-muted");
   for(var i = 0; i<span.length;i++){
     span[i].parentNode.classList.add('remove-borders');
   }
}
Call hideDates() function on every Month change. You can hide the dates.
In css:
.remove-borders{
  border: none !important;
}
.text-muted{
  color:transparent;
}
table tr td button.remove-borders:hover,
table tr td button.remove-borders:active,
table tr td button.remove-borders:focus{
   background: #fff;
   color: #fff;
   pointer-events: none;
   outline: none;
}
</code>```

This will remove the borders for the parentNode which is a Button. So It won't show the previous month & next month dates. Note that we are hiding the dates with the css only.

Body:
You can try this;

function hideDates(){
   var span = document.getElementsByClassName("text-muted");
   for(var i = 0; i<span.length;i++){
     span[i].parentNode.classList.add('remove-borders');
   }
}
Call hideDates() function on every Month change. You can hide the dates.
In css:
.remove-borders{
  border: none !important;
}
.text-muted{
  color:transparent;
}
table tr td button.remove-borders:hover,
table tr td button.remove-borders:active,
table tr td button.remove-borders:focus{
   background: #fff;
   color: #fff;
   pointer-events: none;
   outline: none;
}

This will remove the borders for the parentNode which is a Button. So It won't show the previous month & next month dates. Note that we are hiding the dates with the css only.


问:
Saving attachments from current email to a derived folder.
I'm looking for a starting point here, so no code to post I'm afraid !

I would like (if possible) to be able to open an email in Outlook (in the normal way, from the front-end), and then click a button to run a macro, which will extract the attachments from this email and save them to a directory path (derived from the subject).

Sound do-able ?

Any pointers, links code snippets welcome !

答:
This subroutine will save all attachments found in a user specified Outlook folder to a user specified directory on the file system. It also updates each message with a link to the purged files.

It contains extra comments to help highlight how the .Delete method will shrink Attachment containers dynamically (search for "~~" in the comments).

This subroutine is only tested on Outlook 2010.

```<code>' ------------------------------------------------------------.
' Requires the following references:
'    Visual Basic for Applications
'    Microsoft Outlook 14.0 Object Library
'    OLE Automation
'    Microsoft Office 14.0 Object Library
'    Microsoft Shell Controls and Automation
' ------------------------------------------------------------.

Public Sub SaveOLFolderAttachments()

 ' Ask the user to select a file system folder for saving the attachments
 Dim oShell As Object
 Set oShell = CreateObject("Shell.Application")
 Dim fsSaveFolder As Object
 Set fsSaveFolder = oShell.BrowseForFolder(0, "Please Select a Save Folder:", 1)
 If fsSaveFolder Is Nothing Then Exit Sub
 ' Note:  BrowseForFolder doesn't add a trailing slash

 ' Ask the user to select an Outlook folder to process
 Dim olPurgeFolder As Outlook.MAPIFolder
 Set olPurgeFolder = Outlook.GetNamespace("MAPI").PickFolder
 If olPurgeFolder Is Nothing Then Exit Sub

 ' Iteration variables
 Dim msg As Outlook.MailItem
 Dim att As Outlook.attachment
 Dim sSavePathFS As String
 Dim sDelAtts as String

 For Each msg In olPurgeFolder.Items

   sDelAtts = ""

   ' We check each msg for attachments as opposed to using .Restrict("[Attachment] > 0")
   ' on our olPurgeFolder.Items collection.  The collection returned by the Restrict method
   ' will be dynamically updated each time we remove an attachment.  Each update will
   ' reindex the collection.  As a result, it does not provide a reliable means for iteration.
   ' This is why the For Each style loops will not work. ~~
   If msg.Attachments.Count > 0 Then

     ' This While loop is controlled via the .Delete method which
     ' will decrement msg.Attachments.Count by one each time. ~~
     While msg.Attachments.Count > 0

       ' Save the attachment to the file system
       sSavePathFS = fsSaveFolder.Self.Path & "\" & msg.Attachments(1).FileName
       msg.Attachments(1).SaveAsFile sSavePathFS

       ' Build up a string to denote the file system save path(s)
       ' Format the string according to the msg.BodyFormat.
       If msg.BodyFormat <> olFormatHTML Then
            sDelAtts = sDelAtts & vbCrLf & "<file://" & sSavePathFS & ">"
       Else
            sDelAtts = sDelAtts & "<br>" & "<a href='file://" & sSavePathFS & "'>" & sSavePathFS & "</a>"
       End If

       ' Delete the current attachment.  We use a "1" here instead of an "i"
       ' because the .Delete method will shrink the size of the msg.Attachments
       ' collection for us.  Use some well placed Debug.Print statements to see
       ' the behavior. ~~
       msg.Attachments(1).Delete

      Wend

     ' Modify the body of the msg to show the file system location of
     ' the deleted attachments.
     If msg.BodyFormat <> olFormatHTML Then
        msg.Body = msg.Body & vbCrLf & vbCrLf & "Attachments Deleted:  " & Date & " " & Time & vbCrLf & vbCrLf & "Saved To:  " & vbCrLf & sDelAtts
     Else
        msg.HTMLBody = msg.HTMLBody & "<p></p><p>" & "Attachments Deleted:  " & Date & " " & Time & vbCrLf & vbCrLf & "Saved To:  " & vbCrLf & sDelAtts & "</p>"
     End If

      ' Save the edits to the msg.  If you forget this line, the attachments will not be deleted.  ~~
     msg.Save

    End If

  Next

End Sub
</code>```

Body:
This subroutine will save all attachments found in a user specified Outlook folder to a user specified directory on the file system. It also updates each message with a link to the purged files.

It contains extra comments to help highlight how the .Delete method will shrink Attachment containers dynamically (search for "~~" in the comments).

This subroutine is only tested on Outlook 2010.

' ------------------------------------------------------------.
' Requires the following references:
'    Visual Basic for Applications
'    Microsoft Outlook 14.0 Object Library
'    OLE Automation
'    Microsoft Office 14.0 Object Library
'    Microsoft Shell Controls and Automation
' ------------------------------------------------------------.

Public Sub SaveOLFolderAttachments()

 ' Ask the user to select a file system folder for saving the attachments
 Dim oShell As Object
 Set oShell = CreateObject("Shell.Application")
 Dim fsSaveFolder As Object
 Set fsSaveFolder = oShell.BrowseForFolder(0, "Please Select a Save Folder:", 1)
 If fsSaveFolder Is Nothing Then Exit Sub
 ' Note:  BrowseForFolder doesn't add a trailing slash

 ' Ask the user to select an Outlook folder to process
 Dim olPurgeFolder As Outlook.MAPIFolder
 Set olPurgeFolder = Outlook.GetNamespace("MAPI").PickFolder
 If olPurgeFolder Is Nothing Then Exit Sub

 ' Iteration variables
 Dim msg As Outlook.MailItem
 Dim att As Outlook.attachment
 Dim sSavePathFS As String
 Dim sDelAtts as String

 For Each msg In olPurgeFolder.Items

   sDelAtts = ""

   ' We check each msg for attachments as opposed to using .Restrict("[Attachment] > 0")
   ' on our olPurgeFolder.Items collection.  The collection returned by the Restrict method
   ' will be dynamically updated each time we remove an attachment.  Each update will
   ' reindex the collection.  As a result, it does not provide a reliable means for iteration.
   ' This is why the For Each style loops will not work. ~~
   If msg.Attachments.Count > 0 Then

     ' This While loop is controlled via the .Delete method which
     ' will decrement msg.Attachments.Count by one each time. ~~
     While msg.Attachments.Count > 0

       ' Save the attachment to the file system
       sSavePathFS = fsSaveFolder.Self.Path & "\" & msg.Attachments(1).FileName
       msg.Attachments(1).SaveAsFile sSavePathFS

       ' Build up a string to denote the file system save path(s)
       ' Format the string according to the msg.BodyFormat.
       If msg.BodyFormat <> olFormatHTML Then
            sDelAtts = sDelAtts & vbCrLf & "<file://" & sSavePathFS & ">"
       Else
            sDelAtts = sDelAtts & "<br>" & "<a href='file://" & sSavePathFS & "'>" & sSavePathFS & "</a>"
       End If

       ' Delete the current attachment.  We use a "1" here instead of an "i"
       ' because the .Delete method will shrink the size of the msg.Attachments
       ' collection for us.  Use some well placed Debug.Print statements to see
       ' the behavior. ~~
       msg.Attachments(1).Delete

      Wend

     ' Modify the body of the msg to show the file system location of
     ' the deleted attachments.
     If msg.BodyFormat <> olFormatHTML Then
        msg.Body = msg.Body & vbCrLf & vbCrLf & "Attachments Deleted:  " & Date & " " & Time & vbCrLf & vbCrLf & "Saved To:  " & vbCrLf & sDelAtts
     Else
        msg.HTMLBody = msg.HTMLBody & "<p></p><p>" & "Attachments Deleted:  " & Date & " " & Time & vbCrLf & vbCrLf & "Saved To:  " & vbCrLf & sDelAtts & "</p>"
     End If

      ' Save the edits to the msg.  If you forget this line, the attachments will not be deleted.  ~~
     msg.Save

    End If

  Next

End Sub


问:
brew install python installs python2
I have 3 versions of python installed on my Mac. 2 of them are through brew i.e. python2 and python3 while the native version is python. the problem is when I put brew install python it installs python2 and not python. (By saying python means the version which runs on putting that command in terminal). What should I do so that if I type python my brew installed python launches.I have my path variables set correctly and the brew installation path is ahead than that of the usr/bin The problem I am encountering is that I have nltk installed through pip, pip2 and pip3 and when I import nltk in python2 and python3 there is no problem but when I do that in python it show no module found.

答:
Try which python in a terminal to see which python will run. Then you know and can act accordingly to fix it.

ls -lsa $(which python) will let you see if it is a symlink to another location or a real executable. if a Symlink you can see where it points to and so you can follow the breadcrumbs to the final binary used.

if you run python from a terminal and in the python REPL do the following

```<code>import sys
print sys.path
</code>```

you can see to which site-packages locations are pointed.

Other than that you of course have the option to use virtual environments to set up your version of python

if you really want python2 to be the default python command you can add a symlink to your ~/bin folder (create it if it does not exist)

```<code>mkdir ~/bin
cd ~/bin
ln -s $(which python2) python
chmod +x python
</code>```

and make sure that export PATH=~/bin:$PATH is added at the back of your .bashrc or .profile or .zshrc file

Now start a new terminal session and try out python again it should point to brews version

Hope that helps

Title: brew install python installs python2

Body:
Try which python in a terminal to see which python will run. Then you know and can act accordingly to fix it.

ls -lsa $(which python) will let you see if it is a symlink to another location or a real executable. if a Symlink you can see where it points to and so you can follow the breadcrumbs to the final binary used.

if you run python from a terminal and in the python REPL do the following

import sys
print sys.path

you can see to which site-packages locations are pointed.

Other than that you of course have the option to use virtual environments to set up your version of python

if you really want python2 to be the default python command you can add a symlink to your ~/bin folder (create it if it does not exist)

mkdir ~/bin
cd ~/bin
ln -s $(which python2) python
chmod +x python

and make sure that export PATH=~/bin:$PATH is added at the back of your .bashrc or .profile or .zshrc file

Now start a new terminal session and try out python again it should point to brews version

Hope that helps


问:
How to print just the top 10 words and their frequency from a HashMap?
I have a problem with my code.
I am reading in a file with some text, and then counting the words and their frequency. I am then trying to print out the top 10 most frequently used words in the text.

However, with the approaches that I have tried (putting for loops set to stop after 10 iterations for example), I get the words printed back in the same fashion just 10 times. Otherwise, I am able to print ALL the words in the file with their frequency.I would just require the top-10 most frequently used ones only.

```<code>ArrayList<Integer> values = new ArrayList<>();
    values.addAll(wordcount.values());
    Collections.sort(values, Collections.reverseOrder());
    int last_i = -1;
    for(Integer i: values) {
        if (last_i == i)
            continue;
        last_i = i;
        System.out.println("The top 10 words are: ");
       // for (int count = 0; count < 10; count++) {
            for (String s : wordcount.keySet())
                if (wordcount.get(s) == i)
                    System.out.println(s + " : " + i);
           }
       }
}
</code>```

Please find above the "problematic" code. I am using a

```<code>BufferedReader
</code>```

to read in the text File and then removing all punctuation and stuff that may cause the same word to appear as two different entries in my HashMap.

Any help in greatly appreciated. Thanks!

答:
First you need to count how many times each word is repeated in text with help of Map, after that sort map entries in reverse order in result of this most frequent words will be in the start of the collection and print first ten elements from this collection.

```<code>public void printTopTenWordsByFrequencyFrom(List<String> text) {
    Map<String, Integer> map = new HashMap<>();
    for(String word : text) {
        Integer times = map.get(word);
        if(times == null) {
            map.put(word, 1);
        } else {
            map.put(word, times + 1);
        }
    }
    map.entrySet().stream()
                  .sorted((one, another) -> - one.getValue().compareTo(another.getValue())) //sort entries to reverse order
                  .limit(10)
                  .forEach(entry -> entry.getKey() + " : " + entry.getValue());
}
</code>```

Version without streams and lambdas:

```<code>  public void printTopTenWordsByFrequencyFrom(List<String> text) {
        Map<String, Integer> map = new HashMap<>();
        for(String word : text) {
            Integer times = map.get(word);
            if(times == null) {
                map.put(word, 1);
            } else {
                map.put(word, times + 1);
            }
        }
        List<Map.Entry<String, Integer>> statistics = new ArrayList<>(map.entrySet());
        Collections.sort(statistics, new Comparator<Map.Entry<String, Integer> {

            int compare(Map.Entry<String, Integer> one, Map.Entry<String, Integer> another) {
                return - one.getValue().compareTo(another.getValue());
            }
        });
        List<Map.Entry<String, Integer>> topTen = statistics.sublist(0, 9);
        for(Map.Entry<String, Integer> word : topTen) {
            System.out.println(word.getKey() + " : " + word.getValue());
        }
    }
</code>```

Body:
I have a problem with my code.
I am reading in a file with some text, and then counting the words and their frequency. I am then trying to print out the top 10 most frequently used words in the text.

However, with the approaches that I have tried (putting for loops set to stop after 10 iterations for example), I get the words printed back in the same fashion just 10 times. Otherwise, I am able to print ALL the words in the file with their frequency.I would just require the top-10 most frequently used ones only.

ArrayList<Integer> values = new ArrayList<>();
    values.addAll(wordcount.values());
    Collections.sort(values, Collections.reverseOrder());
    int last_i = -1;
    for(Integer i: values) {
        if (last_i == i)
            continue;
        last_i = i;
        System.out.println("The top 10 words are: ");
       // for (int count = 0; count < 10; count++) {
            for (String s : wordcount.keySet())
                if (wordcount.get(s) == i)
                    System.out.println(s + " : " + i);
           }
       }
}

Please find above the "problematic" code. I am using a

BufferedReader

to read in the text File and then removing all punctuation and stuff that may cause the same word to appear as two different entries in my HashMap.

Any help in greatly appreciated. Thanks!

Body:
First you need to count how many times each word is repeated in text with help of Map, after that sort map entries in reverse order in result of this most frequent words will be in the start of the collection and print first ten elements from this collection.

public void printTopTenWordsByFrequencyFrom(List<String> text) {
    Map<String, Integer> map = new HashMap<>();
    for(String word : text) {
        Integer times = map.get(word);
        if(times == null) {
            map.put(word, 1);
        } else {
            map.put(word, times + 1);
        }
    }
    map.entrySet().stream()
                  .sorted((one, another) -> - one.getValue().compareTo(another.getValue())) //sort entries to reverse order
                  .limit(10)
                  .forEach(entry -> entry.getKey() + " : " + entry.getValue());
}

Version without streams and lambdas:

  public void printTopTenWordsByFrequencyFrom(List<String> text) {
        Map<String, Integer> map = new HashMap<>();
        for(String word : text) {
            Integer times = map.get(word);
            if(times == null) {
                map.put(word, 1);
            } else {
                map.put(word, times + 1);
            }
        }
        List<Map.Entry<String, Integer>> statistics = new ArrayList<>(map.entrySet());
        Collections.sort(statistics, new Comparator<Map.Entry<String, Integer> {

            int compare(Map.Entry<String, Integer> one, Map.Entry<String, Integer> another) {
                return - one.getValue().compareTo(another.getValue());
            }
        });
        List<Map.Entry<String, Integer>> topTen = statistics.sublist(0, 9);
        for(Map.Entry<String, Integer> word : topTen) {
            System.out.println(word.getKey() + " : " + word.getValue());
        }
    }


问:
How to solve this error: Failed to resolve: com.android.support:appcompat-v7:26.1.0
I have just installed Android Studio 3.0.1 and I have attempted numerous solutions from the previous time this question was asked but none of them seem to be working. I still receive this error: Failed to resolve: com.android.support:appcompat-v7:26.1.0

My top level build file is as follows:

```<code>// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {

    allprojects{
        repositories{
            jcenter()
        }
    }
    repositories {
        // Gradle 4.1 and higher include support for Google's Maven repo using
        // the google() method. And you need to include this repo to download
        // Android plugin 3.0.0 or higher.
        maven { url "https://maven.google.com" }
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'
    }
}
</code>```

My app build.gradle file includes:

```<code>apply plugin: 'com.android.application'

repositories {
    maven { url "https://maven.google.com" }
    jcenter { url "http://jcenter.bintray.com/" }
}

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.1"

    defaultConfig {
        applicationId "com.example.application"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
    }
    buildTypes {

        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:26.0.1'
    compile 'com.android.support:design:23.1.1'
}
</code>```

答:
Make your 'com.android.support:design:23.1.1' - same version as your appcompat-v7. Then remove from app gradle

repositories {
    maven { url "https://maven.google.com" }
    jcenter { url "http://jcenter.bintray.com/" }
}

Body:
I have just installed Android Studio 3.0.1 and I have attempted numerous solutions from the previous time this question was asked but none of them seem to be working. I still receive this error: Failed to resolve: com.android.support:appcompat-v7:26.1.0

My top level build file is as follows:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {

    allprojects{
        repositories{
            jcenter()
        }
    }
    repositories {
        // Gradle 4.1 and higher include support for Google's Maven repo using
        // the google() method. And you need to include this repo to download
        // Android plugin 3.0.0 or higher.
        maven { url "https://maven.google.com" }
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'
    }
}

My app build.gradle file includes:

apply plugin: 'com.android.application'

repositories {
    maven { url "https://maven.google.com" }
    jcenter { url "http://jcenter.bintray.com/" }
}

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.1"

    defaultConfig {
        applicationId "com.example.application"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
    }
    buildTypes {

        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:26.0.1'
    compile 'com.android.support:design:23.1.1'
}


问:
Uncaught Error: Module build failed: Error: No PostCSS Config found in: \ng2-admin\node_modules\bootstrap-loader at Error (native)
I have clone ng2-admin directory from git hub few days back and run npm install after that i am able to run and see the UI, now I had been deleted node_modules folder, docs folder, and dist folder from ng2-admin now facing below error in browser console with scattered UI.

So how to fix below error,
`

```<code>Uncaught Error: Module build failed: Error: No PostCSS Config found in: D:\ng2-admin\node_modules\bootstrap-loader
    at Error (native)
    at D:\ng2-admin\node_modules\postcss-load-config\index.js:51:26
    at Error (native)
    at D:\ng2-admin\node_modules\postcss-load-config\index.js:51:26
    at Object../node_modules/css-loader/index.js!./node_modules/postcss-loader/index.js!./node_modules/resolve-url-loader/index.js!./node_modules/sass-loader/index.js?sourceMap!./node_modules/bootstrap-loader/lib/bootstrap.styles.loader.js!./node_modules/bootstrap-loader/no-op.js (http://localhost:5000/assets/vendor.bundle.js:56497:7)
    at __webpack_require__ (http://localhost:5000/assets/polyfills.bundle.js:54:30)
    at Object../node_modules/style-loader/index.js!./node_modules/css-loader/index.js!./node_modules/postcss-loader/index.js!./node_modules/resolve-url-loader/index.js!./node_modules/sass-loader/index.js?sourceMap!./node_modules/bootstrap-loader/lib/bootstrap.styles.loader.js!./node_modules/bootstrap-loader/no-op.js (http://localhost:5000/assets/vendor.bundle.js:88168:15)
    at __webpack_require__ (http://localhost:5000/assets/polyfills.bundle.js:54:30)
    at Object../node_modules/bootstrap-loader/lib/bootstrap.loader.js!./node_modules/bootstrap-loader/no-op.js (http://localhost:5000/assets/vendor.bundle.js:52755:22)
    at __webpack_require__ (http://localhost:5000/assets/polyfills.bundle.js:54:30)
    at Object../node_modules/bootstrap-loader/loader.js (http://localhost:5000/assets/vendor.bundle.js:52782:18)
    at __webpack_require__ (http://localhost:5000/assets/polyfills.bundle.js:54:30)
    at Object../src/vendor.browser.ts (http://localhost:5000/assets/vendor.bundle.js:90058:1)
    at __webpack_require__ (http://localhost:5000/assets/polyfills.bundle.js:54:30)
    at Object.5 (http://localhost:5000/assets/vendor.bundle.js:90087:18)
    at __webpack_require__ (http://localhost:5000/assets/polyfills.bundle.js:54:30)
    at webpackJsonpCallback (http://localhost:5000/assets/polyfills.bundle.js:25:23)
    at http://localhost:5000/assets/vendor.bundle.js:2:1
./node_modules/css-loader/index.js!./node_modules/postcss-loader/index.js!./node_modules/resolve-url-loader/index.js!./node_modules/sass-loader/index.js?sourceMap!./node_modules/bootstrap-loader/lib/bootstrap.styles.loader.js!./node_modules/bootstrap-loader/no-op.js @ util.js:164
__webpack_require__ @ bootstrap 7cfd64b…:52
./node_modules/style-loader/index.js!./node_modules/css-loader/index.js!./node_modules/postcss-loader/index.js!./node_modules/resolve-url-loader/index.js!./node_modules/sass-loader/index.js?sourceMap!./node_modules/bootstrap-loader/lib/bootstrap.styles.loader.js!./node_modules/bootstrap-loader/no-op.js @ no-op.js?6a4c:4
__webpack_require__ @ bootstrap 7cfd64b…:52
./node_modules/bootstrap-loader/lib/bootstrap.loader.js!./node_modules/bootstrap-loader/no-op.js @ no-op.js:1
__webpack_require__ @ bootstrap 7cfd64b…:52
./node_modules/bootstrap-loader/loader.js @ loader.js:1
__webpack_require__ @ bootstrap 7cfd64b…:52
./src/vendor.browser.ts @ vendor.browser.ts:24
__webpack_require__ @ bootstrap 7cfd64b…:52
5 @ src async:7
__webpack_require__ @ bootstrap 7cfd64b…:52
webpackJsonpCallback @ bootstrap 7cfd64b…:23
(anonymous) @ vendor.bundle.js:2
lang.js:134 Angular 2 is running in the development mode. Call enableProdMode() to enable the production mode.
client:38 [WDS] Errors while compiling.
client:80 ./~/css-loader!./~/postcss-loader!./~/resolve-url-loader!./~/sass-loader?sourceMap!./~/bootstrap-loader/lib/bootstrap.styles.loader.js!./~/bootstrap-loader/no-op.js
Module build failed: Error: No PostCSS Config found in: D:\ng2-admin\node_modules\bootstrap-loader
    at Error (native)
    at D:\Client2\ng2-admin\node_modules\postcss-load-config\index.js:51:26
 @ ./~/style-loader!./~/css-loader!./~/postcss-loader!./~/resolve-url-loader!./~/sass-loader?sourceMap!./~/bootstrap-loader/lib/bootstrap.styles.loader.js!./~/bootstrap-loader/no-op.js 4:14-193
 @ ./~/bootstrap-loader/lib/bootstrap.loader.js!./~/bootstrap-loader`  /no-op.js`
 @ ./~/bootstrap-loader/loader.js
 @ ./src/vendor.browser.ts
 @ multi vendor

`
</code>```

答: This can be caused by a reference error, more specifically an import for CSS file that doesn't exist.</p>

Body:
I have clone ng2-admin directory from git hub few days back and run npm install after that i am able to run and see the UI, now I had been deleted node_modules folder, docs folder, and dist folder from ng2-admin now facing below error in browser console with scattered UI.

So how to fix below error,
`

Uncaught Error: Module build failed: Error: No PostCSS Config found in: D:\ng2-admin\node_modules\bootstrap-loader
    at Error (native)
    at D:\ng2-admin\node_modules\postcss-load-config\index.js:51:26
    at Error (native)
    at D:\ng2-admin\node_modules\postcss-load-config\index.js:51:26
    at Object../node_modules/css-loader/index.js!./node_modules/postcss-loader/index.js!./node_modules/resolve-url-loader/index.js!./node_modules/sass-loader/index.js?sourceMap!./node_modules/bootstrap-loader/lib/bootstrap.styles.loader.js!./node_modules/bootstrap-loader/no-op.js (http://localhost:5000/assets/vendor.bundle.js:56497:7)
    at __webpack_require__ (http://localhost:5000/assets/polyfills.bundle.js:54:30)
    at Object../node_modules/style-loader/index.js!./node_modules/css-loader/index.js!./node_modules/postcss-loader/index.js!./node_modules/resolve-url-loader/index.js!./node_modules/sass-loader/index.js?sourceMap!./node_modules/bootstrap-loader/lib/bootstrap.styles.loader.js!./node_modules/bootstrap-loader/no-op.js (http://localhost:5000/assets/vendor.bundle.js:88168:15)
    at __webpack_require__ (http://localhost:5000/assets/polyfills.bundle.js:54:30)
    at Object../node_modules/bootstrap-loader/lib/bootstrap.loader.js!./node_modules/bootstrap-loader/no-op.js (http://localhost:5000/assets/vendor.bundle.js:52755:22)
    at __webpack_require__ (http://localhost:5000/assets/polyfills.bundle.js:54:30)
    at Object../node_modules/bootstrap-loader/loader.js (http://localhost:5000/assets/vendor.bundle.js:52782:18)
    at __webpack_require__ (http://localhost:5000/assets/polyfills.bundle.js:54:30)
    at Object../src/vendor.browser.ts (http://localhost:5000/assets/vendor.bundle.js:90058:1)
    at __webpack_require__ (http://localhost:5000/assets/polyfills.bundle.js:54:30)
    at Object.5 (http://localhost:5000/assets/vendor.bundle.js:90087:18)
    at __webpack_require__ (http://localhost:5000/assets/polyfills.bundle.js:54:30)
    at webpackJsonpCallback (http://localhost:5000/assets/polyfills.bundle.js:25:23)
    at http://localhost:5000/assets/vendor.bundle.js:2:1
./node_modules/css-loader/index.js!./node_modules/postcss-loader/index.js!./node_modules/resolve-url-loader/index.js!./node_modules/sass-loader/index.js?sourceMap!./node_modules/bootstrap-loader/lib/bootstrap.styles.loader.js!./node_modules/bootstrap-loader/no-op.js @ util.js:164
__webpack_require__ @ bootstrap 7cfd64b…:52
./node_modules/style-loader/index.js!./node_modules/css-loader/index.js!./node_modules/postcss-loader/index.js!./node_modules/resolve-url-loader/index.js!./node_modules/sass-loader/index.js?sourceMap!./node_modules/bootstrap-loader/lib/bootstrap.styles.loader.js!./node_modules/bootstrap-loader/no-op.js @ no-op.js?6a4c:4
__webpack_require__ @ bootstrap 7cfd64b…:52
./node_modules/bootstrap-loader/lib/bootstrap.loader.js!./node_modules/bootstrap-loader/no-op.js @ no-op.js:1
__webpack_require__ @ bootstrap 7cfd64b…:52
./node_modules/bootstrap-loader/loader.js @ loader.js:1
__webpack_require__ @ bootstrap 7cfd64b…:52
./src/vendor.browser.ts @ vendor.browser.ts:24
__webpack_require__ @ bootstrap 7cfd64b…:52
5 @ src async:7
__webpack_require__ @ bootstrap 7cfd64b…:52
webpackJsonpCallback @ bootstrap 7cfd64b…:23
(anonymous) @ vendor.bundle.js:2
lang.js:134 Angular 2 is running in the development mode. Call enableProdMode() to enable the production mode.
client:38 [WDS] Errors while compiling.
client:80 ./~/css-loader!./~/postcss-loader!./~/resolve-url-loader!./~/sass-loader?sourceMap!./~/bootstrap-loader/lib/bootstrap.styles.loader.js!./~/bootstrap-loader/no-op.js
Module build failed: Error: No PostCSS Config found in: D:\ng2-admin\node_modules\bootstrap-loader
    at Error (native)
    at D:\Client2\ng2-admin\node_modules\postcss-load-config\index.js:51:26
 @ ./~/style-loader!./~/css-loader!./~/postcss-loader!./~/resolve-url-loader!./~/sass-loader?sourceMap!./~/bootstrap-loader/lib/bootstrap.styles.loader.js!./~/bootstrap-loader/no-op.js 4:14-193
 @ ./~/bootstrap-loader/lib/bootstrap.loader.js!./~/bootstrap-loader`  /no-op.js`
 @ ./~/bootstrap-loader/loader.js
 @ ./src/vendor.browser.ts
 @ multi vendor

`


问:
Debug Websocket with Fiddler
I'm trying to debugging a websocket frame with fiddler, I use the next code to achieve that

```<code>class Handlers
{
    // ...

    static function OnWebSocketMessage(oMsg: WebSocketMessage)
    {
        // Modify a message's content
        var sPayload = oMsg.PayloadAsString();
        var pattern = "Hello, \([a-zA-Z]+\)!";
        var match = Regex.Match(sPayload, pattern);

        if (match.Success) {
            var pattern = "Hello, \([a-zA-Z]+\)!";
            var match = Regex.Match(sPayload, pattern);
            var who = match.Groups[1].ToString();

            var forgedWho = String.Format("FORGED-{0}", who);
            var changedPayload = sPayload.Replace(who, forgedWho);
            FiddlerApplication.Log.LogString(String.Format("Changing {0} to {1}", who, forgedWho));
            oMsg.SetPayload(changedPayload);
        }
    }
}
</code>```

I'm triyng to add "FORGED" to the Web Socket Message but when i try to save the script the next error displays

Error

The error says "The variable 'Regex' has not been declared" but regex is a function, could you help me.

I extract this code from here http://www.mopsled.com/2016/debug-websocket-connections/

答: Add import System.Text.RegularExpressions or use directly System.Text.RegularExpressions.Regex.Match(sPayload, pattern)

ContentLicense: CC BY-SA 4.0

Title: Debug Websocket with Fiddler

Body:
I'm trying to debugging a websocket frame with fiddler, I use the next code to achieve that

class Handlers
{
    // ...

    static function OnWebSocketMessage(oMsg: WebSocketMessage)
    {
        // Modify a message's content
        var sPayload = oMsg.PayloadAsString();
        var pattern = "Hello, \([a-zA-Z]+\)!";
        var match = Regex.Match(sPayload, pattern);

        if (match.Success) {
            var pattern = "Hello, \([a-zA-Z]+\)!";
            var match = Regex.Match(sPayload, pattern);
            var who = match.Groups[1].ToString();

            var forgedWho = String.Format("FORGED-{0}", who);
            var changedPayload = sPayload.Replace(who, forgedWho);
            FiddlerApplication.Log.LogString(String.Format("Changing {0} to {1}", who, forgedWho));
            oMsg.SetPayload(changedPayload);
        }
    }
}

I'm triyng to add "FORGED" to the Web Socket Message but when i try to save the script the next error displays

Error

The error says "The variable 'Regex' has not been declared" but regex is a function, could you help me.

I extract this code from here http://www.mopsled.com/2016/debug-websocket-connections/