Wikipedia:Reference desk/Archives/Computing/2017 February 7

Computing desk
< February 6 << Jan | February | Mar >> February 8 >
Welcome to the Wikipedia Computing Reference Desk Archives
The page you are currently viewing is an archive page. While you can leave answers for any questions shown below, please ask new questions on one of the current reference desk pages.


February 7

edit

I am a programming newbie --- Is it a logical problem in OOP (Js-PHP and maybe some others) ?

edit

I've seen various OOP codepieces till now. In many I've seen I came accross synatx duplications (that were needed practically) like:

this.duration = duration

As a newbie, I do understand that with "this" we reference to the duration object, than access it with the dot accessor and then put a value inside it.

I've seen this literal duplication phenomenon vastly in some PHP codes... I really don't mean to be seen as a provocative jackass, but as a newbie it seemed in elegant to me. Something that might not have been there in the first place if the language design was different to begin with.

I do wonder if any solution dealing with literal duplication is inelegant by means of turing completness and if OOP solutions that doesn't involve duplication of any kind are even possible by means of turing completness? In other words, could we have a world with OOP as a common paradigm, without nothing needs to be duplicated? Thank you and I hope I didn't came out provocative in your eyes. No such intention. 77.180.20.27 (talk) 10:13, 7 February 2017 (UTC)[reply]

Part of object oriented design is that objects should be designed to maintain "correct" values for all object variables. In your example, this.duration is an object variable. The variable duration is likely passed as a parameter. If done properly, you shouldn't blindly set this.duration=duration. You should vet the variable first with something like: if(duration>0 and duration<99) this.duration=duration. Then, set the variable private or protected to keep other programmers from haphazardly setting duration to an improper value. What you've likely seen is nearly proper programming. They protected the variables and used the proper getter/setter methods. They just didn't go that last step and vet the parameters for the setter methods. 209.149.113.5 (talk) 13:02, 7 February 2017 (UTC)[reply]
I assume you're asking about Get and Set functions? 209.149 is correct that the idea is that you could (now or at a later date) implement code that runs some logic on those variables. (As opposed to non-oop code where if you wanted to make that change you'd have to hunt down every single place in the code where that variable was changed.)
In practice it does cause a lot of redundancy, because there are a lot of variables that you really never need to run any logic on when you set them.
Newer languages like C# try to streamline this with automatic gets and sets. In C# You could declare a variable like this.
public int x { get; set; }
The language knows what a "get" function is, and you don't have to type it out. PHP is an older language, and so far as I know it doesn't have that sort of nicety. ApLundell (talk) 14:19, 7 February 2017 (UTC)[reply]
Without further context, it's hard to be sure, but the example you provided looks like simple duplication of variable names in different namespaces. I would guess that there is a class definition, within that class definition there is a method and within that method is this line. The this keyword refers to the specific instance of the class which has been implemented. The first use of duration would then refer to a property of that class. The two usages of duration aren't necessarily code duplication, but rather a duplicated variable name. The second use seems to refer to a variable whose scope extends only to the method in which this code is found. I've put a possible example of this in the collapsed section, here.
Code example
using System;

namespace ExampleNameSpace                                //namespaces are used in scoping, so an ExampleClass from a different project doesn't conflict with this one
{
   class ExampleClass                                     //this is where the definition of the class is
   {
      public float duration { get; set; }                 //this is the property
      
      public DoubleDuration()                             //this is the method
      {
         private float duration = this.duration * 2;
         this.duration = duration;                        //this would then be the line of code you quoted
      }
   }
}
If that is the case, and there are no specific reasons for using the same word to refer to a variable within a method as to a property of the class that contains that method, then this is (mildly) bad practice. It might have been better to call it period or to use an even more descriptive name, such as tempDuration or something similar.
One thing jumps out at me: The period (.) at the end. Was that something you put there to indicate the end of the line of code, or was there code after it? If the latter, then this might be a case of using a constuctor method to copy one instance to another, such as:
this.duration = duration.duplicate();   //in this case, the "this" being referred to is another class that has a duration object as one of its properties
or perhaps an example of casting:
this.duration = duration.ToFloat();   //this would be the case where the same sort of information is tracked in different ways, perhaps one tracking it in milliseconds, using an integer and the other tracking it in seconds, using a floating-point number
ᛗᛁᛟᛚᚾᛁᚱPants Tell me all about it. 14:13, 7 February 2017 (UTC)[reply]

Thank all of you! Indeed, I added the dot in the end of the example by mistake --- Removed it now. 77.180.20.27 (talk) 15:45, 7 February 2017 (UTC)[reply]

@77.180.20.27: No problem. I've added some comments to the code blocks above to help clarify in case anything wasn't obvious. ᛗᛁᛟᛚᚾᛁᚱPants Tell me all about it. 16:12, 7 February 2017 (UTC)[reply]

How can computers represent real-world knowledge?

edit

In its more primitive form, how can computers represent a human concept? I can imagine that they use Venn diagrams, or represent it as a node in a network. What are other alternatives? --Llaanngg (talk) 18:47, 7 February 2017 (UTC)[reply]

