首页 \ 问答 \ PHP中的类似文本(similar text in PHP)

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; If similar_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 > 90

Currently I'm planning to use two loops to achieve this, external loop for $var_1 and internal loop for $var_2 . Each element of the array 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?


原文:
更新时间:2021-07-06 22:07

最满意答案

我有一个类似的情况,我使用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 ...
    • 扩展您的界面: 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, ...
    • 如果要求对象是可序列化的,则它将在接口定义中声明为这样。 如: 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 ...
    • 好主意! 这种模式非常有用,IObservable是一个内置的.Net接口。 MSDN文档提供了更多详细信息。 您的用户意见由IObserver代表,也记录在MSDN上 。 IObserver和IObservable接口为基于推送的通知提供了一种通用机制,也称为观察者设计模式。 IObservable接口表示发送通知的类(提供者); IObserver接口表示接收它们的类(观察者)。 T表示提供通知信息的类[数据传输对象] ... Good idea! This pattern's so useful, I ...
    • 如上所述, 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 ...

    相关文章

    更多

    最新问答

    更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。