How to detect abusive comments on my website? - c#

I have my website where I have given users the opportunity to share their status. How can I detect that if any abusive or slang words are used so as to block such comments?
Is there any library or trick to detect such kind of comments in .NET?

It is not a trick; use a dictionary of bad words, and add some logic to detect "bad words" in good places. Add the ability for users to post complains about mis-correction of your logic (so you can fine tune it) and that's it.
Implementation is pretty easy, and a dictionary of "bad words" - either look it up, or write one your own.
(I used to collect bad words from customer complains on a chat service - after a year it was almost bulletproof.)

This is actually quite difficult to automate and do accurately without unintended side effects. You can maintain a dictionary of bad words, and use regular expressions to replace occurrences of those bad words. Please see my answer to the following question for example code, plus some of the issues:
Replace Bad words using Regex
Automated approaches have a number of shortcommings: false positives, missing bad words that are not in the dictionary, and minor variations of bad words that are not detected. Involvement from users can be used to bolster or as an alternative approach e.g SO has the abiliy to flag comments and moderators can delete or censor them.

There are some bad word lists around which you can download and use.
eg. http://urbanoalvarez.es/blog/2008/04/04/bad-words-list/
The best thing to do is to start with a small list and add to it based on the real comments made on your site. You can put a report link on the comments so other visitors can notify you if there are bad comments made.

Related

C# how to find the state someone was born in with their social security number

I am currently working on an assignment in which I am to validate various formats using regular expressions (phone numbers, birth date, email address, Social Security). One of the features our teacher has suggested would be to have a method that returns the state an individual was born using their Social Security Number.
xxx-xx-xxxx
The first 3 digits correspond to a state/area as outlined here:
http://socialsecuritynumerology.com/prefixes.php
If I've isolated the first 3 numbers as an integer already, is there anyway I could quickly match the number with its corresponding area code?
Currently I'm only using if-else statements but its getting pretty tedious.
Example:
if (x > 0 && x<3)
return "New Hampshire";
else if (x <= 7)
return "Maine";
...
You have a few options here:
50 if statements, one for each state, as you are doing.
A switch with 999 conditions, matching each option with a state. It probably looks cleaner and you can generate it with a script and interject the return statements wherever necessary. Maybe worse than option 1 in terms of tediousness.
Import the file as text, parse it into a Dictionary and do a simple lookup. The mapping is most likely not going to change in the near future, so the robustness argument is rather moot, but it is probably "simpler" in terms of amount of effort*. And it's another chance to practice regex to parse lines in the file you linked.
*Where "effort" is measured purely in the amount of tedious gruntwork prone to annoying human error and hand fatigue. Energy consumed within the brain due to engineering and implementing a solution where the computer does the ugly stuff for you is not included. :)
It's hard to tell your level of skill and what your course has taught you so far which is why it's difficult answering these kinds of questions, and also for the most part that's why you will get a negative response from people - they assume that you would have had the answer in your course materials already and will assume that you are being lazy.
I'm going to assume that you are at a basic level and that you already know how to solve the problem the brute force way (your if/else construct) and that you are genuinely interested in how to make your code better and not simply asking for a solution you can copy/paste.
Now, while your if/else idea will work, that is a procedural way of thinking. You are working with an object oriented language, so I would suggest to you to think about how you could use the principles of OO to make this work better. A good starting point would be to make a collection of state objects that contain all the parameters you need. You could then loop through your state collection and use their properties to find the matching one. You could create the state collection by reading from a file or database or even just hard coding it for the purposes of your assignment.
Your intuition that a long chain of if and else if statements might be unwieldy is sound. Not only is it tedious, but the search to find the correct interval is inefficient. However, something needs to be in charge of this tedium. A simple solution would be to use a Dictionary to store key/value pairs that you could build up once, and reuse throughout the application. This has the downside of requiring more space than necessary, as every individual mapping becomes an element of the data structure. You could instead follow the advice from this question and use a data structure more suited to ranged values for look ups.
It's difficult to tell from your question as it's written what your level of expertise is, and going into any real detail here would essentially mean completing your assignment for you. It should be noted though, that there's nothing inherently wrong with your solution, and depending on where you are in your education it may be what's expected.

Parse numbers from large text, possibly without regex (performance critical)

