我正在Android Studio中开发Android社交应用
任何人都可以知道如何在发布新状态时过滤坏词/冒犯性词语。
例如,我发布了一些不好的词语和令人反感的内容,它会变成这样的" ****" 当我发布很多单词时它会变成这样的"我现在就打你的*****"
如果你知道我真的需要它,请 先谢谢你。
这是我的 PublishingActivity.java 代码
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.iscooldev.socialnetwork.R;
import com.iscooldev.socialnetwork.api.APIService;
import com.iscooldev.socialnetwork.api.GlobalAPI;
import com.iscooldev.socialnetwork.api.PostsAPI;
import com.iscooldev.socialnetwork.api.UsersAPI;
import com.iscooldev.socialnetwork.app.AppConst;
import com.iscooldev.socialnetwork.data.LocationModel;
import com.iscooldev.socialnetwork.data.ResponseModel;
import com.iscooldev.socialnetwork.data.userItem;
import com.iscooldev.socialnetwork.helpers.CacheManager;
import com.iscooldev.socialnetwork.helpers.CropSquareTransformation;
import com.iscooldev.socialnetwork.helpers.FilePath;
import com.iscooldev.socialnetwork.helpers.GPSHelper;
import com.iscooldev.socialnetwork.helpers.M;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.squareup.picasso.Picasso;
import com.thin.downloadmanager.DownloadRequest;
import com.thin.downloadmanager.ThinDownloadManager;
import java.io.File;
import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;
import retrofit.mime.TypedFile;
public class PublishActivity extends AppCompatActivity implements OnClickListener, DialogInterface.OnClickListener {
public Intent mIntent;
public LinearLayoutManager layoutManager;
EditText insertedLink;
private ImageView mImagePreview;
private ImageButton addPhoto;
private ImageButton sendStatus;
private ImageButton changePrivacy;
private EditText statusInput;
private TextView profileName, postPrivacy;
private ImageView profilePicture;
private String privacy = "public";
private String statusValue = null;
private String linkValue = null;
private Uri imageUriValue = null;
private LinearLayout urlPreviewLayout;
private TextView urlValuePreview;
private TextView placeValuePreview;
private LinearLayout placePreviewLayout;
private String placeValue = null;
private ThinDownloadManager downloadManager;
private CacheManager mCacheManager;
private Gson mGson;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (M.getToken(this) == null) {
Intent mIntent = new Intent(this, LoginActivity.class);
startActivity(mIntent);
finish();
} else {
mCacheManager = CacheManager.getInstance(this);
mGson = new Gson();
setContentView(R.layout.activity_publish);
initializeView();
if (getIntent().hasExtra(Intent.EXTRA_SUBJECT)) {
setStatusValue(getIntent().getExtras().get(Intent.EXTRA_SUBJECT).toString());
}
if (getIntent().hasExtra(Intent.EXTRA_TEXT)) {
String text = getIntent().getExtras().get(Intent.EXTRA_TEXT).toString();
if (M.isValidUrl(text)) {
setLinkValue(text);
} else {
setStatusValue(text);
}
}
if (getIntent().hasExtra(Intent.EXTRA_STREAM)) {
setImageUriValue((Uri) getIntent().getExtras().get(Intent.EXTRA_STREAM));
M.L(getIntent().getType());
}
getUser();
}
}
public void initializeView() {
downloadManager = new ThinDownloadManager(AppConst.DOWNLOAD_THREAD_POOL_SIZE);
addPhoto = (ImageButton) findViewById(R.id.addPhoto);
sendStatus = (ImageButton) findViewById(R.id.sendStatus);
changePrivacy = (ImageButton) findViewById(R.id.changePrivacy);
mImagePreview = (ImageView) findViewById(R.id.imagePreview);
statusInput = (EditText) findViewById(R.id.statusEdittext);
profilePicture = (ImageView) findViewById(R.id.postOwnerImage);
profileName = (TextView) findViewById(R.id.postOwnerName);
postPrivacy = (TextView) findViewById(R.id.postPrivacy);
TextView removeLink = (TextView) findViewById(R.id.removeLink);
TextView removePlace = (TextView) findViewById(R.id.removePlace);
ImageButton addPlace = (ImageButton) findViewById(R.id.addPlace);
ImageButton addLink = (ImageButton) findViewById(R.id.addLink);
placePreviewLayout = (LinearLayout) findViewById(R.id.placePreviewLayout);
placeValuePreview = (TextView) findViewById(R.id.placeValuePreview);
urlPreviewLayout = (LinearLayout) findViewById(R.id.urlPreviewLayout);
urlValuePreview = (TextView) findViewById(R.id.urlValuePreview);
sendStatus.setOnClickListener(this);
addPhoto.setOnClickListener(this);
changePrivacy.setOnClickListener(this);
addPlace.setOnClickListener(this);
addLink.setOnClickListener(this);
removePlace.setOnClickListener(this);
removeLink.setOnClickListener(this);
//including toolbar and enabling the home button
Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle(getString(R.string.title_publish));
}
private void getUser() {
if (M.isNetworkAvailable(this)) {
UsersAPI mUsersAPI = APIService.createService(UsersAPI.class, M.getToken(this));
mUsersAPI.getUser(0, new Callback<userItem>() {
@Override
public void success(userItem user, retrofit.client.Response response) {
try {
mCacheManager.write(mGson.toJson(user), "Profile-0.json");
updateView(user);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void failure(RetrofitError error) {
}
});
} else {
try {
updateView((userItem) mGson.fromJson(mCacheManager.readString("Profile-0.json"), new TypeToken<userItem>() {
}.getType()));
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void updateView(userItem user) {
if (user.getName() != null) {
profileName.setText(user.getName());
} else {
profileName.setText(user.getUsername());
}
if (getFilePath(user.getPicture()) != null) {
Picasso.with(getApplicationContext())
.load(getFilePath(user.getPicture()))
.transform(new CropSquareTransformation())
.placeholder(R.drawable.image_holder)
.error(R.drawable.image_holder)
.into(profilePicture);
} else {
Picasso.with(getApplicationContext())
.load(AppConst.IMAGE_PROFILE_URL + user.getPicture())
.transform(new CropSquareTransformation())
.placeholder(R.drawable.image_holder)
.error(R.drawable.image_holder)
.into(profilePicture);
downloadFile(AppConst.IMAGE_PROFILE_URL + user.getPicture(), user.getPicture());
}
}
private void downloadFile(String url, String hash) {
if (getFilePath(hash) == null) {
Uri downloadUri = Uri.parse(url);
Uri destinationUri = Uri.parse(M.getFilePath(getApplicationContext(), hash));
DownloadRequest downloadRequest = new DownloadRequest(downloadUri)
.setDestinationURI(destinationUri);
downloadManager.add(downloadRequest);
}
}
private String getFilePath(String hash) {
return M.filePath(getApplicationContext(), hash);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == AppConst.SELECT_PICTURE) {
setImageUriValue(data.getData());
}
}
}
@Override
public void onClick(final View v) {
if (v.getId() == R.id.removePlace) {
setPlaceValue(null);
} else if (v.getId() == R.id.removeLink) {
setLinkValue(null);
} else if (v.getId() == R.id.addPlace) {
final GPSHelper mGpsHelper = new GPSHelper(this);
if (mGpsHelper.canGetLocation()) {
GlobalAPI mGlobalAPI = APIService.createService(GlobalAPI.class, M.getToken(this));
mGlobalAPI.getCurrentPlace(mGpsHelper.getLatitude(), mGpsHelper.getLongitude(), new Callback<LocationModel>() {
@Override
public void success(LocationModel location, retrofit.client.Response response) {
if (location.isStatus()) {
setPlaceValue(location.getAddress());
} else {
mGpsHelper.showSettingsAlert();
}
}
@Override
public void failure(RetrofitError error) {
mGpsHelper.showSettingsAlert();
}
});
} else {
mGpsHelper.showSettingsAlert();
}
} else if (v.getId() == R.id.addLink) {
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle(getString(R.string.Insert_a_link));
insertedLink = new EditText(this);
insertedLink.setText("http://");
alert.setView(insertedLink);
alert.setPositiveButton(getString(R.string.ok), this);
alert.setNegativeButton(getString(R.string.cancel), this);
alert.show();
} else if (v.getId() == addPhoto.getId()) {
launchImageChooser();
} else if (v.getId() == sendStatus.getId()) {
String statusText = statusInput.getText().toString().trim();
if (!statusText.isEmpty()) {
setStatusValue(statusText);
}
if (getStatusValue() == null && getImageUriValue() == null && getLinkValue() == null) {
M.T(v,
getString(R.string.Error_empty_post));
} else {
PostsAPI mPostsAPI = APIService.createService(PostsAPI.class, M.getToken(this));
TypedFile image = null;
if (getImageUriValue() != null) {
image = new TypedFile("image/jpg", new File(FilePath.getPath(this, getImageUriValue())));
}
M.showLoadingDialog(this);
mPostsAPI.publishPost(image, getStatusValue(), getLinkValue(), getPlaceValue(), privacy, new Callback<ResponseModel>() {
@Override
public void success(ResponseModel responseModel, Response response) {
M.hideLoadingDialog();
M.T(v, responseModel.getMessage());
if (responseModel.isDone()) {
startActivity(new Intent(PublishActivity.this, MainActivity.class));
finish();
}
}
@Override
public void failure(RetrofitError error) {
M.hideLoadingDialog();
M.T(v, getString(R.string.ServerError));
}
});
}
} else if (v.getId() == changePrivacy.getId()) {
if (privacy.equals("public")) {
postPrivacy.setText(R.string.privatePrivacy);
privacy = "private";
M.T(v, getString(R.string.changed_to_private));
} else {
postPrivacy.setText(R.string.publicPrivacy);
privacy = "public";
M.T(v, getString(R.string.changed_to_public));
}
}
}
private void launchImageChooser() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(
Intent.createChooser(intent, "Choose An Image"),
AppConst.SELECT_PICTURE);
}
@Override
public void onClick(DialogInterface dialog, int which) {
String Link = insertedLink.getText().toString();
if (!Link.equals("http://") && !Link.equals("")) {
setLinkValue(Link);
}
}
public String getPlaceValue() {
return placeValue;
}
public void setPlaceValue(String placeValue) {
if (placeValue != null) {
if (placePreviewLayout.getVisibility() != View.VISIBLE) {
placePreviewLayout.setVisibility(View.VISIBLE);
placeValuePreview.setText(placeValue);
}
} else {
if (placePreviewLayout.getVisibility() != View.GONE) {
placePreviewLayout.setVisibility(View.GONE);
placeValuePreview.setText("");
}
}
this.placeValue = placeValue;
}
public Uri getImageUriValue() {
return imageUriValue;
}
public void setImageUriValue(Uri imageUriValue) {
this.imageUriValue = imageUriValue;
mImagePreview.setImageURI(imageUriValue);
if (mImagePreview.getVisibility() != View.VISIBLE) {
mImagePreview.setVisibility(View.VISIBLE);
}
}
public String getLinkValue() {
return linkValue;
}
public void setLinkValue(String linkValue) {
if (linkValue != null) {
if (urlPreviewLayout.getVisibility() != View.VISIBLE) {
urlPreviewLayout.setVisibility(View.VISIBLE);
urlValuePreview.setText(linkValue);
}
} else {
if (urlPreviewLayout.getVisibility() != View.GONE) {
urlPreviewLayout.setVisibility(View.GONE);
urlValuePreview.setText("");
}
}
this.linkValue = linkValue;
}
public String getStatusValue() {
return statusValue;
}
public void setStatusValue(String statusValue) {
String statusInputValue = statusInput.getText().toString().trim();
if (!statusValue.equals(statusInputValue)) {
String finalStatus;
if (TextUtils.isEmpty(statusInputValue)) {
finalStatus = statusValue;
} else {
finalStatus = statusInputValue + " " + statusValue;
}
statusInput.setText(finalStatus);
this.statusValue = finalStatus;
} else {
this.statusValue = statusValue;
}
}
}
答案 0 :(得分:2)
假设您有一个输入字符串dataview v=dt.defaultview; // ( dt datatable)
v.sort="columnName ASC"; // ASC / DESC for ascending / descending order
dt=v.toTable();
以及要用星号替换的单词列表:
s
您只需迭代令人反感的单词列表,查看输入字符串是否包含一个,并执行替换。最简单的方法是使用正则表达式:
String s = "i will hit your head right now";
List<String> words = Arrays.asList("head", "now"); // suppose these words are offensive
我在正则表达式模式的前端和末尾添加for (String word : words) {
Pattern rx = Pattern.compile("\\b" + word + "\\b", Pattern.CASE_INSENSITIVE);
s = rx.matcher(s).replaceAll(new String(new char[word.length()]).replace('\0', '*'));
}
// s: i will hit your **** right ***
以匹配整个单词(即“head”将被处理,“header”将不会)。而这篇文章:
\\b
构建一个新的new String(new char[n]).replace('\0', c)
字符n
字符串。
之后,在您需要的地方使用生成的c
。
答案 1 :(得分:0)
这是一个关于java的基本知识,而不是与android相关的。这样做的一种方法是列出坏词并将其替换为用户输入的字符串。请参阅下面的示例
String userInput = "this is an example of some bad words";
ArrayList<String> badWords = new ArrayList<>();
badWords.add("some");
badWords.add("bad");
badWords.add("words");
for (int i = 0; i < badWords.size(); i++) {
String badWord = badWords.get(i);
if(userInput.toLowerCase().contains(badWord)){
userInput = userInput.replace(badWord, "*****");
}
}