有一个单词列表和禁止单词列表。我想通过单词列表并编辑所有被禁止的单词。这就是我最终做的事情(请注意puts "Give input text:"
text = gets.chomp
puts "Give redacted word:"
redacted = gets.chomp
words = text.split(" ")
redacted = redacted.split(" ")
catched = false
words.each do |word|
redacted.each do |redacted_word|
if word == redacted_word
catched = true
print "REDACTED "
break
end
end
if catched == true
catched = false
else
print word + " "
end
end
布尔值):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button2_Click(object sender, EventArgs e)
{
DropDownList1.Items.Add(TextBox3.Text);
}
protected void Button3_Click(object sender, EventArgs e)
{
if (Panel1.Visible == true)
Panel1.Visible = false;
}
protected void Button1_Click(object sender, EventArgs e)
{
if (Panel1.Visible == false)
Panel1.Visible = true;
}
protected void Button4_Click(object sender, EventArgs e)
{
TableCell tdStudentName = new TableCell();
tdStudentName.Text = TextBox1.Text;
TableCell tdStudentCourse = new TableCell();
tdStudentCourse.Text = DropDownList1.SelectedValue;
TableCell tdStudentGrade = new TableCell();
tdStudentGrade.Text = TextBox2.Text;
TableRow tr = new TableRow();
tr.Cells.Add(tdStudentName);
tr.Cells.Add(tdStudentCourse);
tr.Cells.Add(tdStudentGrade);
Table1.Rows.Add(tr);
}
}
}
有没有合适/有效的方式?
答案 0 :(得分:10)
它也可以。
words - redacted
+
,-
,&
,这些方法非常简单实用。
irb(main):016:0> words = ["a", "b", "a", "c"]
=> ["a", "b", "a", "c"]
irb(main):017:0> redacted = ["a", "b"]
=> ["a", "b"]
irb(main):018:0> words - redacted
=> ["c"]
irb(main):019:0> words + redacted
=> ["a", "b", "a", "c", "a", "b"]
irb(main):020:0> words & redacted
=> ["a", "b"]
答案 1 :(得分:7)
您可以使用.reject
排除words.reject {|w| redacted.include? w}
数组中存在的所有被禁词:
words
如果您想获取words.select {|w| redacted.include? w}
数组中存在的禁止字词列表,可以使用.select
:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body onload="geolocate1()">
<input type="text" id="pickup" onFocus="geolocate()" placeholder="Enter your pick up place" />
<input type="text" id="plat" value="" id="plat"/>
<input type="text" id="plang" value="" id="plang"/>
<input type="text" id="pstatuslat" value="error" />
<input type="text" id="km" />
</br>
</br>
<input type="text" id="drop" onFocus="geolocate1()" placeholder="Enter your Drop off place"/>
<input type="text" id="dlat" value="" id="plat"/>
<input type="text" id="dlang" value="" id="plang"/>
<input type="text" id="dstatuslat" value="error" />
<div id="directions_panel" style="margin:20px;background-color:#FFEE77;"></div>
<input type="button" onclick="GetLocation1();GetLocation();calcRoute();" value="Book Now" />
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=places"></script>
<script type="text/javascript">
<!--
function GetLocation() {
var geocoder = new google.maps.Geocoder();
var address = document.getElementById("pickup").value;
geocoder.geocode({ 'address': address }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var latitude = results[0].geometry.location.lat();
var longitude = results[0].geometry.location.lng();
document.getElementById('plang').value= longitude;
document.getElementById('plat').value= latitude;
document.getElementById('pstatuslat').value= "ok";
} else {
document.getElementById('pstatuslat').value= "error";
}
});
};
function GetLocation1() {
var geocoder = new google.maps.Geocoder();
var address = document.getElementById("drop").value;
geocoder.geocode({ 'address': address }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var latitude = results[0].geometry.location.lat();
var longitude = results[0].geometry.location.lng();
document.getElementById('dlang').value= longitude;
document.getElementById('dlat').value= latitude;
document.getElementById('dstatuslat').value= "ok";
} else {
document.getElementById('dstatuslat').value= "error";
}
});
};
//-->
// This example displays an address form, using the autocomplete feature
// of the Google Places API to help users fill in the information.
var autocomplete;
function geolocate1() {
// Create the autocomplete object, restricting the search
var input = document.getElementById('pickup');
var options = {types: ["geocode"],componentRestrictions: {country: 'uk'}};
autocomplete = new google.maps.places.Autocomplete(input, options);
// When the user selects an address from the dropdown,
// populate the address fields in the form.
google.maps.event.addListener(autocomplete, 'place_changed', function() {
fillInAddress();
});
}
function geolocate() {
// Create the autocomplete object, restricting the search
// to geographical location types.
var input = document.getElementById('drop');
var options = {types: ["geocode"],componentRestrictions: {country: 'uk'}};
autocomplete = new google.maps.places.Autocomplete(input, options);
// When the user selects an address from the dropdown,
// populate the address fields in the form.
google.maps.event.addListener(autocomplete, 'place_changed', function() {
fillInAddress();
});
}
var directionsService = new google.maps.DirectionsService();
function calcRoute() {
var start = document.getElementById('pickup').value;
var end = document.getElementById('drop').value;
var request = {
origin: start,
destination: end,
travelMode: google.maps.TravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
var route = response.routes[0];
var km = parseFloat(route.legs[0].distance.text.replace(" km", ""));
document.getElementById('km').value = km * 0.6214;
}
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</body>
</html>
答案 2 :(得分:1)
这可能更“优雅”。无论是效率还是低于您的解决方案,我都不知道。
puts "Give input text:"
original_text = gets.chomp
puts "Give redacted word:"
redacted = gets.chomp
redacted_words = redacted.split
print(
redacted_words.inject(original_text) do |text, redacted_word|
text.gsub(/\b#{redacted_word}\b/, 'REDACTED')
end
)
那么这里发生了什么?
String#split
而没有参数,因为' '
is the default无论如何。Array#inject
,对阵列中的每个元素执行以下块(盯着do
并结束end
- 在这种情况下,我们的禁用词列表。
original_text
)。String#gsub
执行 g 小册子子纪律/.../
)。除此之外,它并不是真正的字面值,因为我正在对其执行字符串替换(#{...}
)以将当前处理的编辑字汇入其中。\b
word boundary matchers进行编辑的单词。它们匹配字母数字和非字母数字字符(或副verca)之间的边界,而不匹配任何字符本身。 (它们匹配字符之间的零长度'位置。)如果字符串以字母数字字符开头或结尾,\b
也将分别匹配字符串的开头或结尾,以便我们可以使用它匹配整个单词。inject
的结果(这是最后一次执行该块的结果,即所有替换发生时的文本)作为参数传递给print
,这将是输出现在编辑的文本。注意,除了您的解决方案之外,我不会将标点符号视为相邻字词的一部分。
同样注意我的解决方案很容易受到正则表达式的注入。
Give input text:
A fnord is a fnord.
Give redacted word:
ford fnord foo
我的输出:
A REDACTED is a REDACTED.
你的输出:
A REDACTED is a fnord.
Give input text:
A fnord is a fnord.
Give redacted word:
fnord.
我的输出:
A REDACTEDis a fnord.
(注意.
如何被解释为匹配任何角色。)
你的输出:
A fnord is a REDACTED.