(edit conflict)Well, on the most basic level, they represent it as a series of ones and zeros, known as bits. By grouping them together in groups of eight (known as a byte), they can represent numbers between 0 and 255. By grouping bytes together, they can represent larger and more complex numbers.
Then, they can take this huge selection of numbers and start using those numbers to represent things. For instance, a digital image is made of a grid of colored squares, known as pixels. Each pixel can represent a color with three bytes; one representing an amount of red light, one representing an amount of green light, and one representing an amount of blue light.
Simple text is easy to represent, too. You can fit all the letters in the English alphabet, all the numeric digits and punctuation marks we use, and quite few more into a single byte, by mapping each possible value of that byte to a different letter or punctuation mark. So in this way, a long string of numbers can become simply formatted text, with punctuation, spaces, line breaks and more.
There's a lot more to it than that, but you get the idea. You can represent almost anything with numbers, and you can represent numbers with electricity and switches. You might try the following articles for more information:
ᛗᛁᛟᛚᚾᛁᚱPants Tell me all about it. 19:13, 7 February 2017 (UTC)[reply]
(Not a big deal, but when you get an edit conflict, please post your comments below those that were saved while you were editing, so that chronological order of top-level comments is preserved, in accordance with WP:INDENT. Your response is very good, btw, thanks! I only happened to shoot higher because I remembered previous questions/discussions with OP, so I had a little more context. Cheers, SemanticMantis (talk) 22:12, 7 February 2017 (UTC) )[reply]
Cluster_analysis may be of interest. Also perhaps decision tree and Attribute-value_system. We have a general article on Knowledge_representation_and_reasoning, also information organization touches on this, as does ontology, and Ontology_(information_science). I can perhaps give you more directed refs if you clarify, but it looks like you're just looking for a survey of topics, so I just gave the first handful that came to mind. SemanticMantis (talk) 19:11, 7 February 2017 (UTC)[reply]
If SemanticMantis answer is more of what you're looking for, then I apologize for what might seem like condescension in mine. Without anything more specific than "human concepts" to describe, I assumed the question was about the fundamentals, not about more complex data structures. ᛗᛁᛟᛚᚾᛁᚱPants Tell me all about it. 19:15, 7 February 2017 (UTC)[reply]
You might want to look at relational database, but that article is heavy going. The relational model article describes, in plainer terms, how relation databases store the data. LongHairedFop (talk) 19:16, 7 February 2017 (UTC)[reply]
Yes, it was more like SemanticMantis than like MjolnirPants, but it's ok. I expressed myself poorly I suppose.
Is there any way a computer could use some kind of 'universe' to analyze the meaning of a text (in a human language)? That is, can it recreate the universe of discourse, some real-world representation, and add new information to it as it get more of it? Something like Second Life, but used by computers?Llaanngg (talk) 19:25, 7 February 2017 (UTC)[reply]
It depends on processing time, storage requirements, and the programming. For example, you can program a computer to analyze specific metrics about music. Then, you can program the computer to randomly create music that has similar metrics to the analyzed music. The randomly created music should be a new song that doesn't already exist, but should sound somewhat similar to the universe of music previously analyzed by the program. This technique has been done in many areas. Chatbots is one area that has expanded into writing entire thesis papers that read very much like a real thesis, but are actually nonsense. For more about current research in having computers generate music and art, see EvoMusArt 209.149.113.5 (talk) 19:46, 7 February 2017 (UTC)[reply]
That depends on what you mean by "meaning", but going with the usual meaning (pun intended, thank you) what we're talking about is Natural language parsing. In theory, of course it's possible for a computer to account for all of the contextual information that makes up human speech and writing and for it to draw a conclusion similar to a human from this input. But the number of variables involved in even a small amount of banter can be overwhelming to modern hardware. The machine would need to be able to first compare the text to all known idiomatic expressions, because human communication is often less than literal. But it couldn't assume that all idiomatic expressions were idiomatic (for example, if a text contained the phrase "jumped the gun" that probably means the subject started too soon, but it might mean that someone literally leaped over a gun, or perhaps they jumped over something known as "the gun"), so it would have to hold this in memory as it continued analyzing.
Next, it would have to parse the text for key words and phrase, then organize those by paragraph and sentence. Then it would need to compare any unknown words to a database of known proper names, acronyms and other non-dictionary words.
Then, it would need to do the same thing, but using a different method so as to compare the results. This is because there are metaphors and similes and analogies which we humans use all the time but computers don't get. So it would need to be certain that a certain statement didn't belong before it could then conclude that was some sort of metaphor.
Then, it would be time to start parsing metaphors. This would require an incredibly vast database of facts and descriptions of numerous events, objects, people, fictional characters, etc, etc.
Then, it could start comparing those possible idioms to what it's figured out already, checking to see if they now make literal sense within the context. They also need to be checked to make sure they make sense in their idiomatic meaning.
Of course, all of that parsing requires tons of processing. Some of the results (in the form of "Sentence N has meaning X") will be wrong, and will need to be changed by comparison to other results until a meaning is found for all sentences that is contextually fitting. And what is contextually fitting? Well, that itself would take a huge database of examples and information. I hope this helps, ᛗᛁᛟᛚᚾᛁᚱPants Tell me all about it. 20:26, 7 February 2017 (UTC)[reply]
Artificial intelligence often uses probabilities rather than TRUE or FALSE, for any given concept. For example, in diagnosis of a disease, it's rare that you can say symptom X means with 100% certainty that the patient has condition Y. More often, you arrive at probabilities for a range of conditions, based on a range of symptoms. StuRat (talk) 22:49, 7 February 2017 (UTC)[reply]