PHP中的类似文本(similar text in PHP)
我有这样的PHP数组
$array = array("foo", "bar", "hallo", "world", "fooo", "bar1", "hall_o", "wor1ld", "foo", "bard", "hzallo", "w44orld");
我想比较一个数组的每个元素与剩余的元素。
例如:我想
"foo" with "bar", "hallo", "world", "fooo", "bar1", "hall_o", "wor1ld", "foo", "bard", "hzallo" and "w44orld"
。然后,我想
"bar" with "foo", "hallo", "world", "fooo", "bar1", "hall_o", "wor1ld", "foo", "bard", "hzallo", "w44orld"
等等,直到最后一个元素。让我们考虑元素,我们将其作为
$var_1
和剩余元素的变量作为$ var_2进行比较; 如果similar_text($var_1, $var_2, $percent);
返回$percent value > 90%
然后我想打印$var_1
和$var_2
所有相应类似文本值,其匹配百分比> 90目前我计划使用两个循环来实现这一点,
$var_1
外部循环和$var_2
内部循环。array
每个元素最多可以包含5000个字符,并且array
可以有1000个元素,因此我当前的逻辑非常昂贵。任何方向以更好的方式处理它?
I've PHP array something like this
$array = array("foo", "bar", "hallo", "world", "fooo", "bar1", "hall_o", "wor1ld", "foo", "bard", "hzallo", "w44orld");
I want to compare each element of an array with remaining elements.
Ex: I want to compre
"foo" with "bar", "hallo", "world", "fooo", "bar1", "hall_o", "wor1ld", "foo", "bard", "hzallo" and "w44orld"
.Then, I want to compre
"bar" with "foo", "hallo", "world", "fooo", "bar1", "hall_o", "wor1ld", "foo", "bard", "hzallo", "w44orld"
and so on till last element.Let's consider element, which we are comparing as
$var_1
and variable for remaining elements as $var_2; Ifsimilar_text($var_1, $var_2, $percent);
returns$percent value > 90%
then I want to print$var_1
and all corresponding similar text values of$var_2
for which matching percentage > 90Currently I'm planning to use two loops to achieve this, external loop for
$var_1
and internal loop for$var_2
. Each element of thearray
can have value upto 5000 characters and there can be 1000 elements in a array, so my current logic is very expensive.Any direction to handle it in better way?
原文:
最满意答案
我有一个类似的情况,我使用PropertyChangeSupport来听取模型的变化。 我相信最好的方法是创建一个AbstractEntity,它包含一个私有PropertyChangeSupport和两个公共方法addPropertyListener,removePropertyListener和一个受保护的方法firePropertyChange 。 这些方法将用作PropertyChangeSupport的包装器。 所以你的控制器应该只是addPropertyListeners来听取常见模型的变化。
注意:
- 您应该在所有控制器中使用相同的模型实例。
- 您需要的类如下:
- java.beans.PropertyChangeSupport
- 的java.beans.PropertyChangeListener
示例代码如何 public void setValue(String value){ String oldValue=getValue(); this.value=value; firePropertyChange("value",oldValue,getValue()); }
I had a similar case where I used PropertyChangeSupport in order to listen to model's changes. I believe that the best way is to create an AbstractEntity that contains a private PropertyChangeSupport and two public methods addPropertyListener, removePropertyListener and a protected method firePropertyChange. Those methods will be used as wrappers of the PropertyChangeSupport's ones. So your controllers should just addPropertyListeners in order to listen to changes of the common model.
Note:
- You should use the same instance of model accross all the controlers.
- The classes that you need are the following:
- java.beans.PropertyChangeSupport
- java.beans.PropertyChangeListener
Example code for how the public void setValue(String value){ String oldValue=getValue(); this.value=value; firePropertyChange("value",oldValue,getValue()); }
相关问答
更多-
首先通常使用Observable是一个坏主意,因为你需要扩展这个类来使用它。 无论如何,你所描述的是观察实体的聚合。 不要让简单的事情复杂化。 让每个实体都可观察并让它处理注册和通知。 您不必特别包含名为Model的类来聚合所有内容。 打破你的设计。 First of all usually it is a bad idea to use Observable since you need to extend this class to use it. In any case what you are de ...
-
你有一个学生和MessageBoard的具体例子。 学生通过将自己添加到希望在将新消息发布到MessageBoard时被通知的观察者列表中。 当Message添加到MessageBoard时,它会遍历其ObserBS列表,并通知他们事件发生。 认为Twitter 当你说你想跟随某人时,Twitter会把你添加到他们的追随者列表中。 当他们发送一个新的推文时,你会看到它在你的输入。 在这种情况下,您的Twitter帐户是Observer,您所关注的人是Observable。 比喻可能并不完美,因为Twitte ...
-
这是我的评论作为答案:) 我猜你的notifyObservers方法会调用update吗? 您将一组对象传递给这些更新方法,并尝试将此数组转换为类型。 我认为你应该把它投射到一个对象数组中。 应该像Object[] values = (Object[])arg Here's my comment as an answer :) update is being called by your notifyObservers method, I guess? You're passing an array of ...
-
如何使用Observable java(How to use Observable java)[2023-08-11]
扩展您的界面: void update(Object sender, String nn, double bbb, double aaa, String nameThraed); 在您的特殊情况下,您可以检查发件人是否是您的班级E的实例。 if(sender instanceof E){ ... } 希望这会帮助你。 请重构你的参数:) Extend your interface: void update(Object sender, String nn, double bbb, double aaa, ... -
Java内置观察者模式(Java Built-in Observer pattern)[2022-04-10]
如果要求对象是可序列化的,则它将在接口定义中声明为这样。 如: public void update(Observable observable, final Serializable observation) 但事实并非如此,因此不存在这样的要求。 If there was a requirement for the object to be serializable, it would have been declared as such in the interface definition. A ... -
观察者模式具有特定的“可观察/订户”关系(Observer pattern that has specific “observable/subscriber” relationship)[2023-10-31]
好主意! 这种模式非常有用,IObservable是一个内置的.Net接口。 MSDN文档提供了更多详细信息。 您的用户意见由IObserver代表,也记录在MSDN上 。 IObserver和IObservable接口为基于推送的通知提供了一种通用机制,也称为观察者设计模式。 IObservable接口表示发送通知的类(提供者); IObserver接口表示接收它们的类(观察者)。 T表示提供通知信息的类[数据传输对象] ... Good idea! This pattern's so useful, I ... -
基本应用程序架构 - JAVA中的基本观察者/可观察者(Basic App Architecture - Basic Observer / Observable in JAVA)[2021-12-26]
如上所述, Swing有自己的数据模型,通过管理和触发Listener来实现一些“观察者模式”。 请参阅: https : //docs.oracle.com/javase/tutorial/uiswing/components/list.html#creating 您可能想要创建和更新此类模型,并在更新后触发必要的侦听器。 As mentioned, Swing has its own data models, that implements some of the "Observer pattern" ... -
我有一个类似的情况,我使用PropertyChangeSupport来听取模型的变化。 我相信最好的方法是创建一个AbstractEntity,它包含一个私有PropertyChangeSupport和两个公共方法addPropertyListener,removePropertyListener和一个受保护的方法firePropertyChange 。 这些方法将用作PropertyChangeSupport的包装器。 所以你的控制器应该只是addPropertyListeners来听取常见模型的变化。 ...
-
是的,实施方式不同。 但! 模式是概念,而不是实现。 而且这个概念是一样的。 在这两种情况下,观察者都会参考观察到的项目,但你绝对可以想象观察者根本不需要这个参考的情况。 因此,这里的概念是“当某些东西改变状态或以某种方式执行时会收到通知”,并且它比任何实现都要广泛得多。 Yes, the implementations differ. BUT! The pattern is the concept, not the implementation. And the concept is the same. ...
-
notifyObservers()仅在发生更改后才会起作用 (由hasChanged()方法验证)。 在您的代码中,您需要添加setChanged()以便在调用notify之前设置更改指示符。 之后您不需要调用clearChanged() ,因为notifyObservers方法会自动调用此方法 。 notifyObservers() acts only after a change occurs (as verified by hasChanged() method). In your code you ...