Design choice of list of objects or list/map as object property, for the following situations - object

I'm using Flutter/Dart but this is really a basic OOP question I have for all languages.
I'm looking for an answer that tells me which of the designs below have the best resource management as well as easiest coding.
This similar post does not really satisfy me, I feel I need a better black & white answer that can discern the two designs.
Situation One
A rarely modified object which gets created once but read somewhat often and updated rarely
Situation Two
A heavily accessed object- mainly read actions but very regularly created, a great many objects 1000+ & they will be regularly filtered
Example:
List<SalesIncome> salesIncome = "Sales income from DB";
num totalSalesIncome = salesIncome.where((salesPerson) =>
salesPerson.contains('John Lohn')).toList().reduce((a, b) => a + b);
Which of the two has best resource management (memory & CPU) & best codeability for CRUD actions further down the chain at end of API? Not asking a subjective question.
To clarify, I want to know which object design #1 or #2 is best for each situation 1 & 2.
enum TimePeriod {weekly, monthly, yearly}
#1
class SalesQuota {
String salesPerson;
TimePeriod quotaPeriod;
num grossIncomeQuota;
int quotaDate;
}
#2
class SalesQuota {
String salesPerson;
TimePeriod quotaPeriod;
Map<int, num> quotaData;
}
Example for multiple object creation:
#1
SalesQuota(
John Lohn,
TimePeriod.weekly,
45600.00
1646125200000)
SalesQuota(
John Lohn,
TimePeriod.weekly,
50000.00
1646730000000)
SalesQuota(
John Lohn,
TimePeriod.weekly,
65000.00
1647331200000)
SalesQuota(
John Lohn,
TimePeriod.weekly,
68000.00
1647936000000)
#2
SalesData(
John Lohn,
TimePeriod.weekly,
{
1646125200000: 45600.00,
1646730000000: 50000.00,
1647331200000: 65000.00,
1647936000000: 68000.00,
}

Related

Find all references to a supplied noun in StanfordNLP

I'm trying to parse some text to find all references to a particular item. So, for example, if my item was The Bridge on the River Kwai and I passed it this text, I'd like it to find all the instances I've put in bold.
The Bridge on the River Kwai is a 1957 British-American epic war film
directed by David Lean and starring William Holden, Jack Hawkins, Alec
Guinness, and Sessue Hayakawa. The film is a work of fiction, but
borrows the construction of the Burma Railway in 1942–1943 for its
historical setting. The movie was filmed in Ceylon (now Sri Lanka).
The bridge in the film was near Kitulgala.
So far my attempt has been to go through all the mentions attached to each CorefChain and loop through those hunting for my target string. If I find the target string, I add the whole CorefChain, as I think this means the other items in that CorefChain also refer to the same thing.
List<CorefChain> gotRefs = new ArrayList<CorefChain>();
String pQuery = "The Bridge on the River Kwai";
for (CorefChain cc : document.get(CorefCoreAnnotations.CorefChainAnnotation.class).values()) {
List<CorefChain.CorefMention> corefMentions = cc.getMentionsInTextualOrder();
boolean addedChain = false;
for (CorefChain.CorefMention cm : corefMentions) {
if ((!addedChain) &&
(pQuery.equals(cm.mentionSpan))) {
gotRefs.add(cc);
addedChain = true;
}
}
}
I then loop through this second list of CorefChains, re-retrieve the mentions for each chain and step through them. In that loop I show which sentences have a likely mention of my item in a sentence.
for (CorefChain gr : gotRefs) {
List<CorefChain.CorefMention> corefMentionsUsing = gr.getMentionsInTextualOrder();
for (CorefChain.CorefMention cm : corefMentionsUsing) {
//System.out.println("Got reference to " + cm.mentionSpan + " in sentence #" + cm.sentNum);
}
}
It finds some of my references, but not that many, and it produces a lot of false positives. As might be entirely apparently from reading this, I don't really know the first thing about NLP - am I going about this entirely the wrong way? Is there a StanfordNLP parser that will already do some of what I'm after? Should I be training a model in some way?
I think a problem with your example is that you are looking for references to a movie title, and there isn't support in Stanford CoreNLP for recognizing movie titles, book titles, etc...
If you look at this example:
"Joe bought a laptop. He is happy with it."
You will notice that it connects:
"Joe" -> "He"
and
"a laptop" -> "it"
Coreference is an active research area and even the best system can only really be expected to produce an F1 of around 60.0 on general text, meaning it will often make errors.

making link between a person name and pronoun in GATE

Is it possible to make a link between a person name and its PRP? in GATE E.g i have document "Maria Sharapova is a tennis player from russia. She participates in international tennis tournament. She is known for winning Wimbledon, US Open and Australian Open titles as for her looks, decibel-breaking grunts and commercial savvy - all of which made her the world's highest-paid female athlete." i want to annotate the "she" as "Maria Sharapova". I have written the following JAPE rule which identifies a pattern having a PRP after a person name
Phase: Simple
Input: Lookup Token Split
Options: control = appelt
Rule:joblookup
(
({Lookup.majorType == person_first}|
{Lookup.majorType == person_full})
({Token.kind==word})+
{Split.kind==internal}
{Token.category==PRP}
):sameathlete
-->
:sameathlete.sameAthlete1 = {kind="athlete", rule="same-athlete"}
How can i make the annotation that from She means we are talking about the same person whose name is mentioned 1 or 2 sentence before??
Please help
Have you tried Co-reference PR for gate?

Current best-practice regarding record global namespacing

I'm checking that there isn't some current practice with template-haskell/some lens fanciness to deal with the simple case of:
data Person = Person {
name :: String,
...
}
data Company = Company {
name :: String,
...
}
Currently I'm avoiding polluting the global namespace by qualifying the import, but it does make the record-access clumsy.
import Person as P
isFred :: Person -> Bool
isFred p = (P.name p) == "Fred"
Is there really still no better way to access record fields?
I'm accepting #Emmanuel Touzery's answer because of the useful link to another question covering the same ground. The other question doesn't show up for a search on "haskell namespace". Nothing wrong with the other answers, but I can only accept the one.
The solution mentioned there uses Template Haskell, lenses, type-classes and more to basically create one typeclass for each field "HasName" with a single function "name". Each data-type is then an instance of that class with its own implementation. Then there's some magic I don't entirely understand allowing different types to be involved.
For any Haskell newbies wondering what this is all about, it's because records are basically tuples with field-selectors implemented as ordinary functions selecting (e.g.) the second element of that tuple. If you export these field-selector functions then they sit in the global namespace and sooner or later (usually sooner) you get a clash.
So - you either qualify the imports (as in my example above) or try to come up with names that don't clash (prefix the name and hope for the best).
The lens stuff is all the rage as of 2013 and allows composition of field selectors / getters + setters etc. The basic idea of lenses isn't too complicated but the implementation goes right over my head.
For the record (ha!) I think the solution in the other post is probably what I'm after, but it does involve a large amount of magic (5 extensions just to fake record namespacing).
Generally there are only two approaches, but unfortunately there's no consesus about them in the community:
Place your records with functions against them in separate files and use them qualified with complete alias as in:
import qualified Data.Person as Person; import Data.Person (Person)
isFred :: Person -> Bool
isFred p = (Person.name p) == "Fred"
Consider this approach the same as in languages like Java, where a file contains just one class.
Place your records in the same file, while preceding the field names with the names of records, e.g.:
data Person = Person {
personName :: String,
personAge :: Int,
...
}
Neither of the lenses-libraries approaches this problem.
A similar question was already asked, and you can see an answer suggesting lenses here:
https://stackoverflow.com/a/17488365/516188
Right now lenses are a big buzzword in the Haskell community, they definitely have their uses, and they may be part of the solution to this namespacing problem, long-term. But currently people using lenses only to solve that problem would be a small minority I think. As Nikita Volkov said, qualified imports and prefixing would be the typical solutions at this point.
UPDATE: found out about this other option that is not finalized yet but seems promessing. It is mentioned here at the end of this blog post.
Here's my approach. In practice, I find I don't often need to export record names, so namespace pollution is only a concern within the module itself. So I use a short prefix, usually the first letter of the typeclass, like so:
data Person = Person {
pName :: String,
...
}
data Company = Company {
cName :: String,
...
}
In situations where I do need to allow other modules to directly access a field, it's usually only one or two fields. And often I only want to allow read access. So in that case I might be a little creative, perhaps something like this.
module Whatever
{
Person,
personName,
Company,
companyName,
...
}
data Person = Person {
pName :: String,
...
}
data Company = Company {
cName :: String,
...
}
personName :: Person -> String
personName = pName
companyName :: Company -> String
companyName = cName

How to sort depends on Particular Column In Datagridview?

if grid like this
Name Age
Raj 20
Biny 19
Raj 17
Jose 27
Jose 15
now am click Name Grid Column Means
I want output like below
Name Age
Biny 19
Jose 15
Jose 27
Raj 17
Raj 20
See above table sort Based on Name column
Eg:-
now jose age also sort like 15,27
am using below code but it does'nt work properly
private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
var sortGlyph = dataGridView1.Columns["Name"].HeaderCell.SortGlyphDirection;
switch (sortGlyph)
{
case SortOrder.None:
case SortOrder.Ascending:
dataGridView1.Sort(dataGridView1.Columns["Name"], ListSortDirection.Descending);
dataGridView1.Columns["Name"].HeaderCell.SortGlyphDirection = SortOrder.Descending;
break;
case SortOrder.Descending:
dataGridView1.Sort(dataGridView1.Columns["Name"], ListSortDirection.Ascending);
dataGridView1.Columns["Name"].HeaderCell.SortGlyphDirection = SortOrder.Ascending;
break;
}
}
Below Code is Answer For Above Question
switch (sortGlyph)
{
case SortOrder.None:
case SortOrder.Ascending:
dt.DefaultView.Sort = "Name,Age";
dt = dt.DefaultView.ToTable();
dataGridView1.DataSource = dt;
dataGridView1.Columns["Name"].HeaderCell.SortGlyphDirection =SortOrder.Descending;
break;
case SortOrder.Descending:
dt.DefaultView.Sort = "Name,Age";
dt = dt.DefaultView.ToTable();
dataGridView1.DataSource = dt;
dataGridView1.Columns["Name"].HeaderCell.SortGlyphDirection = SortOrder.Ascending;
break;
}
If you're asking about doing it in conjunction with paging, there's no simple and scalable solution. In fact, that is kind of a holy grail of business application web development. See, for example, the StackOverflow question Dynamic Sorting within SQL Stored Procedures, which concerns the same thing. After all, if we had dynamic sorting on our database servers, we would only have to code the mechanism for managing the user's sort choices.
You really only have three options for multi-column sorts:
Do it in the client, letting your data container do the heavy lifting (when you're using a data container that has this functionality built in, like [System.Data.DataView][2]).
Write your own algorithm and sort the data yourself before binding.
Do it at the database server via one of the solutions discussed in the link above.
Neither of the client-side solutions are really scalable since they involve pulling and delivering all your data when you may only need a subset.
There are a lot of examples you can find,
http://asp-net-example.blogspot.in/2008/12/aspnet-gridview-sorting-example-how-to.html
http://csharpdotnetfreak.blogspot.in/2012/06/sorting-gridview-columns-headers-aspnet.html
allow sorting by column gridview

Design a document database schema

I'm vainly attempting to learn how to use object databases. In database textbooks the tradition seems to be to use the example of keeping track of students, courses and classes because it is so familiar and applicable. What would this example look like as an object database? The relational database would look something like
Student
ID
Name
Address
Course
ID
Name
PassingGrade
Class
ID
CourseID
Name
StartTime
StudentClass
ID
ClassID
StudentID
Grade
Would you keep StudentClasses inside of Classes which is, in turn, inside Course and then keep Student as a top level entity?
Student
ID
Name
Address
Course
ID
Name
Classes[]
Name
StartTime
Students[]
StudentID
So you have Courses, Students and Classes, which are parts of Courses and visited by Students? I think the question answers itself if you think about it. Maybe it's clearer if you go away from the pure JSON of MongoDB and look at how you would define it in an ODM (the equivalent of an ORM in RDBs) as document based DBs don't really enforce schemas of their own (example is based on MongoEngine for Python):
class Student(Document):
name = StringField(max_length=50)
address = StringField()
class Attendance(EmbeddedDocument):
student = ReferenceField(Student)
grade = IntField(min_value=0, max_value=100)
class Class(EmbeddedDocument):
name = StringField(max_length=100)
start_time = DateTimeField()
attendance_list = ListField(EmbeddedDocumentField(Attendance))
class Course(Document):
name = StringField(max_length=100)
classes = ListField(EmbeddedDocumentField(Class))
This would give you two collections: one for Students and one for Courses. Attendance would be embedded in the Classes and the Classes would be embedded in the Courses. Something like this (pseudocode):
Student = {
name: String,
address: String
}
Course = {
name: String,
classes: {
name: String,
start_time: DateTime,
attendance_list: {
student: Student,
grade: Integer
}[]
}[]
}
You could of course put the grade info in the student object, but ultimately there really isn't much you can do to get rid of that extra class.
The whole point of an OODBMS is to allow you to design your data model as if it were just in memory. Don't think of it as a database schema problem, think of it as a data modelling problem on the assumption that you have a whole lot of VM and a finite amount of physical memory, You want to make sure that you don't have to boil an ocean of page faults (or, in fact, database I/O operations) to do the operations that are important.
In a pure OODB, your model is fine.

Resources