I'm extremely familiar with regex before you all start answering with variations of: /d+
I want to know if there are alternatives to regex for parsing numbers out of a large text file.
I'm parsing through tons of huge files and need to do some group/location analysis on the positions of keywords. I'm now at the point where i need to start finding groups of numbers as well nested closely to my content of interest. I want to avoid regex if at all possible because this needs to be a speedy process.
It is possible to take chunks of a file to inspect for the numbers of interest. That however would require more work and add hard coded limits for searching. (i'd like to avoid this)
I'm open to any suggestions.
UPDATE
Sorry for the lack of sample data. For HIPAA reasons I'd rather not even consider scrambling the text and posting it.
A great substitute would be the HTML source of any stackoverflow.com question page. Imagine I needed to grab the reputation (score) of all people that posted an answer to a question. This also means that the comma (,) is needed as well. I can't remove the html to simplify the content because I'm using some density analysis to weed out unrelated content. Removing the HTML would mix content too close together.
Unless the file is some sort of SGML, then I don't know of any method (which is not to say there isn't, I just don't know of one)
However, it's not to say that you can't create your own parser; you could eliminate some of the overheads of the .Net regex library by writing something that only finds ranges of numbers.
Fundamentally, I guess that that's all any library would do, at the most basic level.
Might help if you can post a sample of the sort of data you'll be processing?

Sentence generator using Thesaurus

I am creating an application in .NET.
I got a running application name http://www.spinnerchief.com/. It did what I needed it to do but but I did not get any help from Google.
I need functional results for my application, where users can give one sentence and then the user can get the same sentence, but have it worded differently.
Here is an example of want I want.
Suppose I put a sentence that is "Pankaj is a good man." The output should be similar to the following one:
Pankaj is a great person.
Pankaj is a superb man.
Pankaj is a acceptable guy.
Pankaj is a wonderful dude.
Pankaj is a superb male.
Pankaj is a good human.
Pankaj is a splendid gentleman
To do this correctly for any arbitrary sentence you would need to perform natural language analysis of the source sentence. You may want to look into the SharpNLP library - it's a free library of natural language processing tools for C#/.NET.
If you're looking for a simpler approach, you have to be willing to sacrifice correctness to some degree. For instance, you could create a dictionary of trigger words, which - when they appear in a sentence - are replaced with synonyms from a thesaurus. The problem with this approach is making sure that you replace a word with an equivalent part of speech. In English, it's possible for certain words to be different parts of speech (verb, adjective, adverb, etc) based on their contextual usage in a sentence.
An additional consideration you'll need to address (if you're not using an NLP library) is stemming. In most languages, certain parts of speech are conjugated/modified (verbs in English) based on the subject they apply to (or the object, speaker, or tense of the sentence).
If all you want to do is replace adjectives (as in your example) the approach of using trigger words may work - but it won't be readily extensible. Before you do anything, I would suggest that you clearly defined the requirements and rules for your problem domain ... and use that to decide which route to take.
For this, the best thing for you to use is WordNet and it's hyponym/hypernym relations. There is a WordNet .Net library. For each word you want to alternate, you can either get it's hypernym (i.e. for person, a hypernym means "person is a kind of...") or hyponym ("X is a kind of person"). Then just replace the word you are alternating.
You will want to make sure you have the correct part-of-speech (i.e. noun, adjective, verb...) and there is also the issue of senses, which may introduce some undesired alternations (sense #1 is the most common).
I don't know anything about .Net, but you should look into using a dictionary function (I'm sure there is one, or at least a library that streamlines the process if there isn't).
Then, you'd have to go through the string, and ommit words like "is" or "a". Only taking words you want to have synonyms for.
After this, its pretty simple to have a loop spit out your sentences.
Good luck.

C# - Show the differences when comparing strings

In my asp.net project, I have two strings (actually, they are stored in a Session object, then i do a .ToString() )
This project is part of my free Japanese language exercises on my website (Italian only for now, so i won't link/spam)
For now i do an if (original == inputted.ToLower()) , but I would like to compare the strings and highlight the differences on the screen
like this:
original: hiroyashi
wrote by user: hiroyoshi
i was thinking to compare the two strings and save the differences in another variable, with HTML tags, and then show it on a Literal control... but... if the differences are many, or the input is shorter... how to do that?
It looks there is the needing of an huge amount of coding... or not?
I seem to remember someone asking this not too long ago, and essentially they were pointed at difference engines.
A quick search on codeplex brings up:
http://www.codeplex.com/site/search?projectSearchText=diff
May be worth a hunt through some of those that come up - you may be able to plug something into your existing code?
Cheers,
Terry
John Resig wrote a javascript diff algorithm, but he's removed the page explaining what it does from his site. It's still available through the google cache though. Apologies if linking that is bad John. It should do what you want, someone else took it, tweaked it and put an article up about it here - complete with a test page
I am not sure if this would be helpful, but this is a way I would do:
I would use a hashmap, and store all words seperate by space there.
Then using that I would map with the original.
You can add html tags or whatever if they are different.
There is bound to be a performance issue here on a large dictionary of words
The coding itself would not be long though.

Filter out common words for search query

Are there any easy ways to implement filtering a user's input (possibly a question) by extracting the meaningful data in the query?
I basically want to filter out any noise words so I can send a 'clean' query to Google's search api.
Um, won't Google do this for you? Send all those dirty, filthy words to Google and let them clean them up for you.
Jeff talked about "stop words" in one of the previous stackoverflow podcasts. You might try searching for that phrase on google. The wikipedia page seems to have some overview and pointers to options.
http://en.wikipedia.org/wiki/Stop_words
You can try removing the top X most common English words, but you will always run into trouble with a naive approach like this.
This is because common English words can have special significance in the realm of Computer Science (or other areas). A recent SO podcast (#32) mentions this very issue.
I used the stop words approach when implementing a basic search engine and it worked fine.
Try a sample list like the one here
Based on feedback from your users, you can modify your stop word list accordingly.

Categories